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?

4
  • 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). Commented Jun 13, 2017 at 6: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. Commented Jun 13, 2017 at 6: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? Commented Jun 13, 2017 at 8: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. Commented Jun 13, 2017 at 8:28

1 Answer 1

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".

Sign up to request clarification or add additional context in comments.

2 Comments

Victor, the end of your statement is incorrect. You should still add the 'GMT' in the pattern: EEE MMM dd yyyy HH:mm:ss 'GMT'Z
Thanks a lot! That wasn't really clear in the documentation I was using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.