Written by nitinpai on March 28th, 2007
Convert Date to String and String to Date in Java
Java
5 responses
This is quite a common query which is faced by a Java Programmer while dealing with dates. The problem is when a form value returns back the date value input by the user in the form of a string. The application might be using a Date object to hold the date values in the application.
So in order to convert the String date value to Date object you might be tempted to use the simplified conversion format which is,
String str = "26/08/1994"; Date date = new Date(str);
But the problem with the above step is that the Date() constructor is now deprecated in JDK1.5 onwards and hence won’t be a good practice to use the deprecated ones.
So the solution for this is to use the SimpleDateFormat object is,
String str = "26/08/1994"; SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); //please notice the capital M Date date = formatter.parse(str);
The SimpleDateFormat object function parse() helps to parse the string value of the date to the desired value with the help of the customization syntax provided while instantiating the SimpleDateFormat object. The parsed values are then fed to the Date object instead of passing through the constructor as done earlier.
Convert Date back to String
To print back the date in the desired format we again require the same object i.e SimpleDateFormat to format according to our requirements. So to print we would do the following,
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); System.out.println("Date is : " + formatter.format(date)); //using the date object from earlier
Notice that here we use the format() function and not the parse() function since the format() function takes in a parameter of Date type while parse() takes in a String parameter.
Its easier to remember this way,
- String parse() to Date to use in the application
- Date format() to String to produce the display string

5 Responses
Wednesday, July 9, 2008
[...] [...]
Monday, August 4, 2008
i have done this but the format that i specify does not work. it always shows the output like this:
Tue Feb 24 17:39:35 JST 1998
Tuesday, March 31, 2009
String str = “26/08/1994″;
SimpleDateFormat formatter = new SimpleDateFormat(“dd/MM/yyyy”); //please notice the capital M
Date date = formatter.parse(str);
i chose string to formatter.parse(str) is error
Monday, January 11, 2010
Default DateStringFormat: Tue Feb 24 17:39:35 JST 1998
——————————————————————–
[date2string]
String aString = new String();
aString = (new Date()).toString();
[string2date]
DateFormat format = new SimpleDateFormat(“E MMM d HH:mm:ss z yyyy”,new Locale(“en”));
Date aDate = new Date();
aDate = (Date)format.parse(aString);
Friday, March 5, 2010
Hi, I found this post while searching for help with JavaScript. I’ve recently changed browsers from Safari to Internet Explorer 7. Just recently I seem to have a issue with loading JavaScript. Every time I go on a site that needs Javascript, my computer freezes and I get a “runtime error javascript.JSException: Unknown name”. I can’t seem to find out how to fix the problem. Any aid is greatly appreciated! Thanks