Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

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

ParameterTypeDescription
textCharSequenceThe string with a date.
formatterDateTimeFormatterThe 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:

true

An 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 0

Notes

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

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.

Thanks for reading!

Donate to the site — we work hard for you.