How to Convert String to DateTime Object in Java

Rupam Yadav Feb 02, 2024
  1. Use SimpleDateFormat and java.util.Date to Convert a Date String Into DateTime Object in Java
  2. Use Java 8 Date and Time Library to Convert a Date String Into Date Time Format
  3. Use joda-time to Convert a Date String Into Date Time Format
How to Convert String to DateTime Object in Java

This article will introduce how we can convert a given string into a DateTime Object using different approaches and examples.

Use SimpleDateFormat and java.util.Date to Convert a Date String Into DateTime Object in Java

SimpleDateFormat is a class used to parse and format a date in locale-sensitive-manner. It allows us to convert a string to DateTime and convert a DateTime object to a string.

In the below code, we use the string date_time, which is parsed by calling the parse method on the SimpleDateFormat instance dateParser. The format in which we want the string date_time to be parsed is specified inside the SimpleDateFormat constructor.

We have also created a new instance of the SimpleDateFormat class with a different format. The parsed date is then formatted and printed.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateTime {
  public static void main(String[] args) {
    String date_time = "11/27/2020 05:35:00";
    SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
    {
      try {
        Date date = dateParser.parse(date_time);
        System.out.println(date);

        SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yy");
        System.out.println(dateFormatter.format(date));

      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
  }
}

Output:

Fri Nov 27 05:35:00 UTC 2020
11/27/20

Use Java 8 Date and Time Library to Convert a Date String Into Date Time Format

The LocaleDate class represents a date in ISO format without time. The DateTimeFormatter class is used for parsing dates in different formats. We can provide custom patterns by calling ofPattern() method on DateTimeFormatter.

LocaleDate has a parse() method, which takes the custom text string and a specific formatter inputFormat to parse and obtain a LocalDate instance. The obtained LocaleDate instance date is then formatted and printed as output.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String [] args){
      
        DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
     
        LocalDate date = LocalDate.parse("11/27/2020 05:35:00", inputFormat);

        DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy");
        System.out.println(date.format(outputFormat));
    }
}

Output:

11/27/2020

Use joda-time to Convert a Date String Into Date Time Format

joda-time is a standard DateTime library that provides a rich set of methods to perform date and time calculations. The maven dependency to include the functionality of this library is given below.

 <!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.10.8</version>
    </dependency>

DateTimeFormatter can parse custom representations of date and time.

We create a formatter instance datetimeformat with a custom pattern. Calling parseDateTime on the formatter instance gives a DateTime object using the custom string dateTime.

Here we have created a new DateTimeFormatter instance dateTimeFormatOut with a different custom pattern. The print() method is called on the new instance which prints the DateTime object joda_time in a new format.

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class StringToDateTime {
  public static void main(String[] args) {
    String dateTime = "11/27/2020 05:35:00";
    DateTimeFormatter datetimeformat = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");

    DateTime joda_time = datetimeformat.parseDateTime(dateTime);
    System.out.println("joda_time : " + joda_time);

    DateTimeFormatter dateTimeFormatOut = DateTimeFormat.forPattern("MM/dd/yyyy");
    System.out.println("date time format out:  " + dateTimeFormatOut.print(joda_time));
  }
}

Output:

joda_time : 2020-11-27T05:35:00.000+05:30
date time format out:  11/27/2020
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java DateTime