How to Convert String To Date in Java
In this article, we will walk you through very simple and understandable ways of converting Strings to dates in Java. It is a very common practice in the software industry to store dates in plain text format and use it later for business logic. By storing dates in plain text we can save a lot of storage space.
Here is a very classic example of converting String to Date. Try this example on your own, it will help you to get the intuition behind this process of converting strings to date.
Example: Convert String to Date
import java.text.SimpleDateFormat;
import java.util.Date;
public class StudyTonight
{
public static void main(String args[]) throws Exception
{
String string_format="4/1/2021";
Date date_format=new SimpleDateFormat("dd/MM/yyyy").parse(string_format);
System.out.println(" Date in String Format: "+string_format+" \n Converted Date: "+date_format);
}
}
Date in String Format: 4/1/2021 Converted Date: Mon Jan 04 00:00:00 IST 2021
While executing the above example do not forget to use the throws keyword in the main method otherwise compile-time error will occur.
In the code, you might have noticed SimpleDateFormat("dd/MM/yyyy") method to set the format of a date. This means the mentioned date is in the format of “dd/MM/yyyy”. Java supports many formats to customize dates with a lot of flexibility. You can see the same table below.
All Available Formats of Date and Time in Java
Symbol
|
Symbol Represents
|
G
|
era
|
u
|
year
|
y
|
year-of-era
|
D
|
day-of-year
|
M/L
|
month-of-year
|
d
|
day-of-month
|
Q/q
|
quarter-of-year
|
Y
|
week-based-year
|
w
|
week-of-week-based-year
|
W
|
week-of-month
|
E
|
day-of-week
|
e/c
|
localized day-of-week
|
F
|
week-of-month
|
a
|
am-pm-of-day
|
h
|
clock-hour-of-am-pm (1-12)
|
K
|
hour-of-am-pm (0-11)
|
k
|
clock-hour-of-am-pm (1-24)
|
H
|
hour-of-day (0-23)
|
m
|
minute-of-hour
|
s
|
second-of-minute
|
S
|
fraction-of-second
|
A
|
milli-of-day
|
n
|
nano-of-second
|
N
|
nano-of-day
|
V
|
time-zone ID
|
z
|
time-zone name
|
O
|
localized zone-offset
|
X
|
zone-offset 'Z' for zero
|
x
|
zone-offset
|
Z
|
zone-offset
|
Example of Converting String to Date
In this example, we are converting the given date which is in string format to Date
by format using SimpleDateFormat
class. While converting strings to date makes sure that it is in the correct format otherwise it will throw an exception i.e. ParseException
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StudyTonight {
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String date_String = "4-Jan-2021";
try {
Date date = formatter.parse(date_String);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Mon Jan 04 00:00:00 IST 2021 04-Jan-2021
Now let's break the above code and understand line by line
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat is a class from java.text
package which sets the format of a date. In the above example "dd-MMM-yyyy" can be further simplified like this:
- dd - day of month
- MMM - month of year
- yyyy - year of era
So, this is the format of formatted which can be applied to any date later. For further clarifications, you can refer the table given above.
String date_String = "4-Jan-2021";
This is the simple date given in the string format.
Date date = formatter.parse(date_String);
Here, String type date is converted into the Date using formatter.parse()
method.
Here we are providing some sample examples for converting String to Date
Example 1
In this case, we are given a date in the format of "dd/MM/yyyy" and we want to convert that date to "dd/MM/yyyy" format of Date type using SimpleDateFormat()
we created a format for a date then by calling Format.parse()
method converted the string to the desired Date format.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StudyTonight
{
public static void main(String[] args)
{
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String date_String = "01/04/2021";
try
{
Date date = formatter.parse(date_String);
System.out.println(date);
System.out.println(formatter.format(date));
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
Thu Apr 01 00:00:00 IST 2021 01/04/2021
Example 2
In this case, we are given the date "Mon, Jan 4, 2021" and we want to convert that date to "E, MMM dd yyyy" format of Date type. Using SimpleDateFormat()
we created a format for a date then by calling Format.parse()
method converted the string to the desired Date format.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StudyTonight
{
public static void main(String[] args)
{
SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy");
String date_String = "Mon, Jan 4 2021";
try
{
Date date = formatter.parse(date_String);
System.out.println(date);
System.out.println(formatter.format(date));
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
Mon Jan 04 00:00:00 IST 2021 Mon, Jan 04 2021
Example 3
In this case, we are given the date "Monday, Jan 4, 2021, 12:15:30 PM" and we want to convert that date to "EEEE, MMM dd, yyyy HH:mm:ss a" format of Date type. Using SimpleDateFormat()
we created a format for a date then by calling Format.parse()
method converted the string to the desired Date format.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StudyTonight
{
public static void main(String[] args)
{
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
String date_String = "Monday, Jan 4, 2021 12:15:30 PM";
try
{
Date date = formatter.parse(date_String);
System.out.println(date);
System.out.println(formatter.format(date));
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
Mon Jan 04 12:15:30 IST 2021 Monday, Jan 04, 2021 12:15:30 PM
Conclusion:
We have different classes to change the date in a string format to the actual date and this provides us the ability to do different manipulations on a date later. All the valid dates in String format can be converted into Date
format using SimpleDateFormat
class by assigning the desired format. If the dates are in Date format then we can calculate the duration between two days, we can compare two dates which is not possible if date is represented in string format.