Convert String to Date in Java



In this article, we will learn to convert string to date in Java. In Java, dates are provided as strings, and it becomes necessary to convert these strings into Date objects for further processing.

Problem Statement

The goal is to convert the date strings in various formats into Date or LocalDateTime objects.

Input ?

String date1 ="11/10/2018";
String date2 = "15-Mar-2019 21:11:35";

Output ?

Thu Oct 11 00:00:00 UTC 2018
Fri Mar 15 21:11:35 UTC 2019

Different Approaches

The following are the two different approaches to converting string to date in Java ?

Using SimpleDateFormat

The SimpleDateFormat class is part of the java.text package and allows you to format and parse dates in a locale-sensitive manner. 

Converting the string to dates ?

SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date formattedDate1 = dateFormat1.parse(date1);
Date formattedDate2 = dateFormat2.parse(date2);

Example

Below is an example of converting String to Date in Java using the SimpleDateFormat class ?

import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo {
   public static void main(String[] args)throws Exception {
      String date1 ="11/10/2018";
      String date2 = "15-Mar-2019 21:11:35";
      SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
      SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
      Date formattedDate1 = dateFormat1.parse(date1);
      Date formattedDate2 = dateFormat2.parse(date2);
      System.out.println(formattedDate1);
      System.out.println(formattedDate2);
   }
}

Output

Thu Oct 11 00:00:00 UTC 2018
Fri Mar 15 21:11:35 UTC 2019

Time Complexity: Parsing a date string using SimpleDateFormat has a time complexity of O(n).
Space Complexity: The space complexity is O(1) for parsing a single date string.

Using DateTimeFormatter

Java 8 introduced a new Date and Time API in the java.time package, which provides a more modern and flexible way to work with dates and times. The DateTimeFormatter class is part of this API and can be used to parse and format dates.

Converting the string to dates ?

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss");

Example

Below is an example of converting String to Date in Java using the DateTimeFormatter class ?

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

public class Main {
   public static void main(String[] args) {
      String date1 = "11/10/2018";  
      String date2 = "15-Mar-2019 21:11:35";  

      DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
      DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss");

      LocalDate parsedDate1 = LocalDate.parse(date1, formatter1);
      
      LocalDateTime parsedDate2 = LocalDateTime.parse(date2, formatter2);

      System.out.println(parsedDate1);  
      System.out.println(parsedDate2); 
   }
}

Output

2018-10-11
2019-03-15T21:11:35

Time Complexity: parsing a date string using DateTimeFormatter also has a time complexity of O(n).

Space Complexity: The space complexity is O(1) for parsing a single date string.

Conclusion

Converting a string to a date in Java can be done using either the SimpleDateFormat class or the DateTimeFormatter class. While SimpleDateFormat is still widely used, the DateTimeFormatter class from the java.time package is recommended for new code due to its thread safety and modern API.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-02-21T17:32:24+05:30

690 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements