In Java, a string variable can be represented in date format. The task is to convert the String variable into date format.
Example 1:
Input: String s = "2026-05-22T11:16:01Z"
Output: 2026-05-22T11:16:01Z
Example 2:
Input: String s = "2026-03-02T13:21:05Z"
Output: 2026-03-02T13:21:05Z
In the above examples, the date and time value stored in the String variable s is converted into a date representation.
There are the following methods and techniques that can be used to convert of a String value to a date format.

The SimpleDateFormat class provides the parse() method that converts a date string into a Date object. It uses a specified date and time pattern to interpret the string and requires the input string to match that pattern.
Example
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
String sDate1="01/10/2026";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
System.out.println(sDate1+"\t"+date1);
}
}
Output:
01/10/2026 Thu Oct 01 00:00:00 UTC 2026
Let's see another program that converts different types of Strings into Date format. Here, we have used different date formats using the SimpleDateFormat class.
Example
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args)throws Exception {
String sDate1="12/05/2026";
String sDate2 = "23-Oct-2026";
String sDate3 = "09 11, 2026";
String sDate4 = "Thu, Nov 12 2026";
String sDate5 = "Tue, Sep 15 2026 23:37:50";
String sDate6 = "10-Jan-2026 23:37:50";
SimpleDateFormat formatter1=new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatter2=new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat formatter3=new SimpleDateFormat("MM dd, yyyy");
SimpleDateFormat formatter4=new SimpleDateFormat("E, MMM dd yyyy");
SimpleDateFormat formatter5=new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
SimpleDateFormat formatter6=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date date1=formatter1.parse(sDate1);
Date date2=formatter2.parse(sDate2);
Date date3=formatter3.parse(sDate3);
Date date4=formatter4.parse(sDate4);
Date date5=formatter5.parse(sDate5);
Date date6=formatter6.parse(sDate6);
System.out.println(sDate1+"\t"+date1);
System.out.println(sDate2+"\t"+date2);
System.out.println(sDate3+"\t"+date3);
System.out.println(sDate4+"\t"+date4);
System.out.println(sDate5+"\t"+date5);
System.out.println(sDate6+"\t"+date6);
}
}
Output:
12/05/2026 Tue May 12 00:00:00 UTC 2026 23-Oct-2026 Fri Oct 23 00:00:00 UTC 2026 09 11, 2026 Fri Sep 11 00:00:00 UTC 2026 Thu, Nov 12 2026 Thu Nov 12 00:00:00 UTC 2026 Tue, Sep 15 2026 23:37:50 Tue Sep 15 23:37:50 UTC 2026 10-Jan-2026 23:37:50 Sat Jan 10 23:37:50 UTC 2026
Instant class is also used to convert the string into date format. It belongs to java.time package. It 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 gets converted into Date with the help of Instant.parse(). On unsuccessful creation, a DateTimeParseException will be thrown.
Example
import java.time.Instant;
import java.time.format.DateTimeParseException;
public class Main {
// Method utilized for the conversion of String variable to Date format.
public static Instant fetchDate(String s) {
// Creation of an Instant object
Instant TimeStamp = null;
// Parsing through the string to Date
TimeStamp = Instant.parse(s);
// Returning the converted TimeStamp with Date format.
return TimeStamp;
}
public static void main(String[] args) {
// Input the string value
String s = "2026-07-08T10:15:01Z";
// Trying to block for checking for runtime exceptions
try {
// Fetching the Date from String by
// Creation of an object of the Instant class
Instant TimeStamp = fetchDate(s);
// Displaying the successfully converted date
System.out.println("Date Converted from String: " + TimeStamp);
} catch (DateTimeParseException e) {
// Throws DateTimeParseException
// Only if the string cannot be parsed
System.out.println("Exception occurred while parsing: " + e.getMessage());
} catch (Exception e) {
// Handles any other runtime exceptions
System.out.println("An unexpected error occurred: " + e.getMessage());
}
}
}
Output:
Date Converted from String: 2026-07-08T10: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.
For converting the string into date format, an empty LocalDate object must be created. After that the String must converted into date format by using the LocalDate.parse() method. Upon successful conversion, display the resultant date. The method throws an IllegalArgumentException if the input string is not valid.
Example
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class Main {
public static LocalDate fetchDateFromString(String s, DateTimeFormatter format) {
LocalDate d = LocalDate.parse(s, format);
// Returning the converted d at the end
return d;
}
public static void main(String[] args) {
String s = "26 September, 2026";
// Getting the format by creating an object of DateTImeFormatter class
DateTimeFormatter format = DateTimeFormatter.ofPattern("d MMMM, yyyy");
// Try block to check for exceptions
try {
// Getting the Date from String
LocalDate d = fetchDateFromString(s, format);
// Printing the converted d
System.out.println(d);
}
// Block 1
// Catch block to handle exceptions occurring
// if the String pattern is invalid
catch (IllegalArgumentException e) {
// Display the exception
System.out.println("Exception: " + e);
}
// Block 2
// If the String was unable to be parsed
catch (DateTimeParseException e) {
// Display the exception
System.out.println("Exception: " + e);
}
}
}
Output:
2026-09-26
In this method, an object of the Date class must be created which refers to the SimpleDateFormat class. After that the date format must be parsed into it. Then the final result must be displayed.
Example
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class Main {
// Method utilized for generating a random date string
public static String generateRandomDateString() {
Random random = new Random();
int d = random.nextInt(20) + 1; // Random d between 1 and 20
int m = random.nextInt(10) + 1; // Random m between 1 and 10
int y = random.nextInt(100) + 1900; // Random y between 1900 and 1999
return String.format("%02d/%02d/%04d", d, m, y); // Format the date string
}
// Method utilized for calculating age based on parsed date
public static int calculateAge(Date birthDate) {
Date cd = new Date();
long dIMillis = Math.abs(cd.getTime() - birthDate.getTime());
long Diff = dIMillis / (24 * 60 * 60 * 1000 * 365L);
return (int) Diff;
}
public static void main(String[] args) {
try {
for (int i = 0; i < 5; i++) { // Generate and process 5 random dates
// Generate a random date string
String randomDateStr = generateRandomDateString();
// Print the generated random date string
System.out.println("Random Date String: " + randomDateStr);
// Creating an object of Date class with reference
// to SimpleDateFormat class and
// parsing the above string into it
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(randomDateStr); // Print and display the date corresponding
// to the generated random date string
System.out.println("Parsed Date: " + date);
// Calculate and print age based on parsed date
int age = calculateAge(date);
System.out.println("Age: " + age + " years\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Random Date String: 03/02/1951 Parsed Date: Sat Feb 03 00:00:00 UTC 1951 Age: 75 years Random Date String: 07/09/1939 Parsed Date: Thu Sep 07 00:00:00 UTC 1939 Age: 87 years Random Date String: 06/07/1994 Parsed Date: Wed Jul 06 00:00:00 UTC 1994 Age: 32 years Random Date String: 17/07/1913 Parsed Date: Thu Jul 17 00:00:00 UTC 1913 Age: 113 years Random Date String: 17/05/1959 Parsed Date: Sun May 17 00:00:00 UTC 1959 Age: 67 years
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India