0

I am having trouble with DateTimeFormat with this kind of Date : 2021-07-32 My formatter auto changed this into 2021-08-01 How do i disable this feature and return an exception?

1 Answers1

2

You can use ResolverStyle, for example

DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd")
        .withResolverStyle(ResolverStyle.STRICT);

LocalDate date = LocalDate.parse("2021-07-32", dateFormatter);

System.out.println(date);
//Exception: Text '2021-07-32' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 32
lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20