Joda Time and parsing two digits of the year correctly based on hinges

I have the following (somewhat abbreviated) code to parse Stringinput to a Joda Time object DateTime. I need to handle several formats correctly, including four and two years.

setupValidDateFormats();
DateTime date = convertToDateTime(thisField.getText());
if (date != null) {
    thisField.setText(date.toString("MM/dd/yyyy"));
} else {
    System.out.println("Invalid date");
}

private void setupValidDateFormats() {
    DateTimeParser[] formats = {
            DateTimeFormat.forPattern("MM/dd/yyyy").getParser(),
            DateTimeFormat.forPattern("MM/dd/yy").getParser(),
            DateTimeFormat.forPattern("MM-dd-yyyy").getParser(),
            DateTimeFormat.forPattern("MM-dd-yy").getParser(),
            DateTimeFormat.forPattern("MMddyyyy").getParser(),
            DateTimeFormat.forPattern("MMddyy").getParser()
    };
    formatter = new DateTimeFormatterBuilder().append(null, formats).appendTwoDigitYear(1950, true).toFormatter()
            .withLocale(Locale.US);
}

private DateTime convertToDateTime(String input) {
    if (isNullOrEmpty(input)) {
        return null;
    }

    DateTime date;
    try {
        // When you parse a date, it'll throw an exception for not only invalid formats,
        // but for invalid values such as 09/31/2013 or 02/30/2013. Leap year is included as well.
        date = formatter.parseDateTime(input);
    } catch (Exception e) {
        // User input a date in the incorrect format, or an invalid value.
        // Example: 02/30/2013 is invalid.
        date = null;
    }

    return date;
}

The problem I am facing is that when the user types in 120100, I expect him to be able to parse correctly and eventually output to 12/01/1900. However, formatter.parseDateTime(input);in convertToDateTimeinstead it IllegalArgumentExceptionreturns, and the method returns null.

It may be worth noting that if I remove appendTwoDigitYearfrom DateTimeFormatterBuilder, the input parses successfully, but 120100it becomes 12/01/0000, but that is not what I need.

API- Joda Time? appendTwoDigitYear 1950 00 1900 ?

+4
1

,

DateTimeFormat.forPattern("MMddyyyy")

"120100". , 12 MM, 01 dd 00 yyyy, .. 0 0, 0000.

MMddyy parsers, .

, , .

public static void main(String[] args) throws Exception {
    String str = "120100";

    DateTimeParser[] formats = {
            DateTimeFormat.forPattern("MM/dd/yyyy").getParser(),
            DateTimeFormat.forPattern("MM/dd/yy").getParser(),
            DateTimeFormat.forPattern("MM-dd-yyyy").getParser(),
            DateTimeFormat.forPattern("MM-dd-yy").getParser(),
            DateTimeFormat.forPattern("MMddyy").getParser()
    };

    DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, formats).toFormatter()
            .withPivotYear(1950).withLocale(Locale.US);

    DateTime dateTime = formatter.parseDateTime(str);
    System.out.println(dateTime);       
}

1900-12-01T00:00:00.000-05:00

, appendTwoDigitYear.

+6

All Articles