12

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1003208
  • 331
  • 1
  • 3
  • 12
  • Try parsing with, say, `SimpleDateFormat`. –  Feb 24 '12 at 13:57
  • 1
    Do not use the String. Keep the Date object and only transform to String when needed. – m0skit0 Feb 24 '12 at 13:59
  • 1
    It might be a good idea to provide some context on what you're really trying to achieve. – flesk Feb 24 '12 at 14:08
  • 1
    Do not use `Date`. The modern approach uses `java.time.Instant` and standard [ISO 8601](https://en.m.wikipedia.org/wiki/ISO_8601) format for the string. See the [correct Answer by Ole V.V.](https://stackoverflow.com/a/48205424/642706). – Basil Bourque Jan 11 '18 at 17:02
  • Possible duplicate of [How to convert a date in this format (Tue Jul 13 00:00:00 CEST 2010) to a Java Date (The string comes from an alfresco property)](https://stackoverflow.com/questions/5937017/how-to-convert-a-date-in-this-format-tue-jul-13-000000-cest-2010-to-a-java-d) – Ole V.V. Jan 20 '18 at 10:02

5 Answers5

26

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);
flesk
  • 7,439
  • 4
  • 24
  • 33
  • 3
    FYI: The troublesome` Date` Class is now legacy, replaced by `Instant` and the other *java.time* classes. See the modern approach in [this Answer](https://en.m.wikipedia.org/wiki/ISO_8601) by Ole V.V. – Basil Bourque Jan 11 '18 at 17:06
20

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);
Marko
  • 20,385
  • 13
  • 48
  • 64
Sedalb
  • 201
  • 1
  • 3
  • 2
  • This method also works with org.json library when converting Java Date objects to and from JSONObjects using the bean constructor, since it uses Date.toString() when constructing JSONObjects. Thank you for this, it's exactly the format for SimpleDateFormat that I was looking for. – Coty Condry Jan 25 '17 at 16:18
  • 1
    FYI, the troublesome old classes used here are now legacy, supplanted by the *java.time* classes. – Basil Bourque Jan 11 '18 at 17:13
  • An explanation would be in order. What is the gist/idea? – Peter Mortensen Feb 09 '21 at 17:41
  • OK, the OP has left the building: *"Last seen more than 10 years ago"* – Peter Mortensen May 12 '23 at 21:09
6
  1. If your real goal is to serialize and deserialize a date and time (for data transfer or for persistence, for example), serialize to ISO 8601, the standard format for date and time data.
  2. Skip the long outdated Date class. The modern Java date and time API known as java.time is so much nicer to work with. The class you need from it is probably Instant (this depends on your more exact requirements).

The two points go nicely hand in hand:

    Instant i = Instant.now();
    String s = i.toString();
    Instant theSameInstant = Instant.parse(s);

The modern classes’ toString methods produce ISO 8601 format (e.g., 2018-01-11T10:59:45.036Z), and their parse methods read the same format back. So this snippet is all you need, and you get an instant equal to the first, with nanosecond precision.

If you cannot control the string you get, and you get the result from Date.toString(), the format pattern string in Sedalb’s answer works with java.time too:

    DateTimeFormatter dtf 
            = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);
    Date d = new Date();
    String s = d.toString();
    Instant nearlyTheSameInstant = ZonedDateTime.parse(s, dtf).toInstant();

It’s essential to provide a locale. Otherwise the JVM’s default locale will be used, and if it’s not English, parsing will fail. In the worst case you will see your code running fine for many years and suddenly it will break when one day someone runs it on a computer or device with a different locale setting.

The point from jambjo’s answer still applies: The three and four letter time zone abbreviations used in Date.toString() are very often ambiguous, so there is no guarantee that the time zone is interpreted correctly, and again, it will be interpreted differently on different JVMs.

Finally, Date.toString() does not render the milliseconds that the Date holds, which leads to an inaccuracy of up to 999 milliseconds. If using the string from Date.toString(), there is nothing we can do about it (which was why I named the variable nearlyTheSameInstant).

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
2

Take a look at SimpleDateFormat#parse(). It should provide the functionality you're looking for.

Sam DeHaan
  • 10,246
  • 2
  • 40
  • 48
-1

Date theSameDate = new Date(Date.parse(s));

For some not so obvious reasons, this is not a particularly good idea. You can find details on that in the API documentation for the parse method. One problem is e.g. that the time zone abbreviations are ambiguous, so that the parser may fail in interpreting the correct time zone.

jarnbjo
  • 33,923
  • 7
  • 70
  • 94