DateTimeFormatter exception parsing its own print result

Viewed 74

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 ?

1 Answers
Related