BackendBench
Coffee

Home โ€บ Java exceptions โ€บ DateTimeParseException

Unchecked exception ยท java.time.format

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): 13

Extends 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

How to fix it

  1. Define a DateTimeFormatter.ofPattern that matches the input exactly, and test it with sample values.
  2. Parse to the type that matches the data: LocalDate.parse for dates, OffsetDateTime.parse for values with offsets.
  3. Use the Java DateTimeFormatter tester on this site to preview a pattern.
  4. Validate or catch the exception at the input boundary and return a clear error.

Broken and fixed

fails
LocalDate.parse("09/03/2026"); // default expects 2026-03-09
works
LocalDate.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.

Related exceptions

More tools you might need