Java 8 – How to convert String to LocalDate ?

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,

1. String to LocalDate conversion:

We will use below formats to convert String to LocalDate

  1. yyyy-MM-dd (default)
  2. dd-MM-yyyy
  3. dd.MM.yyyy
  4. dd/MMM/yyyy
  5. E, MMM dd yyyy
  6. MMM dd, yyyy
  7. 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDate in different formats ?
Java 8 – How to parse LocalDate in String form ?