Search code examples
androidexceptionsimpledateformatandroid-dateunparseable

simpleDateFormat throws unparseable date exception


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();

            }

Solution

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