To parse a string into a date or time in Java, create a DateTimeFormatter that matches the text and pass it to the parse method of the target type, for example LocalDate.parse(text, formatter). Use a predefined formatter such as DateTimeFormatter.ISO_LOCAL_DATE for ISO-8601 input, or DateTimeFormatter.ofPattern("dd-MM-yyyy") for a custom layout.
DateTimeFormatter lives in java.time.format, has been part of the JDK since Java 8, and is immutable and thread-safe. Unlike the old SimpleDateFormat, you can safely store one in a static final field and share it across threads.
The target type must match the information in the string. Parsing a date-only string into a LocalDateTime fails because the time fields are missing.
| String contains | Parse into |
|---|---|
| Date only | LocalDate |
| Time only | LocalTime |
| Date and time, no zone | LocalDateTime |
| Date, time and zone or region | ZonedDateTime |
| Date, time and numeric offset | OffsetDateTime |
ISO instant ending in Z | Instant |
For ISO input (yyyy-MM-dd) you do not need a formatter at all, because LocalDate.parse(CharSequence) uses ISO_LOCAL_DATE. For any other layout, build a formatter with ofPattern.
The pattern letters are case sensitive. dd is day of month, MM is month number, and uuuu is the proleptic year. yyyy means year of era and also works, but it only resolves strictly when you add an era field, so prefer uuuu when you use strict parsing.
Use HH for the 24-hour clock (00 to 23) and hh with a for the 12-hour clock with an AM/PM marker.
Always pass a Locale when the pattern contains text such as a, MMM or EEEE. Without it the formatter uses the default locale of the JVM, so PM or Nov may not parse on a machine configured for another language.
When the string carries zone information, parse into a type that can hold it. The built-in formatters cover the common cases.
In patterns, z matches a zone name such as CET, VV matches a region ID such as Europe/Paris, and xxx or XXX matches an offset like +05:30. Short zone abbreviations are ambiguous, so prefer region IDs or offsets in data you control.
By default ofPattern uses the SMART resolver style. It rejects values that are out of range for the field, but it silently adjusts a few cases, for example day 31 in a 30-day month can be rolled back for some patterns. Use ResolverStyle.STRICT when you want invalid dates to fail.
Every parse failure throws the unchecked DateTimeParseException. It tells you the text that failed and the index where parsing stopped, which makes error messages easy to produce.
For user input, catch the exception at the boundary and return a validation error rather than letting it propagate. If you must accept several layouts, keep a list of formatters and try each in turn, or build one formatter with DateTimeFormatterBuilder.appendOptional.
Pattern letters are case sensitive. mm is minutes and MM is months, and hh needs an a marker whereas HH does not. Check that every letter and every separator in the pattern matches the input exactly.
Not directly, because the time fields are missing. Parse it into a LocalDate and call atStartOfDay(), or define optional fields with DateTimeFormatterBuilder and default the time with parseDefaulting.
Yes. It is immutable, so a single instance can be shared by many threads. This is the main practical improvement over SimpleDateFormat, which was not thread-safe.
Do not use Date or SimpleDateFormat in new code. Parse with DateTimeFormatter into a java.time type, and convert only at the edge where an old API requires it, for example with Date.from(instant).
Choose the java.time type that matches the data, then call its parse method with a DateTimeFormatter. Use the predefined ISO formatters when you can, ofPattern with an explicit Locale for custom layouts, and ResolverStyle.STRICT when invalid dates must be rejected. Catch DateTimeParseException at the point where text enters your application.