DateValidator { protected boolean isValidHelper(String monthString, String yearString) { if (TextUtils.isEmpty(monthString)) { return false; } if (TextUtils.isEmpty(yearString)) { return false; } if (!TextUtils.isDigitsOnly(monthString) || !TextUtils.isDigitsOnly(yearString)) { return false; } int month = Integer.parseInt(monthString); if (month < 1 || month > 12) { return false; } int currentYear = getCurrentTwoDigitYear(); int year; int yearLength = yearString.length(); if (yearLength == 2) { year = Integer.parseInt(yearString); } else if (yearLength == 4) { year = Integer.parseInt(yearString.substring(2)); } else { return false; } if (year == currentYear && month < getCurrentMonth()) { return false; } if (year < currentYear) { int adjustedYear = year + 100; if (adjustedYear - currentYear > MAXIMUM_VALID_YEAR_DIFFERENCE) { return false; } } if (year > currentYear + MAXIMUM_VALID_YEAR_DIFFERENCE) { return false; } return true; } protected  DateValidator(Calendar calendar); static boolean isValid(String month, String year); static final int MAXIMUM_VALID_YEAR_DIFFERENCE; }
@Test public void yearWrapping() { Calendar endOfCenturyCalendar = Calendar.getInstance(); endOfCenturyCalendar.set(Calendar.YEAR, 2095); endOfCenturyCalendar.set(Calendar.MONTH, Calendar.NOVEMBER); DateValidator endOfCenturyValidator = new DateValidator(endOfCenturyCalendar); assertTrue(endOfCenturyValidator.isValidHelper("01","01")); assertTrue(endOfCenturyValidator.isValidHelper("01","05")); assertTrue(endOfCenturyValidator.isValidHelper("01","96")); assertFalse(endOfCenturyValidator.isValidHelper("01","94")); assertInvalid("01","01"); assertInvalid("01","05"); assertInvalid("01","94"); }