Written by nitinpai on March 28th, 2007
Convert Date to String and String to Date in Java
Java
12 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

12 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
Wednesday, January 12, 2011
this code wil work well in java application, but not in midlet for java mobile, anybody can help how covert string date from recordstore back to datafield date formar.
Saturday, February 26, 2011
Hi All,
The solution given above will work when you will specify the date in string i.e. hard coded.
but it will not work for the fol scenario:
When you will fetch the current date
say
Date date =new Date();
This will return you the current date in the format as
Wed Jan 01 11:54:53 GMT+05:30 2003
The DateFormat class instance will not be able to parse it.
can anybody please provide solution for this.
thanks in advance.
Sunday, April 3, 2011
this code wil work well in java application, but not in midlet for java mobile, anybody can help how covert string date from recordstore back to datafield date formar.
(I also faced this problem,anybody help?)
Wednesday, July 13, 2011
Thank you, this is a problem I face all the time, and it’s nice to have a quick easy solution.
Monday, August 29, 2011
Hi all:
I need to compare oracle column which containg date as a values with the current system date in the servlet page but I dont know how to do it. Anyone can tell me the code. It’s really very urgent.
Monday, October 3, 2011
it does’nt work because it says “unparseable Date” “2010-04-02″ when i take the format like
“yyyy-MM-dd” ,i dont knw why it does’nt work. every where i see the same code but it does’nt work in my system
please tell me any other way becoz i have tried different pattern also
none of them are usefull;
finally i converted every thing into string;
Saturday, December 17, 2011
Thanks for this