Why I don tcan parse "01-01-2000" to 01.jan.2000 (LocalDate)? (Java)


Hello my brothers from another mothers!
When I have

the String "01-01-2000" I can easy parse it to locale date with DateTimeFormatter.ofPattern("d-MM-yyyy")
In combination with Localdate.parse("01-01-2000",DateTimeFormatter.ofPattern("d-MM-yyyy") it works.

But! When I want it to make 01.jan.2000 it dont work!
I tried it so:
LocalDate variablle=Localdate.parse("01-01-2000",DateTimeFormatter.ofPattern("d-MjMM-yyyy")

Normally MMM should show it now with the month name…

But It dont work!

>Solution :

jan will not work, the only way you can do this is with Jan (mind the uppercase of the first letter). The following works:

LocalDate variable = LocalDate.parse("01.Jan.2000", DateTimeFormatter.ofPattern("dd.MMM.yyyy"));

It seems that what you want is to format a LocalDate variable after parsing a plain String. The following is what you are looking for:

LocalDate date = LocalDate.parse("01-01-2000", DateTimeFormatter.ofPattern("dd-MM-yyyy"));

Leave a Reply Cancel reply