For one of the projects, I was given a millisecond from another system. I need to convert to "yyyy-DDDT:HH:mm:ssZ"
The trick is to display it in leap-second.
Since Java libraries does not have leap-second, I decided to use Time4J's Moment class.
But Moment's toString()
is printing yyyy-MM-ddTHH:mm:ss
.
Is it possible to get day of year in Moment class?
Is there any example to create a custom formatter. I tried a few including ChronoFormatter
. but could not get the desired results.
One nasty workaround is to use Calendar
for Date part and Moment
for time part.
But it is tedious, and will be difficult when parsing back from String.
Formatting or parsing moments/instants with the element day-of-year is no special problem with the class Moment
and can be done just by choosing a suitable pattern, see this demonstration for a leap second:
Moment ls = PlainTimestamp.of(2016, 12, 31, 23, 59, 59).atUTC().plus(1, SI.SECONDS);
System.out.println(ls); // 2016-12-31T23:59:60Z
ChronoFormatter<Moment> f =
ChronoFormatter.ofMomentPattern(
"uuuu-DDD'T'HH:mm:ssX", PatternType.CLDR, Locale.ROOT, ZonalOffset.UTC);
String text = f.format(ls);
System.out.println(text); // 2016-366T23:59:60Z
Moment parsed = f.parse(text);
System.out.println(parsed.isSimultaneous(ls)); // true
Keep in mind that any conversion of Moment
-objects to for example java.time.Instant
or java.util.Date
will loose the leap second info however.
Another point: Getting access to the day-of-year-element in class Moment
is not directly possible because the day-of-year is a local element not related to the universal time without zone offset but can be done programmatically if you also specify a zone offset or a timezone. Example:
System.out.println(ls.get(PlainDate.DAY_OF_YEAR.at(ZonalOffset.UTC))); // 366
The same procedure can also be applied to time elements:
System.out.println(ls.get(PlainTime.SECOND_OF_MINUTE.at(ZonalOffset.UTC))); // 60
About the last example: It will only work in context of Moment
but not in context of the local type PlainTime
, of course. The leap second support is deeply embedded in Time4J on all levels when handling the class Moment
, programmatically and in formatting and parsing.