JB Header
Java 8 - How to convert String to LocalDate and LocalDate to String in specific format
This Java 8 code tip first shows how to convert String date to java.time.LocalDate instance using the parse() method of java.time.DateTimeFormatter class. It then shows how to use the format() method of LocalDate class to convert LocalDate instance to a String in the specified format.

Java 8 code to convert String to LocalDate
package com.javabrahman.java8.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class StringToLocalDate {
  public static void main(String args[]){
   //Converting String in format 'dd-MMM-yyyy' to LocalDate
   String dateStr_1="28-Sep-2016";
   DateTimeFormatter formatter_1=DateTimeFormatter.ofPattern("dd-MMM-yyyy");
   LocalDate localDate_1= LocalDate.parse(dateStr_1,formatter_1);
   System.out.println("Input String with value: "+dateStr_1);
   System.out.println("Converted Date in default ISO format: "+localDate_1+"\n");

   //Converting String in format 'EEEE, MMM d yyyy' to LocalDate
   String dateStr_2="Wednesday, Sep 28 2016";
   DateTimeFormatter formatter_2=DateTimeFormatter.ofPattern("EEEE, MMM d yyyy");
   LocalDate localDate_2= LocalDate.parse(dateStr_2,formatter_2);
   System.out.println("Input String with value: "+dateStr_2);
   System.out.println("Converted Date in default ISO format: "+localDate_2+"\n");

   //Converting String in format 'dd/MM/YY' to LocalDate
   String dateStr_3="28/09/16";
   DateTimeFormatter formatter_3=DateTimeFormatter.ofPattern("dd/MM/yy");
   LocalDate localDate_3= LocalDate.parse(dateStr_3,formatter_3);
   System.out.println("Input String with value: "+dateStr_3);
   System.out.println("Converted Date in default ISO format: "+localDate_3);
 }
}
 OUTPUT of the above code
Input String with value: 28-Sep-2016
Converted Date in default ISO format: 2016-09-28

Input String with value: Wednesday, Sep 28 2016
Converted Date in default ISO format: 2016-09-28

Input String with value: 28/09/16
Converted Date in default ISO format: 2016-09-28
Explanation of the code
  • We start by creating a String with value 28-Sep-2016, named dateStr_1, which we then convert to a LocalDate.
  • We need to convey to the Java 8 Date-Time API the format in which the date is being given to it inside dateStr_1. This format is specified using the DateTimeFormatter class. We pass the format of the date dd-MMM-yyyy to DateTimeFormatter’s static method named ofPattern().
  • ofPattern() method returns an instance of DateTimeFormatter, named formatter_1, with the required date format set in it.
  • We then pass the dateStr_1 and formatter_1 as inputs to the LocalDate.parse() method which creates an instance of LocalDate, named localDate_1, with the value as 28th of September 2016.
  • We then print localDate_1 which prints the date as 2016-09-28 which is in the Standard ISO Format for dates.
  • Similarly, two other date formats are shown as examples to show how the date formats work resulting in dates localDate_2 and localDate_3. These are also printed in the default ISO format to show that the String has been converted properly to its equivalent LocalDate.
  • To print dates in formats other than the default ISO format, we will need to use the LocalDate.format() method. The next section of this article shows how to format a LocalDate into formats other than the default ISO format.
Java 8 code to convert LocalDate to String
package com.javabrahman.java8.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
public class LocalDateToString {
  public static void main(String args[]){
   //We will use current date from the system clock
   LocalDate today=LocalDate.now();
   System.out.println("Current date using default toString(same as ISO Standard Format): "+today);

   //Converting Date to a user specific format 1 - dd-MMM-yyyy
   DateTimeFormatter formatter_1 = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
   String format_1=(today).format(formatter_1);
   System.out.println("Current date in format 'dd-MMM-yyyy': "+format_1);

   //Converting Date to a user specific format 2 - dd/MM/yyyy
   DateTimeFormatter formatter_2 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
   String format_2=(today).format(formatter_2);
   System.out.println("Current date in format 'dd/MM/yyyy': "+format_2);

   //Converting Date to a user specific format 3 - E, dd MMM yyyy
   DateTimeFormatter formatter_3 = DateTimeFormatter.ofPattern("E, dd MMM yyyy");
   String format_3=(today).format(formatter_3);
   System.out.println("Current date in format 'E, dd MMM yyyy': "+format_3);
 }
}
 OUTPUT of the above code
Current date using default toString(same as ISO Standard Format):  2016-09-28
Current date in format 'dd-MMM-yyyy':  28-Sep-2016
Current date in format 'dd/MM/yyyy':  28/09/2016
Current date in format 'E, dd MMM yyyy':  Wed, 28 Sep 2016
Explanation of the code
  • We start by creating a LocalDate instance, named today, using the LocalDate.now() method. Object today holds the system date for today. We then print today and it is in the default ISO format as expected i.e. 2016-09-28.
  • We then create a SimpleDateFormatter, named formatter_1, using its static ofPattern() method to which we pass the required date format as a String containing dd-MMM-yyyy.
  • Next we invoke the format() method on today object with formatter_1 as the input containing the format in which we need the String equivalent of today.
  • The format() method returns a String, named format_1, containing the date in the required format i.e. 28-Sep-2016, which is then printed as output.
  • Similarly, today is printed in two different formats, as Strings format_2 and format_3, as shown in the output - 28/09/2016 and Wed,28 Sep 2016.