Convert Date to String and String to Date in Java

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 thoughts on “Convert Date to String and String to Date in Java

  1. 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

  2. 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

  3. 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);

  4. 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

  5. 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.

  6. 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.

  7. 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?)

  8. 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.

  9. 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;

Leave a Reply

Your email address will not be published. Required fields are marked *