Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a java Date back from toString() [duplicate]

Tags:

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.

like image 477
prepetti Avatar asked Jun 28 '12 07:06

prepetti


1 Answers

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"));` 
like image 69
Nuno Gonçalves Avatar answered Sep 21 '22 15:09

Nuno Gonçalves