-1

I am trying to parse this (and many similar) dateString - "Wed Aug 26 2020 11:03:30 GMT-0500"

Looking at the SimpleDateFormat documentation, I was assuming that a pattern like this should work:

String dateFormat = "EEE MMM d yyyy HH:mm:ss z";

However, it doesn't. But the following format is able to parse

String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'z";

But when I print the parsed date, I get the time with an hour added and offset reduced by an hour - Wed Aug 26 12:03:30 GMT-04:00 2020

What can I do to prevent this offset change?

Here is the sample code:

String dateStr = "Wed Aug 26 2020 11:03:30 GMT-0500";        
String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'z";
Date date = new SimpleDateFormat(dateFormat).parse(dateStr);
System.out.println("Original Date String : "+dateStr);
System.out.println("Original Date  Object : "+date);
Output:
Original Date String : Wed Aug 26 2020 11:03:30 GMT-0500
Original Date  Object : Wed Aug 26 12:03:30 GMT-04:00 2020
Gowri
  • 13
  • 1
  • 2
    [`Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) does *not* store time zone information, so when you print the value, it is formatted in the JVM's default time zone. To retain time zone information, use the newer [`OffsetDateTime`](https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html) and [`DateTimeFormatter`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) classes. – Andreas Aug 25 '20 at 14:22
  • 1
    The title of this question is completely wrong btw. It is parsing the date just fine. your problem is that you are unhappy with the way it prints that parsed date again. – OH GOD SPIDERS Aug 25 '20 at 14:26
  • 2
    **Never ever use `Date` and `SimpleDateFormat`.** Use the new Java Date and Time API (in the `java.time` package) instead. – MC Emperor Aug 25 '20 at 14:33
  • @MCEmperor Except when you don't have a choice, e.g. when using a library that needs a `Date`. Never say never. – Andreas Aug 25 '20 at 14:37
  • @Andreas Of course, but first learn the rule, then the exception. Not the other way around. – MC Emperor Aug 25 '20 at 14:45
  • For the nuances: If a legacy API that we cannot upgrade right now requires a `Date`, produce an `Instant` and convert it to `Date` before calling. But fortunately very, very few APIs require a `SimpleDateFormat`, so we can *almost* always avoid using that one. – Ole V.V. Aug 25 '20 at 17:21
  • Does this answer your question? [TimeZone problem in Java](https://stackoverflow.com/questions/1812700/timezone-problem-in-java) Does [this](https://stackoverflow.com/questions/16107898/simpledateformat-returns-wrong-time-zone-during-parse)? – Ole V.V. Aug 25 '20 at 17:34

1 Answers1

2

Use java.time.OffsetDateTime here because there is no zone in that String, just an offset and the classes you are using are outdated for good reasons... Get rid of java.util.Date and java.text.SimpleDateFormat.

See this example:

public static void main(String[] args) {
    // provide the String to be parsed
    String dateStr = "Wed Aug 26 2020 11:03:30 GMT-0500";
    // provide a matching pattern
    String dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'Z";
    // create a formatter with this pattern and a suitable locale for unit names
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat, Locale.ENGLISH);
    // parse the String to an OffsetDateTime using the formatter
    OffsetDateTime odt = OffsetDateTime.parse(dateStr, dtf);
    // print the result in the default format
    System.out.println("Default/ISO format:\t" + odt);
    // and print it in your custom format
    System.out.println("Custom format:\t\t" + odt.format(dtf));
}

Output:

Default/ISO format: 2020-08-26T11:03:30-05:00
Custom format:      Wed Aug 26 2020 11:03:30 GMT-0500
deHaar
  • 17,687
  • 10
  • 38
  • 51