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?
Asked
Active
Viewed 63 times
0

Nguyễn Văn Quyền
- 350
- 1
- 10
-
July 32nd is August 1st. – seenukarthi Aug 05 '21 at 10:19
-
How can i disable this feature and return an exception?, that what i want. – Nguyễn Văn Quyền Aug 05 '21 at 10:20
-
Is that the Joda-Time `DateTimeFormat` class? – Ole V.V. Aug 05 '21 at 11:54
-
1Or [`@DateTimeFormat`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/format/annotation/DateTimeFormat.html). – MC Emperor Aug 05 '21 at 11:55
-
I can’t reproduce. I get `org.joda.time.IllegalFieldValueException: Cannot parse "2021-07-32": Value 32 for dayOfMonth must be in the range [1,31]`. – Ole V.V. Aug 05 '21 at 11:57
-
Does this answer your question? [SimpleDateFormat parse in wrong date for 29 February \[duplicate\]](https://stackoverflow.com/questions/68042689/simpledateformat-parse-in-wrong-date-for-29-february) – Ole V.V. Aug 05 '21 at 11:59
1 Answers
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