Horje
How to Convert a String to a LocalDate in Java?

Converting a String to a LocalDate in Java is a common operation when you are dealing with date inputs from users. Java provides the LocalDate class in the java.time package for representing a date without time information. LocalDate class is part of the java.time package introduced in Java 8.

String to a LocalDate Conversion in Java

LocalDate.parse() method of the java.time.LocalDate class to convert a String representation of a date into a LocalDate object. The parse() method will take the date string and a DateTimeFormatter (yyyy-MM-dd) as parameters.

Syntax to Define an Input Format

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

Syntax to Convert String Date into LocalDate

LocalDate localDate = LocalDate.parse(dateString, formatter);

Program to Convert String to LocalDate in Java

An example demonstrates how to convert String data into LocalDate:

Java

// Java Program to Convert
// String to LocalDate
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
 
// Driver Class
public class GFG {
    // main function
    public static void main(String[] args)
    {
        try {
            // Input String Date
            String dateString = "2024-01-20";
 
            // Define a DateTimeFormatter for the input
            // format
            DateTimeFormatter formatter
                = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 
            // Convert user input to LocalDate
            LocalDate localDate
                = LocalDate.parse(dateString, formatter);
 
            // Print the resulting LocalDate
            System.out.println("Converted LocalDate: "
                               + localDate);
        }
        catch (Exception e) {
            System.out.println("Error parsing the date: "
                               + e.getMessage());
        }
    }
}

Output

Converted LocalDate: 2024-01-20





Explanation of the above Program:

  • First, We have imported the necessary classes: LocalDate, DateTimeFormatter.
  • Then inside the try-catch block, We have defined a String variable dateString that value is in yyyy-MM-dd format.
  • We have used DateTimeFormatter.ofPattern() method to define format of our input dateString.
  • By using a LocalDate.parse() method, we are converting date string into a LocalDate.
  • After converting, we are printing the output.
  • If an exception occurs during parsing, we catch it and print an error message.



Reffered: https://www.geeksforgeeks.org


Java

Related
Get Two Different Objects Into a TreeSet in Java Get Two Different Objects Into a TreeSet in Java
How to Use Deque as a Stack in Java? How to Use Deque as a Stack in Java?
Find the Intersection of Two HashSets in Java Find the Intersection of Two HashSets in Java
Convert String to LocalDate in Java Convert String to LocalDate in Java
How to Convert a Java String Against a Pattern Regex? How to Convert a Java String Against a Pattern Regex?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12