Open In App

Convert String to LocalDate in Java

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to convert a String into Local Date format in Java. It will need to take a data value as a String after that by using LocalDate class we get the required result.

Note: LocalDate class is available in java.time package in Java

Methods to Convert String to LocalDate in Java

The Major use of the parse method in the concept makes it one of the most useful Methods for the topic.

Syntax

// Parse the String to LocalDate using the formatted
LocalDate localDate = LocalDate.parse(dateString, formatter);

Methods for Conversion using Parse() Method in Java are mentioned below:

  • Default Pattern Parse() Method in Java
  • Creating Custom Pattern with Parse() Method
  • Locale-specific Patterns

1. Default Pattern

In this example, I take one date value as a String value after that by using the LocalDate Parse method we convert the String into a LocalDate Value. After that, for better understanding, I display the class names of the input string as well as the local date.

Example of Default Pattern with Parse() Method:

Java




// Java Program to implement
// parse() Method with Default Pattern
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
  
// Driver Class
public class StringToLocalDateExampleOne {
    // Main Function
      public static void main(String[] args) {
        // Example String representing a date
        String dateString = "2024-01-20";
  
        // Parse the String to LocalDate using the formatter
        LocalDate localDate = LocalDate.parse(dateString);
  
        // Output the type of the dateString variable
        System.out.println("Type of dateString: " + dateString.getClass().getName());
        System.out.println("Type of localDate: " + localDate.getClass().getName());
        
        // Output the LocalDate
        System.out.println("Parsed LocalDate: " + localDate);
    }
}


Output

Type of dateString: java.lang.String
Type of localDate: java.time.LocalDate
Parsed LocalDate: 2024-01-20

Explaination of the above method:

In the above code the dateString variable contains a string value represents the date in the format of String. After that I used DateTimeFormatter class for getting expected date format in this example my date format is yyyy-MM-dd. After that I used LocalDate.parse method for parse the String to LocalDate by using DateTimeFormatter class object. After that I print the class names of the input string and localdate for getting better understanding on the concept.

2. Creating Custom Pattern in Java

Parse() Method with Custom Pattern unlike the default

Default: YYYY-MM-DD
Custom: DD-MM-YYYY

Example of Creating Custom Pattern in Java mentioned below:

Java




// Java Program to implement
// parse() Method with Custom Pattern
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
  
// Driver Class
public class Temp {
    // Main Function
    public static void main(String[] args)
    {
        // Example String representing a date
        String dateString = "29-Mar-2019";
  
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
        LocalDate localDate = LocalDate.parse(dateString, formatter);
  
        // Output the type of the dateString variable
        System.out.println("Type of dateString: " + dateString.getClass().getName());
  
        System.out.println("Type of localDate: " + localDate.getClass().getName());
  
          // Output the LocalDate
        System.out.println("Parsed LocalDate: " + localDate);
    }
}


Output

Type of dateString: java.lang.String
Type of localDate: java.time.LocalDate
Parsed LocalDate: 2019-03-29

3. Local-Specific Pattern

Date Pattern has been a topic of discussion always as Date across the globe is not same it varies from country to country. So, with this method we can define the Locality of the pattern in which we want to display the Date.

Java




// Java Program to implement
// parse() Method with Custom Pattern
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
  
// Driver Class
public class Temp {
    // Main Function
    public static void main(String[] args)
    {
        // Example String representing a date
        String dateString = "19-mai-2022";
  
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy").withLocale(Locale.FRENCH);
        LocalDate localDate = LocalDate.parse(dateString, formatter);
  
        // Output the type of the dateString variable
        System.out.println("Type of dateString: " + dateString.getClass().getName());
  
        System.out.println("Type of localDate: " + localDate.getClass().getName());
  
          // Output the LocalDate
        System.out.println("Parsed LocalDate: " + localDate);
    }
}


Output

Type of dateString: java.lang.String
Type of localDate: java.time.LocalDate
Parsed LocalDate: 2022-05-19



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads