Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a java.time.LocalDate from a date string using the "d. LLLL YYYY" pattern?

I'm trying to create a LocalDate object:

LocalDate date = LocalDate.parse(
    "1. juli 2014", 
    DateTimeFormatter.ofPattern("d. LLLL YYYY", new Locale("nb", "NO"));

But I get an exception:

java.time.format.DateTimeParseException: Text '1. juli 2014' could not be parsed: 
    Unable to obtain LocalDate from TemporalAccessor: 
    {WeekBasedYear[WeekFields[MONDAY,4]]=2014, MonthOfYear=7, DayOfMonth=1},
    ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1919)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1854)
at java.time.LocalDate.parse(LocalDate.java:400)

The exception seems to indicate that the correct data is in there ("juli" is Norwegian and is month number 7).

like image 810
neu242 Avatar asked Oct 31 '25 02:10

neu242


1 Answers

So, I found the solution, even though this probably is a Java 8 bug, as @NimChimpsky commented on.

By using "d. LLLL yyyy" (with year-of-era) instead of "d. LLLL YYYY" (with week-based-year), the parsing works as intended.

like image 67
neu242 Avatar answered Nov 02 '25 15:11

neu242



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!