Java 8 LocalDate is a date without a time zone in the ISO-8601 calendar system.
LocalDate.parse() method can be used to convert a String to Java 8 LocalDate.
In this tutorial, you will learn different ways convert a String to Java 8 LocalDate object using the LocalDate.parse()
method.
Using LocalDate.parse()
A ISO-8601 standard date format(“YYYY-MM-DD”) string can be directly converted to LocalDate using the LocalDate.parse(string)
method.
This can be used when the default ISO-8601 Date standard is followed in your application.
To convert a String to LocalDate without any explicit formatter,
- Invoke the LocalDate.parse(string) method passing the string
- String should be matchig the ISO-8601 standard format.
- An LocalDate object will be returned
Code
LocalDate localDate = LocalDate.parse("2023-02-14");
System.out.println(localDate); //2023-02-14
The parsed LocalDate value is printed in the default ISO-8601 standard format.
You can also use Predefined DateTimeFormatter options instead of custom patterns.
Custom format Using DateTimeFormatter
The Java 8 DateTimeFormatter can be used with the required date pattern to convert to a string
to LocalDate
using the LocalDate.parse(string, formatter)
method.
This can be used when the default Date standard is not used, and a specific format is required to convert the String to LocalDate.
To convert a String to LocalDate using DateTimeFormatter,
- Use DateTimeFormatter.ofPattern(“d/MM/yyyy”) method to define a custom pattern
- Invoke the LocalDate.parse(string, formatter) method with the string and custom dateTimeFormatter.
- An LocalDate Object will be returned.
Code
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
LocalDate localDate = LocalDate.parse("14/02/2023", formatter);
System.out.println(localDate); //2023-02-14
The DateTimeFormatter is used to understand the format(d/MM/yyyy
) of the String, and after converting to LocalDate, it is printed in the default ISO-8601 standard format.
Using specific Locale conversion
The Locale specification is required to convert string to LocalDate specific to region.
In the below example, The pattern("E, MMM d yyyy"
) used only will work for Locale US and Locale.Europe and executing this on any other region will throw DateTimeParseException.
This can be used when region-specific conversion is required, and it is always good to have Locale based conversion to avoid DateTimeParseException.
To convert String to LocalDate specific to Locale,
- Use DateTimeFormatter.ofPattern(“d/MM/yyyy”, Locale.US) method to define a custom pattern with Locale value
- Invoke the LocalDate.parse(string, formatter)
- An LocalDate object is returned.
Let us see an example, when specific Locale is set and not passed while parsing the string to LocalDate.
Code
Locale.setDefault(Locale.FRANCE);
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM d yyyy");
LocalDate localDate = LocalDate.parse("Sat, Feb 18 2023", formatter);
System.out.println(localDate); //Exception in thread "main" java.time.format.DateTimeParseException: Text 'Sat, Feb 18 2023' could not be parsed at index 0
The pattern passed is not accepted in Locale.FRANCE and conversion results in a DateTimeParseException. By providing Locale-specific information to the formatter, the conversion is successful as like below,
Code
Locale.setDefault(Locale.FRANCE);
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM d yyyy", Locale.US);
LocalDate localDate = LocalDate.parse("Sat, Feb 18 2023", formatter);
System.out.println(localDate); //2023-02-18