java.time.format.DateTimeParseException: Unable to

2020-04-19 05:52发布

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 SO, 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. I know that I am surely missing something stupid but I can't see what. Can anybody point me in the right direction?

3条回答
啃猪蹄的小仙女
2楼-- · 2020-04-19 06:17

In the string you parse, there is no information about the TimeZone - so how should the Formatter know how to transer into ZonedDateTime?

You either need to have the TimeZone information in your string, or use another Object to parse it into which has no TimeZone infomation and afterwards transfer it into the Zone you need.

查看更多
Lonely孤独者°
3楼-- · 2020-04-19 06:19

You want :

  String test = ZonedDateTime.now().format(formatter);
查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-04-19 06:30

ddMMyyyyhhmmss does not contain any zone information, it's not ZonedDateTime, it should be LocalDateTime.

查看更多
登录 后发表回答