Convert String DateTime To LocalDateTime in Java

Sometimes we may need to convert a String DateTime to a LocalDateTime in a Java program because it allows us to manipulate and use the date and time values in our code.

When we get a date/time value as a String, it is essentially just a sequence of characters. We cannot perform any meaningful operations on it until we convert it to a data type that Java can understand, like LocalDateTime.

LocalDateTime allows us to perform various date/time operations such as formatting, comparing, and arithmetic operations. It also enables us to handle time zones and daylight saving time changes, making it a versatile and reliable data type for working with dates and times in Java applications.

The following code demonstrates how to convert a String datetime to LocalDateTime in Java:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Example {

   public static void main(String[] args) {
      // Sample String datetime
      String strDatetime = "2021-05-10 11:50:10";

      // Creating DateTimeFormatter object for parsing datetime
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

      // Converting String datetime to LocalDateTime
      LocalDateTime localDateTime = LocalDateTime.parse(strDatetime, formatter);

      // Printing localDateTime
      System.out.println("String to LocalDateTime = " + localDateTime);
   }

}

The output of the above code is as follows:

String to LocalDateTime = 2021-05-10T11:50:10


Here's another example, with "Z" symbol in the datetime format. The "Z" symbol represents the timezone offset "Zulu time", which is also known as Coordinated Universal Time (UTC). When "Z" is used in a date-time string, it means that the date and time are represented in UTC timezone:

For example, the date-time string "2023-04-03T08:30:00Z" represents April 3rd, 2023, at 8:30:00 AM UTC time. The "Z" at the end indicates that the time is in the UTC timezone.

It's worth noting that if the letter "Z" is not included in a date-time string, it does not necessarily mean that the time is in the local timezone. The absence of a timezone offset symbol means that the date and time are being represented without a specific timezone, and it's up to the application to determine the timezone in which the date and time should be interpreted.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Example {

   public static void main(String[] args) {
      // Sample String datetime
      String strDatetime = "2023-05-29T17:34:15Z";

      // Creating DateTimeFormatter object for parsing datetime
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");

      // Converting String datetime to LocalDateTime
      LocalDateTime localDateTime = LocalDateTime.parse(strDatetime, formatter);

      // Printing localDateTime
      System.out.println("String to LocalDateTime = " + localDateTime);
   }

}

The output of the above code is as follows:

String to LocalDateTime = 2023-05-29T17:34:15


Here's the third example for converting string datetime with AM/PM format to LocalDateTime:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Example {

   public static void main(String[] args) {
      // Sample String datetime
      String strDatetime = "05/19/2023 12:45:11 AM";

      // Creating DateTimeFormatter object for parsing datetime
      DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy h:mm:ss a");

      // Converting String datetime to LocalDateTime
      LocalDateTime localDateTime = LocalDateTime.parse(strDatetime, dateTimeFormatter);

      // Printing localDateTime
      System.out.println("String to LocalDateTime = " + localDateTime);
   }

}

The output of the above code is as follows:

String to LocalDateTime = 2023-05-19T00:45:11