Different methods to convert String to Date in Java
In order to convert a String to Date in java, the first requirement is that the String should be in a standard Date format. In real time applications it is very common to store the dates in text format and use it later as a Date while inserting on the database. So, by storing Date as an string can save lots of space in the program and finally at the time of insertion it can be converted to Date using any of the approaches listed below.
There are five ways to convert a String to Date in java as listed below.
- Using
SimpleDateFormat
Class - Using
LocalDate
Class - Using
DateTimeFormater
Class - Using Instant Class
- Using Apache Commons Lang - DateUtils
Method-1: Using SimpleDateFormat Class
This is the simplest approach to convert the String to Date in Java. The SimpleDateFormat
is a class of java.text
module. The list below shows some of the standard date patterns used in SimpleDateFormat
Class. This is a traditional approach where we are using java.util.Date
class.
- y - Year (2022,22)
- M - Month(June, Jun, 07)
- d - Day in Month
- D - Day in Year
Example : In this example, we are converting strings of different format to the equivalent date using the patterns listed.
// Program to convert String to date in Java
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
// Initializing the string
String s1 = "31/01/2022";
// Converting to Date and printing
Date d = new SimpleDateFormat("dd/MM/yyyy").parse(s1);
System.out.println("String is " + s1 + "\t Its equivalent Date is " + d);
// Initializing the string
s1 = "01/31/2022";
// Converting to Date and printing
d = new SimpleDateFormat("MM/dd/yyyy").parse(s1);
System.out.println("String is " + s1 + "\t Its equivalent Date is " + d);
// Initializing the string
s1 = "Jan 31,2022";
// Converting to Date and printing
d = new SimpleDateFormat("MMM d,yyyy").parse(s1);
System.out.println("String is " + s1 + "\t Its equivalent Date is " + d);
}
}
Output
String is 31/01/2022 Its equivalent Date is Mon Jan 31 00:00:00 GMT 2022
String is 01/31/2022 Its equivalent Date is Mon Jan 31 00:00:00 GMT 2022
String is Jan 31,2022 Its equivalent Date is Mon Jan 31 00:00:00 GMT 2022
Method-2: Using LocalDate Class
In this approach, we will use LocalDate class of java.time package to convert String to Date. This class is used when we want a date without a time-zone i.e. in the ISO-8601 calendar system. By default, Java dates are in this format. Moreover, if the String is invalid it will throw an exception.
Example : In this example, we are converting String given without time-zone into the equivalent date.
// Program to convert String to date in Java
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
// Initializing the string
String s1 = "2022-01-31";
// Converting to Date and printing
LocalDate d = LocalDate.parse(s1);
System.out.println("String is " + s1 + "\t Its equivalent Date is " + d);
}
}
Output
String is 2022-01-31 Its equivalent Date is 2022-01-31
Method-3: Using DateTimeFormater Class
In this approach, we are using DateTimeFormater
class of java.time.format
package. This class is mainly used with the other classes to format the date and time objects. For an invalid string, this will throw DateTimeParseException
.
Example : In this example, we are taking dates in various formats using the same pattern as it was in SimpleDateFormat
class.
// Program to convert String to date in Java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Initializing the string
String s1 = "Jan 31,2022";
// Converting to Date and printing
DateTimeFormatter f = DateTimeFormatter.ofPattern("MMM d,yyyy");
LocalDate d = LocalDate.parse(s1, f);
System.out.println("String is " + s1 + "\t Its equivalent Date is " + d);
// Initializing the string
s1 = "January 31,2022";
// Converting to Date and printing
f = DateTimeFormatter.ofPattern("MMMM d,yyyy");
d = LocalDate.parse(s1, f);
System.out.println("String is " + s1 + "\t Its equivalent Date is " + d);
// Initializing the string
s1 = "01/31/2022";
// Converting to Date and printing
f = DateTimeFormatter.ofPattern("MM/dd/yyyy");
d = LocalDate.parse(s1, f);
System.out.println("String is " + s1 + "\t Its equivalent Date is " + d);
}
}
Output
String is Jan 31,2022 Its equivalent Date is 2022-01-31
String is January 31,2022 Its equivalent Date is 2022-01-31
String is 01/31/2022 Its equivalent Date is 2022-01-31
Method-4: Using Instant Class
This approach is used in extremely sensitive application as it gives nanosecond accuracy in time. The Instant class models a single instantaneous point on the time-line. So, it can be used to record the event time-stamps in the applications. The parse() method is used to convert the date in the specific format. Moreover, if the string is not in the same format, it will throw DateTimeParseException exception.
Example : In this example, we are using the date in valid format and an invalid format to show that it will raise an exception for all the invalid formats.
// Program to convert String to Date
import java.time.Instant;
class Main {
public static void main(String[] args) {
// Initializing
String s1 = "2022-01-31T15:23:01Z";
Instant d = null;
// Parsing the string to Date
d = Instant.parse(s1);
// Printing the converted date
System.out.println("String is " + s1 + "\t Its equivalent Date is " + d);
// Initializing
s1 = "2022-01-31";
// Parsing the string to Date
d = Instant.parse(s1);
// Printing the converted date
System.out.println("String is " + s1 + "\t Its equivalent Date is " + d);
}
}
Output
String is 2022-01-31T15:23:01Z Its equivalent Date is 2022-01-31T15:23:01Z
Exception in thread "main"
java.time.format.DateTimeParseException: Text '2022-01-31' could not be parsed at index 10
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.Instant.parse(Instant.java:395)
at Main.main(Main.java:19)
Method-5: Apache Commons Lang - DateUtils
In this approach, we will need to download commons-lang3-3.12.0-bin.zip from the url mentioned in the reference section. Thereafter, we have to add this jar to our existing project folder using the below given steps.
- Right click your project folder in eclipse.
- Click on Build Path -> Add external archives.
- Now, go to the path where your zip folder is extracted.
- Select the jar (commons-lang3-3.12.0)from the extracted zip folder.
- You will see that jar added to Referenced Libraries
Example : In this example we are using DateUtils.parseDate() function that takes two parameters as a string and format.
import org.apache.commons.lang3.time.DateUtils;
import java.util.*;
public class temp {
public static void main(String[] args) throws Exception {
String dateInString = "01/31-2022";
Date date = DateUtils.parseDate(dateInString, "MM/dd-yyyy");
System.out.println(date);
}
}
Summary
The knowledge of Converting a String to a Date in Java is very useful while working on a real time applications. It’s important to note the date format which each method accepts. In this tutorial, we covered five different approaches to convert String to Date in Java. As per the requirement of an application, we can choose an appropriate approach for conversion. We learned in detail about this approaches with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on converting string to date in Java.
References
Date Class
SimpleDateFormat Class
LocalDate Class
DateTimeFormatter Class
Instant Class
DateUtils Class
Apache commons lang3 Downloads