Reputation: 357
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
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