1

I have following code

final String pattern = "MM/dd/YYYY";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDate localDate = LocalDate.parse(date, formatter);

and while trying to parse String date = "05/29/2003" I receive an exception:

Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2023, DayOfMonth=29, MonthOfYear=5},ISO of type java.time.format.Parsed

What is the reason ?

kris82pl
  • 977
  • 3
  • 12
  • 21

1 Answers1

4

Your pattern is not correct instead, try this:

final String pattern = "MM/dd/uuuu"; // or "MM/dd/yyyy" 

Formatting patterns are case-sensitive. The "yyyy" represents the year, while "YYYY" represents the week-based year. So you need to use yyyy or much better uuuu to solve your issue.


Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140