Home โบ Java exceptions โบ DateTimeParseException
DateTimeParseException: causes and fixes
A date or time string did not match the expected pattern or contained an invalid value.
What it looks like
java.time.format.DateTimeParseException: Text '2026-13-01' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 13Extends DateTimeException in java.time.format.
What it means
The parse methods of LocalDate, LocalDateTime, Instant, ZonedDateTime and DateTimeFormatter throw it with the text and the position or reason. There are two families of failure: the text does not match the pattern (Text could not be parsed at index 4), or it matches but a value is out of range, such as month 13 or 30 February. A pattern that lacks fields the target type needs also fails, for example parsing a date-only string as a LocalDateTime.
Common causes
- The input format differs from the formatter:
dd/MM/yyyytext parsed with the ISO formatter. - A pattern letter mistake:
mm(minutes) instead ofMM(months), orYYYY(week-based year) instead ofyyyy. - A missing field: a LocalDateTime needs a time, an Instant needs a zone or offset.
- Invalid values such as month 13, day 31 in April, or 24:30.
How to fix it
- Define a
DateTimeFormatter.ofPatternthat matches the input exactly, and test it with sample values. - Parse to the type that matches the data:
LocalDate.parsefor dates,OffsetDateTime.parsefor values with offsets. - Use the Java DateTimeFormatter tester on this site to preview a pattern.
- Validate or catch the exception at the input boundary and return a clear error.
Broken and fixed
LocalDate.parse("09/03/2026"); // default expects 2026-03-09LocalDate.parse("09/03/2026", DateTimeFormatter.ofPattern("dd/MM/yyyy"));๐ Unravel the failing code ๐ Unravel the fix
In Spring and enterprise code
Jackson and Spring MVC use their own conversion errors for dates in JSON and form fields, but the root cause is usually the same pattern mismatch.
Debug it faster
Paste the whole stack trace into the stack trace beautifier to hide framework frames, jump to the root cause and find the first line of your own code. It runs in your browser, so the trace never leaves your machine.
Frequently asked questions
Is DateTimeParseException a checked or unchecked exception?
DateTimeParseException is an unchecked exception (it extends DateTimeException, which is a RuntimeException), so you are not forced to catch it. Prevent it with validation, or handle it where the input enters the system.
Which package is DateTimeParseException in?
It lives in java.time.format, so the import is import java.time.format.DateTimeParseException;.
What is the first thing to check when I see DateTimeParseException?
Read the message and the first stack trace line in your own code. The usual causes are: The input format differs from the formatter: dd/MM/yyyy text parsed with the ISO formatter; and A pattern letter mistake: mm (minutes) instead of MM (months), or YYYY (week-based year) instead of yyyy.