Search code examples
javadatedatetimetimezoneddatetime

java.time.format.DateTimeParseException: Unable to obtain ZonedDateTime from TemporalAccessor in format ddMMyyyyhhmmss


I'm having trouble in formatting a string to a ZonedDateTime.

My customer wants to have the date in a format such as ddMMyyyyhhmmss, with no separators or stuff like that.

This is what I've done so far

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;


public class MyClass {
    public static void main(String args[]) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyyhhmmss");
        String test = formatter
        .format(ZonedDateTime.now()).toString();
        System.out.println(test);
        ZonedDateTime a = ZonedDateTime.parse(test, formatter);
        System.out.println(a.toString());
    }
}

While it correctly produces the string, the error occurs at the parsing process for creating the LocalDateTime variable:

28032019100707

Exception in thread "main" java.time.format.DateTimeParseException: Text '28032019100707' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
    at MyClass.main(MyClass.java:14)
Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 of type java.time.format.Parsed
    at java.time.ZonedDateTime.from(ZonedDateTime.java:565)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 2 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneId from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 of type java.time.format.Parsed
    at java.time.ZoneId.from(ZoneId.java:466)
    at java.time.ZonedDateTime.from(ZonedDateTime.java:553)
    ... 4 more
Command exited with non-zero status 1

Searching on Stack Overflow, I saw that some answers to the same issue suggested to use the LocalDateTime class as an intermediate and then parse to ZonedDateTime, but it is still not working, throwing the same error.

I've also tried in changing the way I initialize a DateTimeFormatter with this procedure:

DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("ddMMyyyyhhmmss")
                          .toFormatter()
                          .withZone(ZoneId.systemDefault());

But it is still not working. How can I fix it?


Solution

  • You want:

      String test = ZonedDateTime.now().format(formatter);