21

I have a String containing the result of toString() called on an instance of java.util.Date. How can I parse this value back to a Date object?

The Java docs say that toString() converts this Date object to a String of the form:

 dow mon dd hh:mm:ss zzz yyyy

but of course there is no such format tag as "dow" or "mon".

Could someone please help me with this problem. Please note that unfortunately the toString() call is in a piece of code out of my control.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
prepetti
  • 367
  • 1
  • 2
  • 11

5 Answers5

33

If you don't have control over the code that's generating the String:

To parse the toString() format you need to set the SimpleDateFormat locale to english and use the format: "EEE MMM dd HH:mm:ss Z yyyy".

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", new Locale("us"));`
Alex R
  • 11,364
  • 15
  • 100
  • 180
Nuno Gonçalves
  • 6,202
  • 7
  • 46
  • 66
  • Mm..as I said, I have no control over the toString(), since it is made by Struts. Anyway, setting the EN locale does the trick, so thank you. I just have to note that the right format string is "EEE MMM dd HH:mm:ss zzz yyyy", you had forgotten the Month. – prepetti Jun 28 '12 at 07:40
  • I edited the answer to have it. :) – Nuno Gonçalves Jun 28 '12 at 07:44
2

I didn't test but something like this probably would work:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM HH:mm:ss z yyyy");
Date date = sdf.parse(dateStr);

If not, try to correct it using documentation: http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#toString() http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Przemysław Różycki
  • 809
  • 2
  • 10
  • 21
-1

Use simpledateformat. Find the doumentation here: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
thommie
  • 438
  • 5
  • 22
-1

First take a look at all the date formats provided by java Date Formats. And you can use SimpleDateFormat class to do what you want.

 public class DateFormatTest 
    { 
      public DateFormatTest() 
      { 
        String dateString = // in "dow mon dd hh:mm:ss zzz yyyy" format

        SimpleDateFormat dateFormat = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy"); 
        Date convertedDate = dateFormat.parse(dateString); 

        System.out.println("Converted string to date : " + convertedDate); 
      } 

      public static void main(String[] argv) 
      { 
        new DateFormatTest(); 
      } 
    } 
  }
JAVAGeek
  • 2,674
  • 8
  • 32
  • 52
-2

Suppsoe you get String of "dateString" ;

SimpleDateFormat sdf = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");

Date date = sdf.parse("dateString");
Ahmad
  • 2,110
  • 5
  • 26
  • 36
  • This does not work and results in an exception being thrown. The correct constructor call is: `SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", new Locale("us"));` – sizzle Sep 26 '17 at 15:53