Dmitrii Bocharov
Dmitrii Bocharov

Reputation: 935

java.time.format.DateTimeParseException at LocalDateTime.parse

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

Upvotes: 2

Views: 3452

Answers (2)

Ruslan Akhundov
Ruslan Akhundov

Reputation: 2216

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

Upvotes: 6

mzherdev
mzherdev

Reputation: 462

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

Upvotes: 2

Related Questions