-->
Skip to main content

LocalDateTime parse() Method Example in Java [2021]

LocalDateTime parse() Method Example in Java [2021] 

In this tutorial, we will see how to use the LocalDateTime parse() method to convert String to LocalDateTIme instance in Java. In the earlier tutorial, we have seen LocalDateTime to String Conversion In Java

LocalDateTime parse() method

There are two parse() methods present in LocalDateTime class to convert text string to LocalDateTime instance.
  1. public static LocalDateTime parse(CharSequence text)
  2. public static LocalDateTime parse(CharSequence text,DateTimeFormatter formatter)
We will look into both the methods in detail one by one.

LocalDateTime parse() : Convert String to LocalDateTime

1. public static LocalDateTime parse(CharSequence text)

  • This method returns LocalDateTime instance from a text string such as 2020-07-03T08:10:48.
  • The string should have valid date-time format and is parsed using DateTimeFormatter.ISO_LOCAL_DATE_TIME.

Parameters:

text - The text to which needs to be parsed such as "2020-07-03T08:10:48" which is not null.

Returns:

the parsed local date-time which is not null.

Throws:

DateTimeParseException is thrown if the text cannot be parsed.

Example:

 import java.time.LocalDateTime;

 public class LocalDateTimeParseExample1 {
   public static void main(String[] args) {

     // Using parse method with string as input,
     // Gives an instance of LocalDateTime from a text string such as
     // 2020-07-03T10:15:30.
     LocalDateTime dateTimeUsingParseMethod = LocalDateTime.parse("2020-07-03T10:15:30");
     System.out.println("Getting LocalDateTime instance parse method : " + dateTimeUsingParseMethod);
   }
 }

Output:

Getting LocalDateTime instance parse method : 2020-07-03T10:15:30

2. public static LocalDateTime parse(CharSequence text,DateTimeFormatter formatter)

  • This method returns LocalDateTime instance from a text string using a specified formatter provided by the user.
  • The text string is parsed using the formatter specified and it will return date-time.

Parameters:

text - The text string to parse which is not null.
formatter - the formatter to use which is not null.

Returns:

the parsed local date-time instance which is not null.

Throws:

DateTimeParseException is thrown if the text cannot be parsed.

Example 2.1:

This example shows formatters that are user-defined and library defined.
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;

 public class LocalDateTimeParseExample2 {
   public static void main(String[] args) {

     //Using library defined pattern.
     LocalDateTime dateTime1 = LocalDateTime.parse("2020-06-26T21:51:22", DateTimeFormatter.ISO_DATE_TIME);
     System.out.println("With ISO_DATE_TIME Pattern : "+ dateTime1);

     //Using yyyy-MM-dd HH:mm:ss.SSS pattern
     LocalDateTime dateTime2 = LocalDateTime.parse("2020-07-03 14:32:58.410",
     DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
     System.out.println("With yyyy-MM-dd HH:mm:ss.SSS Pattern : "+ dateTime2);
		
     //Using yyyy-MM-dd HH:mm pattern
     LocalDateTime dateTime3 = LocalDateTime.parse("2020-07-03 14:32",
     DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
     System.out.println("With yyyy-MM-dd HH:mm Pattern : "+ dateTime3);
   }
 }

Output:

With ISO_DATE_TIME Pattern : 2020-06-26T21:51:22
With yyyy-MM-dd HH:mm:ss.SSS Pattern : 2020-07-03T14:32:58.410
With yyyy-MM-dd HH:mm Pattern : 2020-07-03T14:32

Example 2.2:

This example shows the formatter that is provided by library and with Locale use, in some cases, we might want to change our text String to LocalDateTime instance with different Locale. In these kinds of scenarios, we have the option to provide Locale value while creating a DateTimeFormatter Instance.
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Locale;

 public class LocalDateTimeParseExample3 {
    public static void main(String[] args) {

      //Using Locale English 
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm", Locale.ENGLISH);
      LocalDateTime dateTime1 = LocalDateTime.parse("2020-Jul-03 14:32", formatter);
      System.out.println("Parsing with Locale : "+dateTime1);
    }
 }

Output:

Parsing with Locale : 2020-07-03T14:32

Conclusion:

In this tutorial, we have seen how to use the LocalDateTime parse() method to convert text String to LocalDateTime instance in Java.

Comments