Search code examples
javasimpledateformat

Date parse exception EEE MMM dd HH:mm:ss z yyyy in few cases


I am using this format to parse:

SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH)

However in few minor cases, the date not parsed.

Parsing error

Mon Jul 12 13:42:07 GMT+1 2021
Sat Jul 17 09:20:27 IST 2021

And successful parsing for the following:

Sun Oct 11 12:56:41 GMT+05:30 2020
Fri Aug 20 03:33:16 CST 2021

Solution

  • java.time

    I recommend that you use java.time, the modern Java date and time API, for your date and time work. Parsing all four of your strings is a bit of challenge alright. The following formatter works.

    // Substitute your own set of preferred zones here
    private static final Set<ZoneId> PREFERRED_ZONES
            = Set.of(ZoneId.of("Atlantic/Reykjavik"), ZoneId.of("America/Havana"));
    private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()
            .appendPattern("EEE MMM dd HH:mm:ss [O][")
            .appendZoneText(TextStyle.SHORT, PREFERRED_ZONES)
            .appendPattern("] yyyy")
            .toFormatter(Locale.ENGLISH);
    

    Trying it out:

        String[] strings = {
                "Mon Jul 12 13:42:07 GMT+1 2021",
                "Sat Jul 17 09:20:27 IST 2021",
                "Sun Oct 11 12:56:41 GMT+05:30 2020",
                "Fri Aug 20 03:33:16 CST 2021"
        };
    
        for (String string : strings) {
            ZonedDateTime zdt = ZonedDateTime.parse(string, PARSER);
            System.out.println(zdt);
        }
    

    Output is:

    2021-07-12T13:42:07+01:00
    2021-07-17T09:20:27Z[Atlantic/Reykjavik]
    2020-10-11T12:56:41+05:30
    2021-08-20T03:33:16-04:00[America/Havana]
    

    While IST, GMT+05:30 and CST can be parsed as time zone abbreviations, GMT+1 cannot. So I am instructing my formatter to parse a localized offset if it can. Pattern letter O parses GMT+1 in English (and most other locales). And then to try to parse a time zone abbreviation. The square brackets in the format pattern strings denote optional parts.

    Next challenge was that IST and CST like many time zone abbreviations are ambiguous. So I am also giving the formatter a set of preferred time zones to use to solve the ambiguities. You need to decide which time zones you want those abbreviations to denote, and supply a corresponding set of ZonedId objects.