Java String to Date

Last Updated : 8 Jan 2026

In Java, a string variable can be represented in the format of date. The task is to translate or change the String variable into date format. Let's understand in detail the various methods and approaches used for the conversion of a String variable into a date.

Example 1:

Input: String s = "2020-03-28T10:15:01Z"

Output: 2020-03-28T10:15:01Z

Explanation: The value stored in the string s variable is converted into a date format.

Example 2:

Input: String s = "2024-02-06T15:23:01Z"

Output: 2024-02-06T15:23:01Z

Methods to Convert String into Date

There are various methods and techniques that can be utilized for the conversion of a String value to a date format are as follows. Let us understand about them in detail.

  1. Using SimpleDateFormat Classs
  2. Using Instant Class
  3. Using DateTimeFormatter Class
  4. Using SimpleDateFormat Class
Java Convert String to Date

Using SimpleDateFormat.parse() Method

Let's see the simple code to convert String to Date in Java.

StringToDateExample1.java

Output:

31/12/1998	Thu Dec 31 00:00:00 IST 1998

Let's see another code to convert different types of Strings to Date in Java. Here, we have used different date formats using SimpleDateFormat class.

StringToDateExample2.java

Output:

31/12/1998	Thu Dec 31 00:00:00 IST 1998
31-Dec-1998	Thu Dec 31 00:00:00 IST 1998
12 31, 1998	Thu Dec 31 00:00:00 IST 1998
Thu, Dec 31 1998	Thu Dec 31 00:00:00 IST 1998
Thu, Dec 31 1998 23:37:50	Thu Dec 31 23:37:50 IST 1998
31-Dec-1998 23:37:50	Thu Dec 31 23:37:50 IST 1998

Using Instant Class

In Java, the java.time package contains the Instant class which provides the users with an accuracy in nanoseconds. The accuracy provided by the Instant class is better when compared with that of Date class.

The approach or algorithm of this method is very simple. The string that needs to be converted must be taken as input from user or assigned value. Then an empty object of Instant timestamp must be created. Upon successful creation of the timestamp object, the string will be converted to Date with the help of Instant.parse() method of Java. Upon unsuccessful creation, a DateTimeParseException will be thrown.

Let's understand about this approach in detail with the help of an example Java program.

Filename: StringToDate.java

Output:

Date Converted From String: 2020-03-28T10:15:01Z

It was the procedure or method for the conversion of a String value to a Date format. Let us look at another methods or approaches for converting String to Date.

Using DateTimeFormatter Class

In this approach or technique, the DateTimeFormatter class will be utilized for the conversion of a String into a Date format. The approach or algorithm for this method is mentioned below.

Fetch the string that needs to be converted into a date either by user or assign the value manually. An empty LocalDate object must be created. The String must then be converted into date format utilizing the LocalDate.parse() method. Upon successful conversion, display the resultant date. An IllegalArgumentException might be thrown if the input string is not valid.

Let's understand about this approach in detail with the help of a Java example program.

Filename: StringToDate1.java

Output:

2002-09-26

Using SimpleDateFormat Class

In this approach or technique, the SimpleDateFormat class will be utilized for the conversion of a string into a date format. Let's understand in detail about the algorithm or approach.

The input string must be declared and assigned with value. An object of the Date class must be created which refers to the SimpleDateFormat class. Then the date format must be parsed into it. Then the final result must be displayed . Let's understand about this approach in detail with the help of a Java example program.

Filename: StringToDate2.java

Output:

Random Date String: 15/09/1996
Parsed Date: Sun Sep 15 00:00:00 IST 1996
Age: 27 years

Random Date String: 01/11/1950
Parsed Date: Wed Nov 01 00:00:00 IST 1950
Age: 73 years

Random Date String: 20/11/1955
Parsed Date: Sun Nov 20 00:00:00 IST 1955
Age: 68 years

Random Date String: 09/07/1927
Parsed Date: Sat Jul 09 00:00:00 IST 1927
Age: 96 years

Random Date String: 27/08/1913
Parsed Date: Wed Aug 27 00:00:00 IST 1913
Age: 110 years