Convert String to Date In Java – Here we cover the different ways to convert string to date in Java. Out of all those at first we would like to go for SimpleDateFormat.
For Example:
Let us assume we have a String “5/10/2009” that holds a Date.
In American format we can understand this as May-10-2009. Suppose we are working with a British/Indian/etc date format where day is written first, month next and then year.
In that case our string date should represent 5th oct 2009. So, we should specify in which format the system should interpret the given string.
That format is specified using SimpleDateFormat(). If we specify “MM/dd/yyyy”, it will indicate 5th month, 10th day and 2009 year. If we specify “dd/MM/yyyy”, it will indicate 5th day, 10th month and 2009 year.
Example:
1 |
DateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy"); |
Once the format is specified, we can use the parse() method to actually travel through the string and create a Date object.
Example:
1 |
Date date=dateFormat.parse("5/10/2009"); |
Once the date object is created, we can use it in any way. We have a simple program below to show how this is done.
Example Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util .*; import java.text.*; class Check { public static void main(String arg[]) throws ParseException { DateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy"); Date date=dateFormat.parse("5/10/2009"); System.out.println(date); } } |
1 |
Mon Oct 05 00:00:00 IST 2009 |
The classes DateFormat and SimpleDateFormat are in the package java.text.
The Date class is in the package java.util.
Similarly for the statements like,
- DateFormat dateFormat=new SimpleDateFormat(“dd/MM/yyyy”);
- Date date=dateFormat.parse(“13/10/2009”);
- System.out.println(date);
Output will be like: Tue Oct 13 00:00:00 IST 2009
If we want to specify time along with date, then we can specify the format and string like,
- DateFormat dateFormat=new SimpleDateFormat(“dd/MM/yyyy/hh/mm/ss”);
- Date date=dateFormat.parse(“13/10/2009/17/55/20”);
- System.out.println(date);
Output will be like: Tue Oct 13 17:55:20 IST 2009
Based on the above examples, it is obvious that the code:
- DateFormat dateFormat=new SimpleDateFormat(“yyyy-MM-dd-hh-mm”);
- Date date=dateFormat.parse(“2009-10-13-5-45”);
- System.out.println(date);
Will output like: Tue Oct 13 05:45:00 IST 2009
The parse() methods throws a ParseException if the specified string does not match with the specified format. So it should be handled properly. A code like the following will throw ParseException as the symbols – and / are not matched.
- DateFormat dateFormat=new SimpleDateFormat(“MM/dd/yyyy”);
- Date date=dateFormat.parse(“13-10-2009”);
Suppose we have a date-string like “13 october, 2009”, then the argument in SimpleDateFormat() constructor should be like “dd MMMM, yyyy”.
- For date-string like “2-3-16, 22:50:34”, format can be “d-M-y, h:m:s”.
- For a date-string like “Tuesday, 30-Aug-16”, format can be “E, dd-MMM-y”.
- For a date-string like “16-8-30 10:55 PM”, format can be “y-M-d hh:mm a”. (Tue Aug 30 22:55:00 IST 2016)
In the argument to SimpleDateFormat(), we have used MM (in uppercase) and the MM represents month, had we used mm (in lowercase), then it represents minutes. So a proper understanding of these pattern letters is very much useful.
- y-year
- M-month (like 3, 7, etc)
- MMM-month (like January, May, etc)
- d-day in month
- h-hour
- m-minute in hour
- s-second in minute
- E-day name in week (like Sunday, Monday, etc)