代碼:無法解析00秒8 API
LocalDateTime ldt3 = getUtcDateTime("2016-02-01T10:11:00Z");
System.out.println(ldt3);
輸出:
2016-02-01T10:11
所需的輸出:
2016-02-01T10:11:00
是否有可能得出這樣的結果?另外如何將UTC String值從LocalDateTime輸出爲零秒?
代碼:
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();
輸出:
2016-02-12T12:12Z
所需的輸出:
2016-02-12T12:12:00Z
如何獲得在兩種情況下需要的輸出?
我不明白你的問題。你想做什麼?你想打印'2016-02-01T10:11:00'嗎?如果是這樣,爲什麼你要重新格式化並不打印格式化的字符串? – Tunaki
請參閱本指南https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html –