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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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);

1 Comment

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: docs.oracle.com/javase/8/docs/api/java/time/format/…. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.