Java Programs›Date & Time›Parse a Date String

Parse a Date String in Java

intermediate·  Date & Time  ·  Date & Time

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.

Input
"2026-03-09", "09-03-2026", "31-02-2026"
Output
2026-03-09, 2026-03-09, 2026-02-28 (lenient) / rejected (strict)

Java Program

Java
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

ISO text: 2026-03-09 dd-MM-yyyy: 2026-03-09 Lenient 31-02: 2026-02-28 Strict 31-02: rejected as an invalid date

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.

How It Works
  1. 1LocalDate.parse("2026-03-09") needs no pattern, because the text is ISO-8601.
  2. 2ofPattern("dd-MM-yyyy") reads day-first text such as 09-03-2026.
  3. 3The default SMART resolver quietly turns 31 February into 28 February, the last valid day.
  4. 4With uuuu and ResolverStyle.STRICT, the same text throws DateTimeParseException, which is caught and reported.
Both 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

LocalDate.parse()DateTimeFormatterResolverStyle.STRICTDateTimeParseException

Approach 2: Parsing Date and Time Together

Java
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

ISO: 2026-03-09T14:05:30 Custom: 2026-03-09T14:05 Hour (24h): 14

Core Logic

LocalDateTime.parse works the same way, and a pattern with h and a reads 12-hour times with AM/PM.

How It Works
  1. 1ISO text with a T separator parses without a formatter.
  2. 2d MMM yyyy, h:mm a matches text like 9 Mar 2026, 2:05 PM.
  3. 3Locale.ENGLISH lets Mar and PM be recognised regardless of the machine's language.
  4. 4getHour() 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

LocalDateTime.parse()12-hour patternLocale

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.

Learn the Concept

Related Programs