Java Convert String to LocalDate
Tags: String Java LocalDate Java 8 DateTimeFormatter
In this Java core tutorial we learn how to convert a String value to a java.time.LocalDate object in Java programming language with different solutions and example Java codes.
Table of contents
- How to convert String to LocalDate in Java
- How to convert String to LocalDate with custom date format
How to convert String to LocalDate in Java
In Java, we can use the LocalDate.parse(CharSequence text) static method to convert a String in ISO-8601 extended local date format to a LocalDate object as the example Java code below.
ConvertStringToLocalDateExample1.java
import java.time.LocalDate;
public class ConvertStringToLocalDateExample1 {
public static void main(String... args) {
LocalDate localDate = LocalDate.parse("2022-05-20");
System.out.println(localDate);
}
}
2022-05-20
How to convert String to LocalDate with custom date format
Using the LocalDate.parse(CharSequence text, DateTimeFormatter formatter) method we can convert a date String in specified format to LocalDate object as following example Java code.
ConvertStringToLocalDateExample2.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDateExample2 {
public static void main(String... args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localDate = LocalDate.parse("20/05/2022", dateTimeFormatter);
System.out.println(localDate);
}
}
2022-05-20
Happy Coding 😊
Related Articles
Java Convert String to LocalDateTime
Java Convert String to LocalTime
Java Convert Date to ZonedDateTime
Java Convert Date to LocalDateTime
Java Convert Date to LocalTime
Java Convert Calendar to LocalDateTime