-->
Skip to main content

LocalDate parse string example | Convert String to LocalDate [2021]

LocalDate parse string example | Convert String to LocalDate [2021] 

In this tutorial, we will see LocalDate parse string example and show how to convert string to localdate in java. There are two types of parse methods in the LocalDate Java class.
  1. LocalDate parse(CharSequence text)
  2. LocalDate parse(CharSequence text, DateTimeFormatter formatter)
LocalDate Java class is an immutable and thread-safe class introduced in new Date and Time API in Java8 and date is represented in format YYYY-MM-dd.


LocalDate parse String Example

Localdate Parse String to LocalDate example

Here we will parse a string into LocalDate instance 
  1. Using LocalDate.parse(CharSequence text) with default LocalDate format YYYY-MM-dd 
  2. Using LocalDate.parse(CharSequence text, DateTimeFormatter formatter) with custom LocalDate format MM/dd/yyyy.
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;

 public class LocalDateParseStringExample1 {
  public static void main(String[] args) {
    // Using Default format yyyy-MM-dd
    LocalDate today = LocalDate.parse("2020-07-16");
    System.out.println("With Default format yyyy-MM-dd : " + today);

    // Using Custom format MM/dd/yyyy
    DateTimeFormatter formatter =
        DateTimeFormatter.ofPattern("MM/dd/yyyy");
    LocalDate date = LocalDate.parse("07/16/2020", formatter);
    System.out.println("With Custom format MM/dd/yyyy : " + date);
  }
 }

Output:

With Default format yyyy-MM-dd : 2020-07-16
With Custom format MM/dd/yyyy : 2020-07-16
For the custom format, we are using DateTimeFormatter class for converting a String date to a LocalDate.

LocalDate Parse Multiple Formats

Now we will see multiple formats in which we can convert string to localdate.

1. LocalDate Parse MM/dd/yyyy

In this, we are going to parse String as localdate instance in format MM/dd/yyyy
 // Using Custom format MM/dd/yyyy
 DateTimeFormatter formatter =
    DateTimeFormatter.ofPattern("MM/dd/yyyy");
 LocalDate date = LocalDate.parse("07/16/2020", formatter);
 System.out.println("With Custom format MM/dd/yyyy : " + date);

Output:

With Custom format MM/dd/yyyy : 2020-07-16

2. LocalDate parse dd-MMM-yyyy

In this example, we are going to parse LocalDate from string using custom format dd-MMM-yyyy.

 // Using Custom format dd-MMM-yyyy
 DateTimeFormatter formatter1 =
    DateTimeFormatter.ofPattern("dd-MMM-yyyy");
 LocalDate date1 = LocalDate.parse("16-Jul-2020", formatter1);
 System.out.println("With Custom format dd-MMM-yyyy : " + date1);

Output:

With Custom format dd-MMM-yyyy : 2020-07-16

3. LocalDate parse dd/MM/yyyy

In this example, we are going to parse String into LocalDate using custom format dd/MM/yyyy.
 // Using Custom format dd/MM/yyyy
 DateTimeFormatter formatter2 =
   DateTimeFormatter.ofPattern("dd/MM/yyyy");
 LocalDate date2 = LocalDate.parse("16/07/2020", formatter2);
 System.out.println("With Custom format dd/MM/yyyy : " + date2);

Output:

With Custom format dd/MM/yyyy : 2020-07-16

4. LocalDate parse E, MMM dd yyyy

In this example, we are going to parse String in LocalDate using custom format E, MMM dd yyyy.
 // Using Custom format E, MMM dd yyyy
 DateTimeFormatter formatter3 =
    DateTimeFormatter.ofPattern("E, MMM dd yyyy");
 LocalDate date3 =
   LocalDate.parse("Thu, Jul 16 2020", formatter3);
 System.out.println("With Custom format E, MMM dd yyyy : " + date3);

Output:

With Custom format E, MMM dd yyyy : 2020-07-16

5. LocalDate parse EEEE, MMM dd, yyyy HH:mm:ss a

In this example, we are going to parse String as LocalDate using custom format EEEE, MMM dd, yyyy HH:mm:ss a.
 // Using Custom format EEEE, MMM dd, yyyy HH:mm:ss a
 DateTimeFormatter formatter4   =
  DateTimeFormatter.ofPattern("EEEE, MMM dd, yyyy HH:mm:ss a");
 LocalDate date4 = 
  LocalDate.parse("Thursday, Jul 16, 2020 10:10:10 AM", formatter4);
 System.out.println("With Custom format EEEE, MMM dd, yyyy HH:mm:ss a : " + date4);

Output:

With Custom format EEEE, MMM dd, yyyy HH:mm:ss a : 2020-07-16

6. LocalDate parse using DateTimeFormatter.ISO_LOCAL_DATE_TIME

In this example, we are getting LocalDate after parse from String using format DateTimeFormatter.ISO_LOCAL_DATE_TIME.
 // Using DateTimeFormatter.ISO_LOCAL_DATE_TIME
 DateTimeFormatter formatter5 =
        DateTimeFormatter.ISO_LOCAL_DATE_TIME;
 LocalDate date5 = 
    LocalDate.parse("2020-07-16T09:53:30", formatter5);
 System.out.println("Using DateTimeFormatter.ISO_LOCAL_DATE_TIME : " + date5);

Output:

Using DateTimeFormatter.ISO_LOCAL_DATE_TIME : 2020-07-16

7. LocalDate parse using DateTimeFormatter.ISO_DATE_TIME

In this example, we are going to parse String to LocalDate using format DateTimeFormatter.ISO_DATE_TIME.
 //Using DateTimeFormatter.ISO_DATE_TIME
 DateTimeFormatter formatter6 =
    DateTimeFormatter.ISO_DATE_TIME;
 LocalDate date6 =
    LocalDate.parse("2020-07-16T09:53:30+05:30", formatter6);
 System.out.println("Using DateTimeFormatter.ISO_DATE_TIME : " + date6);

Output:

Using DateTimeFormatter.ISO_DATE_TIME : 2020-07-16

8. LocalDate parse UTC Timezone

In this example, we are going to parse a string to LocalDate in UTC Timezone 
 //Using DateTimeFormatter with UTC Timezone
 DateTimeFormatter formatter7 = 
    DateTimeFormatter.ofPattern("dd-MM-yyyy")
   .withZone(ZoneId.of("UTC"));
 LocalDate date7 =
     LocalDate.parse("16-07-2020", formatter7);
 System.out.println("Using DateTimeFormatter with UTC Timezone : " + date7);

Output:

Using DateTimeFormatter with UTC Timezone : 2020-07-16

Conclusion:

In this tutorial, We have covered LocalDate parse String Examples with different custom and default formats and even we parse the string to LocalDate in UTC Timezone.

Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.

Happy Learning!!!

Comments