Java LocalDate parse() Method
Example
Turn a text into a date:
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate myDate = LocalDate.parse("2026-01-15");
System.out.println(myDate);
System.out.println(myDate.getMonth());
}
}
Definition and Usage
The parse() method creates a LocalDate object from a text (a String).
By default, the text must be in the format yyyy-MM-dd, for example "2026-01-15". To read a date in another format, add a DateTimeFormatter.
Tip: LocalDate.parse("15/01/2026", DateTimeFormatter.ofPattern("dd/MM/yyyy")) reads a date in the format day/month/year.
Syntax
One of the following:
public static LocalDate parse(CharSequence text)
public static LocalDate parse(CharSequence text, DateTimeFormatter formatter)
CharSequence is an interface that String implements, so you can pass a String.
Parameter Values
| Parameter | Description |
|---|---|
| text | Required. The text to read, for example "2026-01-15". |
| formatter | Optional. A DateTimeFormatter that describes the format of the text. If it is left out, the format yyyy-MM-dd is used. |
Technical Details
| Returns: | A LocalDate object with the date from the text. |
|---|---|
| Throws: | DateTimeParseException - if the text is not a valid date in the expected format. |
Related Pages
❮ Date and Time Methods