How to Convert String to Date in Java
How to Convert String to Date in java
You can convert String to Date in Java by using the parse() method. There are two classes which have parse() method namely, DateFormat and SimpleDateFormat classes. Both classes belong to java.text package. The SimpleDateFormat class is used to both parse and format dates according to the formatting pattern specified by you.
You can create a SimpleDateFormat instance like this:
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
The pattern parameter passed to the constructor of the SimpleDateFormat class. The pattern is used for formatting and parsing of dates.
Remember
There is a little bit confusion between “m” and “M”, “m” represents a minutes while “M” represents the Month. Similarly “d” represents a date in the month while “D” represents Day of the week. This is the most common cause of error while converting String to Date. In short, ddMMyy is not equal to DDmmyy.
Example
In this example, Datestr is a variable of String type contains a string “23/05/2019”. The date is a variable of type Date stores the converted value of Datestr. A new is a keyword that creates the constructor of the SimpleDateFormat class and parses the date format as an argument. parse() is a method of SimpleDateFormat class and parses Datestr as an argument which is to be converted into Date. The println statement prints the converted value of Datestr.
The parse() method throws an exception java.text.ParseException, to remove this exception you should use either try catch block or simply write throws Exception after the main method.
import java.text.SimpleDateFormat; import java.util.Date; public class StringtoDateExample { public static void main(String[] args)throws Exception { String Datestr="23/05/2019"; Date date=new SimpleDateFormat("dd/MM/yyyy").parse(Datestr); System.out.println(date); } }
Output
Thu May 23 00:00:00 IST 2019
Let’s see another example in which different date format is used.
Example
import java.text.SimpleDateFormat; import java.util.Date; public class StringToDateExample2 { public static void main(String[] args)throws Exception { String Datefrmt1="31/12/1998"; String Datefrmt2= "23-May-2019"; String Datefrmt3= "05 23, 2019"; String Datefrmt4= "Thu, May 23 2019"; String Datefrmt5 = "Thu, May 23 2019 21:31:20"; String Datefrmt6 = "23-May-2019 21:31:20"; 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(Datefrmt1); Date date2=formatter2.parse(Datefrmt2); Date date3=formatter3.parse(Datefrmt3); Date date4=formatter4.parse(Datefrmt4); Date date5=formatter5.parse(Datefrmt5); Date date6=formatter6.parse(Datefrmt6); System.out.println(Datefrmt1+"\t"+date1); System.out.println(Datefrmt2+"\t"+date2); System.out.println(Datefrmt3+"\t"+date3); System.out.println(Datefrmt4+"\t"+date4); System.out.println(Datefrmt5+"\t"+date5); System.out.println(Datefrmt6+"\t"+date6); } }
Output
23/05/2019 Thu May 23 00:00:00 IST 2019 23-May-2019 Thu May 23 00:00:00 IST 2019 05 23, 2019 Thu May 23 00:00:00 IST 2019 Thu, May 23 2019 Thu May 23 00:00:00 IST 2019 Thu, May 23 2019 21:31:20 Thu May 23 21:31:20 IST 2019 23-May-2019 21:31:20 Thu May 23 21:31:20 IST 2019