2

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

Dmitrii Bocharov
  • 872
  • 6
  • 21

2 Answers2

6

I believe its because ISO_INSTANT is for parsing Instant

As the javadoc says:

This is a special case formatter intended to allow a human readable form of an java.time.Instant.

This code works:

    String dateString = "2018-03-01T14:52:48Z";
    DateTimeFormatter dtf = DateTimeFormatter.ISO_INSTANT;
    Instant instant = Instant.from(dtf.parse(dateString));

To make it localdatetime you can use this:

LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault())

This is a good explanation about such errors: Java SE 8 TemporalAccessor.from issues when used with a java.time.Instant object

Ruslan Akhundov
  • 2,178
  • 4
  • 20
  • 38
2

LocalDateTime doesnt contain Zone in self. If you need parse string with zone useZonedDateTime`:

DateTimeFormatter dtf = DateTimeFormatter.ISO_INSTANT.withZone(ZoneId.systemDefault());
ZonedDateTime dateTime = ZonedDateTime.parse(dateString, dtf);
mzherdev
  • 462
  • 1
  • 4
  • 11
  • I also don't have zone in my string. And DateTimeFormatter.ISO_INSTANT also doesn't have zone: "Date and Time of an Instant" from the docs: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT. So i didn't want to parse it with zone. Ruslan's answer is more correct here. Especially the link to the other question helped with explanation of the Instant. – Dmitrii Bocharov Mar 13 '18 at 14:16