Search code examples
javaandroiddatedate-parsingapache-commons-dateutils

Problems with parsing dates using DateUtils


I am trying to parse various slightly different date strings for an Android application.

Two examples are:

Thu, 20 Aug 2020 13:30:16 +0000
Wed, 19 Aug 2020 15:28:47 GMT

Here is how I tried parsing these Strings:

Date pubDate = org.apache.commons.lang3.time.DateUtils.parseDate(dateString, 
    "EEE, dd MMM yyyy HH:mm:ss Z", "EEE, dd MMM yyyy HH:mm:ss zzz");

As far as I can tell this should work, but I am getting java.text.ParseException: Unable to parse the date: Wed, 19 Aug 2020 15:28:47 GMT exceptions for both those examples. What am I doing wrong here?


Solution

  • Your format is built into java.time

    Use DateTimeFormatter.RFC_1123_DATE_TIME; it works for both of your strings. It’s really the same format, only with the offset from UTC (or GMT) denoted differently. DateTimeFormatter is part of java.time, the modern Java date and time API. I recommend that you prefer java.time over Date and DateUtils, also because the Date class is poorly designed and long outdated.

        String s = "Thu, 20 Aug 2020 13:30:16 +0000";
        OffsetDateTime dt = OffsetDateTime.parse(s, DateTimeFormatter.RFC_1123_DATE_TIME);
        System.out.println(dt);
    

    Output is:

    2020-08-20T13:30:16Z

    Let’s try with your other string example:

        String s = "Wed, 19 Aug 2020 15:28:47 GMT";
    

    2020-08-19T15:28:47Z

    Question: Doesn’t java.time require Android API level 26?

    java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

    • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
    • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
    • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

    Links