0

I'm trying to parse a date string with a GMT+100

new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss").parse("Thu Apr 23 2015 11:30:49 GMT+0100")

Comes out as

Thu Apr 23 11:30:49 UTC 2015

If I add z or Z or X to the format, it's unparseable. If I don't add it, it's off by the offset, one hour.

What is the right way to parse this date?

--

Update: this differs from Java SimpleDateFormat Pattern for JavaScript Date in two ways: 1) this question is pure java and would have accepted answers other than those using SimpleDateFormat (i.e. new Java 8 features), and 2) the solution here is different to the other question.

antonyh
  • 2,131
  • 2
  • 21
  • 42
  • 1
    try changing the format to `EEE MMM dd yyyy HH:mm:ss 'GMT'Z`(with the quotes) – Ayush Gupta Feb 03 '18 at 18:06
  • I also had to set `format.setTimeZone(TimeZone.getTimeZone("UTC"));` to get consistent results, otherwise the date objects would display the same via toString(), but off by one hour via getTime(). – antonyh Feb 03 '18 at 18:48
  • 1
    The `toString` method lies, implicitly applying your JVM’s current default time zone. These classes are now legacy, supplanted by the java.time classes. Please search Stack Overflow as all of this has been addressed here many many many many many times already. – Basil Bourque Feb 03 '18 at 19:15
  • I've checked the output via getTime() - it's off there too. This isn't a toString() / local time zone issue. Variations of this have been asked, but not identically... I did search but given the goal of parsing this format, it's still open to fresh answers particularly if they can use newer JDK features. Ultimately I just want a stable implementation, I'm not forced to use SimpleDateFormat. – antonyh Feb 07 '18 at 14:10
  • @antonyh I gave [a lengthy Answer](https://stackoverflow.com/a/41991050/642706) on the original Question. Your Question here is a duplicate of that Question. Follow the link to there. – Basil Bourque Feb 07 '18 at 16:49
  • @BasilBourque Thanks, I somehow failed to see that; have an upvote! What should I do with this question now - leave it as a duplicate or delete it? I'm not sure what the etiquette / policy is for duplicates. – antonyh Feb 08 '18 at 13:17
  • @antonyh This Question is now closed, so you need not do anything. The founders of Stack Overflow value leaving duplicates in place as bread crumbs for later readers to follow in discovery of the earlier original Questions. This is speaking generally, some Questions are occasionally voted for deletion if considered to be of no use at all. You can always delete your own Question, if you think it will collect down-votes. – Basil Bourque Feb 08 '18 at 19:04

1 Answers1

2

Your expresssion should be EEE MMM dd yyyy HH:mm:ss 'GMT'Z(including the quotes), so your code is as follows:

new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z").parse("Thu Apr 23 2015 11:30:49 GMT+0100")

HOW THIS WORKS

According to the docs, anything passed in between single quotes(') in SimpleDateFormat pattern is not interpolated, but is assumed be just a part of the date format to be ignored while parsing.

And the equivalent of +0100 in SimpleDateFormat pattern terms is Z

Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92
  • 1
    You are using troublesome old date-time classes that were outmoded years ago by the *java.time* classes. – Basil Bourque Feb 03 '18 at 19:12
  • Sometimes the troublesome old Java is all we have, pre-Java 8, without using extra libraries. – antonyh Feb 07 '18 at 11:45
  • @BasilBourque please feel free to answer with more modern Java, I've revised the question so it's not limited to SimpleDateFormat (because I'm not forced to use this) – antonyh Feb 07 '18 at 14:11