In this article, we will learn how to convert String to LocalDate in Java 1.8 version using different date formats
Q) What is the need of converting String to LocalDate ?
- Generally, whenever we receive any data from web application then it is passed in the form of String
- But for further processing we need to convert String to LocalDate
- This article explains about String to LocalDate conversion in different formats
Read also,
- String to Date conversion in different formats
- Date to String conversion is also required for reverse process
- For LocalDate to String conversion, read Java 8 – How to convert LocalDate to String ?
1. String to LocalDate conversion:
We will use below formats to convert String to LocalDate
- yyyy-MM-dd (default)
- dd-MM-yyyy
- dd.MM.yyyy
- dd/MMM/yyyy
- E, MMM dd yyyy
- MMM dd, yyyy
- MM dd, yyyy
Note: for creating above formats we need DateTimeFormatter
2. Convert String to LocalDate examples:
2.1 Convert String in (yyyy-MM-dd) format to LocalDate:
ConvertStringToLocalDate1.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
public class ConvertStringToLocalDate1 {
public static void main(String[] args) {
// 1. string
String strDate = "2017-05-29";
// 2. parse date in String to default LocalDate format
LocalDate localDate = LocalDate.parse(strDate);
// 3. print to console
System.out.println("Original String :- \n" + strDate);
System.out.println("\nString in default (yyyy-MM-dd) format to LocalDate :- \n"
+ localDate);
}
}
Output:
Original String :-
2017-05-29
String in default (yyyy-MM-dd) format to LocalDate :-
2017-05-29
2.2 Convert String in (dd-MM-yyyy) format to LocalDate:
ConvertStringToLocalDate2.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDate2 {
public static void main(String[] args) {
// 1. string
String strDate = "16-02-1987";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
// 3. parse date in String format using above dateTimeFormatter
LocalDate localDate = LocalDate.parse(strDate, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDate);
System.out.println("\nString in (dd-MM-yyyy) format to LocalDate :- \n"
+ localDate);
}
}
Output:
Original String :-
16-02-1987
String in (dd-MM-yyyy) format to LocalDate :-
1987-02-16
2.3 Convert String in (dd.MM.yyyy) format to LocalDate:
ConvertStringToLocalDate3.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDate3 {
public static void main(String[] args) {
// 1. string
String strDate = "03.06.2022";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
// 3. parse date in String format using above dateTimeFormatter
LocalDate localDate = LocalDate.parse(strDate, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDate);
System.out.println("\nString in (dd.MM.yyyy) format to LocalDate :- \n"
+ localDate);
}
}
Output:
Original String :-
03.06.2022
String in (dd.MM.yyyy) format to LocalDate :-
2022-06-03
2.4 Convert String in (dd/MMM/yyyy) format to LocalDate:
ConvertStringToLocalDate4.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDate4 {
public static void main(String[] args) {
// 1. string
String strDate = "30/Jul/2014";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MMM/yyyy");
// 3. parse date in String format using above dateTimeFormatter
LocalDate localDate = LocalDate.parse(strDate, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDate);
System.out.println("\nString in (dd/MMM/yyyy) format to LocalDate :- \n"
+ localDate);
}
}
Output:
Original String :-
30/Jul/2014
String in (dd/MMM/yyyy) format to LocalDate :-
2014-07-30
2.5 Convert String in (E, MMM dd yyyy) format to LocalDate:
ConvertStringToLocalDate5.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDate5 {
public static void main(String[] args) {
// 1. string
String strDate = "Fri, Apr 23 2021";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy");
// 3. parse date in String format using above dateTimeFormatter
LocalDate localDate = LocalDate.parse(strDate, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDate);
System.out.println("\nString in (E, MMM dd yyyy) format to LocalDate :- \n"
+ localDate);
}
}
Output:
Original String :-
Fri, Apr 23 2021
String in (E, MMM dd yyyy) format to LocalDate :-
2021-04-23
2.6 Convert String in (MMM dd, yyyy) format to LocalDate:
ConvertStringToLocalDate6.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDate6 {
public static void main(String[] args) {
// 1. string
String strDate = "Apr 23, 2021";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MMM dd, yyyy");
// 3. parse date in String format using above dateTimeFormatter
LocalDate localDate = LocalDate.parse(strDate, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDate);
System.out.println("\nString in (MMM dd, yyyy) format to LocalDate :- \n"
+ localDate);
}
}
Output:
Original String :-
Apr 23, 2021
String in (MMM dd, yyyy) format to LocalDate :-
2021-04-23
2.7 Convert String in (MM dd, yyyy) format to LocalDate:
ConvertStringToLocalDate7.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDate7 {
public static void main(String[] args) {
// 1. string
String strDate = "08 11, 2022";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM dd, yyyy");
// 3. parse date in String format using above dateTimeFormatter
LocalDate localDate = LocalDate.parse(strDate, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDate);
System.out.println("\nString in (MM dd, yyyy) format to LocalDate :- \n"
+ localDate);
}
}
Output:
Original String :-
08 11, 2022
String in (MM dd, yyyy) format to LocalDate :-
2022-08-11
3. DateTimeParseException:
- If the specified String doesn’t match with the specified pattern/format then DateTimeParseException is thrown
- In the below illustration, given date in String format and the formatter doesn’t matches and hence there is DateTimeParseException
StringToLocalDateConversion.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class StringToLocalDateConversion {
public static void main(String[] args) {
// 1. string
String strDate = "16-02-1987";
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
// 3. parse date in String format using above dateTimeFormatter
LocalDate localDate = LocalDate.parse(strDate, dateTimeFormatter);
// 4. print to console
System.out.println("Original String :- \n" + strDate);
System.out.println("\nString in (dd/MM/yyyy) format to LocalDate :- \n"
+ localDate);
}
}
Output:
Exception in thread "main" java.time.format.DateTimeParseException:
Text '16-02-1987' could not be parsed at index 2
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
at java.base/java.time.LocalDate.parse(LocalDate.java:430)
at in.bench.resources.java8.localdate.examples.StringToLocalDateConversion
.main(StringToLocalDateConversion.java:19)
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!