Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimeFormatter exception parsing its own print result [duplicate]

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 ?

like image 770
iXô Avatar asked Jun 05 '20 08:06

iXô


Video Answer


1 Answers

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.

like image 104
YCF_L Avatar answered Oct 28 '22 04:10

YCF_L