Why does the following code:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class TestProject {
public static void main(String[] args) {
String dateString = "2018-03-01T14:52:48Z";
DateTimeFormatter dtf = DateTimeFormatter.ISO_INSTANT;
LocalDateTime localDateTime = LocalDateTime.parse(dateString, dtf);
}
}
throw:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2018-03-01T14:52:48Z' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {NanoOfSecond=0, InstantSeconds=1519915968, MilliOfSecond=0, MicroOfSecond=0},ISO of type java.time.format.Parsed
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1959)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1894)
at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
at org.bdshadow.TestProject.main(TestProject.java:11)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {NanoOfSecond=0, InstantSeconds=1519915968, MilliOfSecond=0, MicroOfSecond=0},ISO of type java.time.format.Parsed
at java.base/java.time.LocalDateTime.from(LocalDateTime.java:461)
at java.base/java.time.format.Parsed.query(Parsed.java:235)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1890)
... 2 more
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {NanoOfSecond=0, InstantSeconds=1519915968, MilliOfSecond=0, MicroOfSecond=0},ISO of type java.time.format.Parsed
at java.base/java.time.LocalDate.from(LocalDate.java:396)
at java.base/java.time.LocalDateTime.from(LocalDateTime.java:456)
... 4 more
The string representation of the date looks totally like in the javadocs for DateTimeFormatter.ISO_INSTANT
: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT. And it hasn't changed in java 9: https://docs.oracle.com/javase/9/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT