PiterM
PiterM

Reputation: 41

OffsetDateTime.toString() removes seconds and nanoseconds when they are 0

For example when I am trying to print such OffsetDateTime:

OffsetDateTime offsetDateTime = OffsetDateTime.of(2018, 3, 18, 0, 0, 0, 0, ZoneOffset.UTC);

output is:

2018-03-18T00:00Z

instead of

2018-03-18T00:00:00.000Z

Maybe you know are there any easier way to do it then implement own toString/converter method?

Upvotes: 4

Views: 3832

Answers (1)

Veselin Davidov
Veselin Davidov

Reputation: 7081

From Java API: format(DateTimeFormatter formatter) Formats this date-time using the specified formatter.

I would guess the easiest for you would be to use:

OffsetDateTime.of(2018, 3, 18, 0, 0, 0, 0, ZoneOffset.UTC).format(DateTimeFormatter.ISO_DATE_TIME);

Upvotes: 3

Related Questions