javaapache-commons-dateutils

Parseing strictly date with SimeplDateFormat


I'm trying to parse a date but I only get an ParseException. I'm working with the Apache DateUtils.

The code with the date that I'm tryting to parse is:

        Locale.setDefault(Locale.ENGLISH);
        System.out.println(Locale.getDefault());
        String[] list = {"EEE, d MMM yyyy HH:mm:ss Z"};
        try {
            DateUtils.parseDateStrictly("Mon, 20 Sep 2013 07:38:22 +0000",
                    list);          
        } catch (ParseException ex) {
            System.out.println(ex);          
        }

One of the things that I did, it was to change my Locale, since my default Locale is es_ES, but it doesn't work anyway.

Why doesn't it work?


Solution

  • Because you're using a strict parser; and September 20, 2013 was a Friday not a Monday. From the api, the parser parses strictly - it does not allow for dates such as "February 942, 1996".

    Date d = DateUtils.parseDateStrictly(
          "Fri, 20 Sep 2013 07:38:22 +0000",
          "EEE, d MMM yyyy HH:mm:ss Z");
    System.out.println(d);