How to parse only 4 digit years

Viewed 2116

I'm using Joda-Time to parse years like this:

private DateTime attemptParse(String pattern, String date) {
        DateTimeFormatter parser = DateTimeFormat.forPattern(pattern).withLocale(Locale.ENGLISH);
        DateTime parsedDateTime = parser.parseLocalDateTime(date).toDateTime(WET);
        return parsedDateTime;
    }

I'm trying to parse multiple formats: "yyyy-MM-dd", "yyyy-MMM-dd","yyyy MMM dd-dd","yyyy MMM", (etc), "yyyy". When one doesn't work, I try the next one.

And it works like a charm when the string is indeed only 4 digits (e.g: "2016"). The problem is that I sometimes receive things like this: "201400". And Joda-Time matches this with "yyyy" pattern and returns a date with year 201400.

I wanted to avoid the ugly if to check if year > 9999. Is there any way to do this using Joda-Time?

2 Answers
Related