DateTimeFormatter exception parsing its own print result [duplicate]
Asked Answered
F

1

5

I don't understand something, I have this code :

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
System.out.println(LocalDateTime.parse(LocalDateTime.now().format(fmt), fmt));

This generate and exception :

Exception in thread "main" java.time.format.DateTimeParseException: Text '20200605102607066' could not be parsed at index 0
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at Test.main(AccountPasswordHistoryTypeHandler.java:102)

But if I change the pattern to have a separator between date part and time part : "yyyyMMdd_HHmmssSSS" it works as expected.

Why is a formatter can't parse its own result ?

Fishback answered 5/6, 2020 at 8:30 Comment(4)
It works fine with me, what java version you are using?Vaal
check also the ideone demoVaal
I am using jdk 1.8u251Prurient
@Sweeper sorry, I didn't see this post.Prurient
V
6

It is a bug in Java8 check this :

JDK-8031085 : DateTimeFormatter won't parse dates with custom format "yyyyMMddHHmmssSSS"

It was fixed in the new version.

Vaal answered 5/6, 2020 at 8:42 Comment(3)
Thanks a lot, I will use the workaround.Prurient
or make an upgrade to your JDK @Fishback there are many bugs fixed and many other featuresVaal
It is more than 2 years than I want to do so :( maybe at the end of the year I will succeed to make the higher ups understand.Prurient

© 2022 - 2024 — McMap. All rights reserved.