Do you wanna learn how you can convert Date stored in String to Date format in Java ? Today you are going to learn the same. We can convert a String to Date in java using parse() method of Dateformat and SimpleDateFormat classes.
Java String to Date Conversion Example
Let’s see the simple code of String to Date conversion in java.
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
public static void main(String[] args)throws Exception {
String stringDate="01/01/2021";
SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy");
Date mydate = format.parse(stringDate);
System.out.println(stringDate+"\t"+mydate);
}
}
output
01/01/2021 Fri Jan 01 00:00:00 IST 2021
The java.text.SimpleDateFormat class has methods which are used for both to parse and format date according to a formatting pattern you want to use. The SimpleDateFormat parses the date pattern in java String format. The SimpleDateFormat formats the date into String and can also format the date into StringBuffer.
Let’s see example of SimpleDateFormat
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Formatting Dates
Once you have create SimpleDateFormat instance you can use the format() method to format the date. Let’s see an example
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
java.util.Date – Date in Java
The java.util.date class represents date and time in java. It provides methods and constructors to deal with date and time in java. Let’s see with example of using it with SimpleDateFormat class in java.
java.util.Date date=new java.util.Date();
System.out.println(date);
Output
Sat Jan 02 08:22:02 IST 2021
Formatting Pattern Syntax
Let’s see some symbols used in the formatting pattern to format date
G Era designator (before christ, after christ)
y Year (e.g. 12 or 2012). Use either yy or yyyy.
M Month, Number of M's determine length of format(e.g.MM, MMM or MMMMM)
d Day in month. Number of d's determine length of format (e.g. d or dd)
h Hour of day, 1-12 (AM / PM) (normally hh)
H Hour of day, 0-23 (normally HH)
m Minute in hour, 0-59 (normally mm)
s Second in minute, 0-59 (normally ss)
S Millisecond in second, 0-999 (normally SSS)
E Day in week (e.g Monday, Tuesday etc.)
D Day in year (1-366)
F Day of week in month (e.g. 1st Thursday of December)
w Week in year (1-53)
W Week in month (0-5)
a AM / PM marker
k Hour in day (1-24, unlike HH's 0-23)
K Hour in day, AM / PM (0-11)
z Time Zone
' Escape for text delimiter
' Single quote
Pattern Example for SimpleDateFormat
Let’s see few SimpleDateFormat pattern examples :
Pattern Example
dd-MM-yy 31-01-12
dd-MM-yyyy 31-01-2012
MM-dd-yyyy 01-31-2012
yyyy-MM-dd 2012-01-31
yyyy-MM-dd HH:mm:ss 2012-01-31 23:59:59
yyyy-MM-dd HH:mm:ss.SSS 2012-01-31 23:59:59.999
yyyy-MM-dd HH:mm:ss.SSSZ 2012-01-31 23:59:59.999+0100
EEEEE MMMMM yyyy HH:mm:ss.SSSZ Saturday November 2012 10:45:42.720+0100
Now, Lets see code to convert different Strings to Date in java. Here, you will learn to use different date format patterns.
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
public static void main(String[] args)throws Exception {
String sDate1 = "31/12/1998";
String sDate2 = "31-Dec-1998";
String sDate3 = "12 31, 1998";
String sDate4 = "Thu, Dec 31 1998";
String sDate5 = "Thu, Dec 31 1998 23:37:50";
String sDate6 = "31-Dec-1998 23:37:50";
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat format3 = new SimpleDateFormat("MM dd, yyyy");
SimpleDateFormat format4 = new SimpleDateFormat("E, MMM dd yyyy");
SimpleDateFormat format5 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
SimpleDateFormat format6 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date date1=format1.parse(sDate1);
Date date2=format2.parse(sDate2);
Date date3=format3.parse(sDate3);
Date date4=format4.parse(sDate4);
Date date5=format5.parse(sDate5);
Date date6=format6.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
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