LocalDate.parse()
Parses a date from a string — in ISO format (2025-03-14) or with a pattern.
Available since: Java 8
Syntax
static LocalDate parse(CharSequence text, DateTimeFormatter formatter)Parameters
| Parameter | Type | Description |
|---|---|---|
| text | CharSequence | The string with a date. |
| formatter | DateTimeFormatter | The string format. Without it the ISO format yyyy-MM-dd is expected. |
Return value
LocalDateThe date.
Examples
ISO format and a custom pattern:
void main() {
LocalDate a = LocalDate.parse("2025-03-14");
LocalDate b = LocalDate.parse("14.03.2025", DateTimeFormatter.ofPattern("dd.MM.yyyy"));
IO.println(a.equals(b));
}Output:
trueAn invalid string throws DateTimeParseException:
void main() {
try {
LocalDate.parse("14/03/2025");
} catch (DateTimeParseException e) {
IO.println(e.getMessage());
}
}Output:
Text '14/03/2025' could not be parsed at index 0Notes
LocalTime.parse("09:30") and LocalDateTime.parse("2025-03-14T09:30") work the same way. If the pattern has month names, give the formatter a language: ofPattern("d MMMM yyyy", Locale.ENGLISH).
See also
- LocalDate.of() — Creates a date from a year, month and day.
- DateTimeFormatter.ofPattern() — Creates a formatter for printing and parsing dates with a pattern such as dd.MM.yyyy.
Related recipes
In the tutorial
- Formatting and parsing dates — DateTimeFormatter in Java — patterns like dd.MM.yyyy, month names in the language you need, parsing a string into a date and pattern mistakes.