0

I have this string I want to convert to a java Date object:
"Mon Jun 12 2017 21:00:15 GMT+0200 (W. Europe Summer Time)"

The first thing I do is splitting the string before the first '(' then removing any trailing spaces so I'm left with:
"Mon Jun 12 2017 21:00:15 GMT+0200"

Right now I'm trying to parse it using the following SimpleDateFormat pattern:
"E M d y H:m:s 'GMT'Z"

Unfortunately that won't work. I tried a few variations but nothing will work. Is it even possible to convert that particular string to a Date?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Luminai
  • 29
  • 1
  • 1
  • 8
  • 1
    As an aside, you are still using the outdated `SimpleDateFormat` and `Date` classes? Any reason? I would suggest `OffsetDateTime.parse("Mon Jun 12 2017 21:00:15 GMT+0200", DateTimeFormatter.ofPattern("EEE MMM d uuuu HH:mm:ss 'GMT'XX", Locale.ENGLISH))`. The modern date and time classes are so much nicer to work with (the format pattern letters are more or less the same). – Ole V.V. Jun 13 '17 at 06:44
  • PS I had hoped to match all of `GMT+0200` with pattern `OOOO` (capital letter O), but it doesn’t work. It seems to require a colon: `GMT+02:00`. This may be a bug in Java 8, and if so, it may be fixed in Java 9. – Ole V.V. Jun 13 '17 at 06:48
  • @OleV.V. I am usin the `Date` class because I had the time and date in one object which made it easy to persist to a MySQL database using database type DATETIME. I used the SDF because that's all I come a cross when I searched for ways to convert string to a temporal object. What would be the modern classes to use in this situation? – Luminai Jun 13 '17 at 08:09
  • I haven’t used MySQL since 2011 or so. I seem to read that with Connector/J 5.1 or newer, you can directly store and retrieve the newer classes to and from the database. Store an `Instant`, it corresponds to the oldfashioned `Date` class. The `OffsetDateTime` class used in my first comment has a `toInstant` method that does what the name says. Please search your documentation and the net for details. – Ole V.V. Jun 13 '17 at 08:28

1 Answers1

2

The pattern you are using is incorrect. If you have an abbreviation for the day of the week that is 3 letters long you should use EEE, and so on.

Try "EEE MMM dd yyyy HH:mm:ss 'GMT'z".

victor
  • 1,573
  • 11
  • 23