Java

How to Convert a String to a DateTime Object in Java

While programming in Java, sometimes, you may need to perform String to date object conversion. Although there is no predefined method in Java for the specified purpose, still several methods can be utilized for converting a string to a DateTime object.

This blog will explain the procedures for String to a DateTime object conversion in Java.

How to Convert String to DateTime Object in Java?

For converting a String to a DateTime object in Java, you can use:

  • SimpleDateFormat class
  • LocalDate class
  • ZonedDateTime class

Let’s see how the mentioned classes help in converting a String to a DateTime object.

Method 1: Convert String to DateTime Object Using SimpleDateFormat Class

For converting a String to a DateTime object, you can use the Java “SimpleDateFormat” class. Using this class, a String can be parsed into the required DateTime object with the help of the “parse()” method.

Syntax
Here is the syntax of the parse() method of SimpleDateFormat class:

sf.parse("DateTime String");

The “sf” is the object of the SimpleDateFormat class that invokes the “parse()” method by passing a String in a DateTime format.

Example
First, we will create an instance of the SimpleDateFormat class and pass a date and time format as parameter:

SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yyyy;HH:mm:ss");

Use a try-catch block in which firstly you have to create an object of the “Date” class named “dateTime”. This object will store the date parsed as a String using the parse() method, and then prints the converted DateTime object with the “System.out.println()” method:

try {
      Date dateTime = sf.parse("19-08-2022; 01:34:23");
    System.out.println(dateTime);
} catch (ParseException e) {
    e.printStackTrace();
}

The given output indicates that the String is successfully converted into a DateTime object:

Now, let’s see how the LocalDate class works for converting a String to a DateTime object.

Method 2: Convert String to DateTime Object Using LocalDate Class

Another popular DateTime class in Java is “LocalDateTime”. The format of its object comprises “T”, which represents “Time” and acts as a point of separation between date and time.

Syntax
The below-given syntax can be followed to use the parse() method of the LocalDate class:

LocalDateTime.parse("DateTime String");

Here, the LocalDate class calls the “parse()” method by passing a DateTime String to convert it into a DateTime Object.

Example
We will first create an object of the LocalDateTime class named “dateTime” and parse the specified String argument with the help of the “parse()” method:

LocalDateTime dateTime = LocalDateTime.parse("2022-08-19T02:30:45");

Finally, print the resultant “dateTime” object on the console:

System.out.println(dateTime);

Output

Method 3: Convert String to DateTime Object Using ZonedDateTime Class

Sometimes, we need time zone information with the DateTime. For this purpose, Java supports a class called “ZonedDateTime” that fetches the current time zones while working with date and time. This class also utilizes the “parse()” method with the ZonedDateTime Class to parse a string and convert it to a DateTime object.

Syntax
To use the ZonedDateTime class, follow the given syntax:

ZonedDateTime.parse("DateTime String");

Here, the ZonedDateTime class invokes the “parse()” method by passing a String to be converted into a DateTime Object.

Example
Firstly, we will create an object of the ZonedDateTime class named “zone” and call the “parse()” method by passing a DateTime String in it as an argument. The specified string represents the date and time of the “America” time zone:

ZonedDateTime zone = ZonedDateTime.parse
                 ("2022-08-19T02:56:45.513464300-05:00[America/Chicago]");

Print the converted DateTime object:

System.out.println(zone);

As you can see, the converted DateTime object is displayed with the time zone information:

We gathered all the necessary information for converting a String to a DateTime object in Java.

Conclusion

For converting a string to a DateTime object, several ways in Java are used, such as the SimpleDateFormat class, LocalDate class, and ZonedDateTime class. These classes are a part of the Java.time and Java.util packages. For converting a String using these classes, execute the “parse()” method by passing a String as an argument. This blog explained the procedures to convert a String to a DateTime object in Java with proper examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.