Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert Date.toString back to Date?

Tags:

java

I have a string obtained by calling the toString method of an instance of the class Date. How can I get a Date object from this string?

Date d = new Date();
String s = d.toString;
Date theSameDate = ...

UPDATE

I've tried to use SimpleDateFormat, but I get java.text.ParseException: Unparseable date What is the date format produced by Date.toString ()?

like image 882
user1003208 Avatar asked Feb 24 '12 13:02

user1003208


2 Answers

You could do it like this

Date d = new Date();
String s = d.toString;
Date theSameDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);
like image 195
Sedalb Avatar answered Sep 27 '22 17:09

Sedalb


If your real goal is to serialize a Date object for some kind of custom made persistence or data transfer, a simple solution would be:

Date d = new Date();
long l = d.getTime();
Date theSameDate = new Date(l);
like image 29
flesk Avatar answered Sep 27 '22 19:09

flesk