1

I have been trying to parse a string to a Date and i have searched this everywhere and could not find a solution to it. I have a String formatted. And when I try to parse it it always throws an exception though i have tried setting Locale.English also and giving the date pattern (obviously). And my Date pattern is "Wed, 29 Jun 2016 16:16:32 +0000". Thanks in advance for help.

dateFormat = new SimpleDateFormat("EEE, DD MMM yyyy HH:mm:ss 'Z'", Locale.ENGLISH);

try {
                String dateA = "Wed, 29 Jun 2016 16:16:32 +0000";
                String dateB = "Wed, 29 Jun 2016 16:04:54 +0000";
                Date parsedDateA = dateFormat.parse(dateA);
                Date parsedDateB = dateFormat.parse(dateB);
                if (parsedDateA .equals(parsedDateB ) || parsedDateA .before(parsedDateB )) {
                    //Do some work here

                }

            } catch (ParseException e) {
                e.printStackTrace();

            }
Shoaib Anwar
  • 1,555
  • 12
  • 26

1 Answers1

1

From the docs : "EEE, d MMM yyyy HH:mm:ss Z" . The 'd' should be lowercase.

Uppercase D represents the day in year rather than day of the month.

Edit: Thanks to @MikeM.'s suggestion: remove the single quotes 'Z' you have around Z. I did not notice that at first.

Vucko
  • 7,371
  • 2
  • 27
  • 45
  • Yes thanks for pointing out that mistake but thing is that it still throws an exception. It says unparseable date (at offset 26) and that happens to be the offset of + symbol in "Wed, 29 Jun 2016 16:16:32 +0000". – Shoaib Anwar Jun 29 '16 at 19:38
  • This error is about the timezone (I counted 26 characters from the start). Check [this](https://developer.android.com/reference/java/text/SimpleDateFormat.html#rfc822timezone). Change your timezone to `+00:00`, try some different variations to see which one works. – Vucko Jun 29 '16 at 19:49
  • 1
    @ShoaibAnwar The `Z` should not be in single quotes. – Mike M. Jun 29 '16 at 22:52
  • @ShoaibAnwar Read that again. Should _not_ be in single quotes. Not. – Mike M. Jun 29 '16 at 22:54
  • @MikeM. ah mike nice spot, I did not even notice that in his post. ShoaibAnwar remove the quotes around 'Z'. – Vucko Jun 29 '16 at 22:55
  • Oh ok, thanks alot , issue's resolved. Thanks again. – Shoaib Anwar Jun 29 '16 at 22:59