Parse a Date String in Java
Problem
Parsing is the reverse of formatting: it reads text according to a pattern and builds a date object, failing with an exception if the text doesn't fit.
Parse an ISO date, a day-first date, and the impossible date 31-02-2026 in both the default and strict modes.
Java Program
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
public class ParseDateString {
public static void main(String[] args) {
LocalDate iso = LocalDate.parse("2026-03-09"); // ISO format needs no pattern
System.out.println("ISO text: " + iso);
DateTimeFormatter dayFirst = DateTimeFormatter.ofPattern("dd-MM-yyyy");
System.out.println("dd-MM-yyyy: " + LocalDate.parse("09-03-2026", dayFirst));
System.out.println("Lenient 31-02: " + LocalDate.parse("31-02-2026", dayFirst)); // quietly adjusted
DateTimeFormatter strict = DateTimeFormatter.ofPattern("dd-MM-uuuu").withResolverStyle(ResolverStyle.STRICT);
try {
LocalDate.parse("31-02-2026", strict);
} catch (DateTimeParseException e) {
System.out.println("Strict 31-02: rejected as an invalid date");
}
}
}Output
Core Logic
LocalDate.parse reads ISO text directly and takes a formatter for anything else; the formatter's resolver style decides how out-of-range values are treated.
- 1
LocalDate.parse("2026-03-09")needs no pattern, because the text is ISO-8601. - 2
ofPattern("dd-MM-yyyy")reads day-first text such as09-03-2026. - 3The default SMART resolver quietly turns 31 February into 28 February, the last valid day.
- 4With
uuuuandResolverStyle.STRICT, the same text throwsDateTimeParseException, which is caught and reported.
2026-03-09 and 09-03-2026 produce the same LocalDate.Key Point: Validate user input with a STRICT formatter — the default can silently accept an impossible date. STRICT also requires uuuu rather than yyyy, because yyyy needs an era to resolve.
Key Concepts
Approach 2: Parsing Date and Time Together
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class ParseDateTimeString {
public static void main(String[] args) {
LocalDateTime a = LocalDateTime.parse("2026-03-09T14:05:30");
System.out.println("ISO: " + a);
DateTimeFormatter custom = DateTimeFormatter.ofPattern("d MMM yyyy, h:mm a", Locale.ENGLISH);
LocalDateTime b = LocalDateTime.parse("9 Mar 2026, 2:05 PM", custom);
System.out.println("Custom: " + b);
System.out.println("Hour (24h): " + b.getHour());
}
}
Output
Core Logic
LocalDateTime.parse works the same way, and a pattern with h and a reads 12-hour times with AM/PM.
- 1ISO text with a
Tseparator parses without a formatter. - 2
d MMM yyyy, h:mm amatches text like9 Mar 2026, 2:05 PM. - 3
Locale.ENGLISHletsMarandPMbe recognised regardless of the machine's language. - 4
getHour()returns 14, confirming the 24-hour value.
2:05 PM becomes 14:05 after parsing.Key Point: A formatter without a locale uses the system default, so the same code can fail on a machine set to another language.
Key Concepts
Interview Questions
Why does parsing "2026-3-9" fail?
The ISO format expects two-digit months and days. Use DateTimeFormatter.ofPattern("yyyy-M-d") to accept single digits.
How do you parse dates from several possible formats?
Try each formatter in turn and catch DateTimeParseException, or build one formatter with optional sections using [ and ] in the pattern.