Viraj
Viraj

Reputation: 357

Cannot resolve 00 seconds when preparing specific Format of UTC date in Java SE 8 API

Code:

LocalDateTime ldt3 = getUtcDateTime("2016-02-01T10:11:00Z");
System.out.println(ldt3);

Output:

2016-02-01T10:11

Required Output:

2016-02-01T10:11:00

Is it possible to derive such a output? Also how to print the UTC String value out of LocalDateTime having zero seconds?

Code:

LocalDateTime localDateTime = getCurrentUtcDateTime();
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Z"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
ZonedDateTime.parse(zonedDateTime.format(formatter), formatter)
                .toString();

Output:

2016-02-12T12:12Z

Required Output:

2016-02-12T12:12:00Z

How to get the required outputs in both cases?

Upvotes: 0

Views: 107

Answers (1)

Ferrybig
Ferrybig

Reputation: 18834

You should use the DateTimeFormatter to print out your output, instead of reparsing it again. A LocalDateTime has no system to store the formatter it was created with.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
System.out.println(zonedDateTime.format(formatter));

Upvotes: 2

Related Questions