/** * Converts a two-digit input year to a four-digit year. As the current calendar year * approaches a century, we assume small values to mean the next century. For instance, if * the current year is 2090, and the input value is "18", the user probably means 2118, * not 2018. However, in 2017, the input "18" probably means 2018. This code should be * updated before the year 9981. * * @param inputYear a two-digit integer, between 0 and 99, inclusive * @return a four-digit year */ @IntRange(from = 1000, to = 9999) static int convertTwoDigitYearToFour(@IntRange(from = 0, to = 99) int inputYear) { return convertTwoDigitYearToFour(inputYear, Calendar.getInstance()); }
private void updateInputValues(@NonNull @Size(2) String[] parts) { int inputMonth; int inputYear; if (parts[0].length() != 2) { inputMonth = INVALID_INPUT; } else { try { inputMonth = Integer.parseInt(parts[0]); } catch (NumberFormatException numEx) { inputMonth = INVALID_INPUT; } } if (parts[1].length() != 2) { inputYear = INVALID_INPUT; } else { try { inputYear = DateUtils.convertTwoDigitYearToFour(Integer.parseInt(parts[1])); } catch (NumberFormatException numEx) { inputYear = INVALID_INPUT; } } mIsDateValid = DateUtils.isExpiryDataValid(inputMonth, inputYear); }
/** * Gets the expiry date displayed on this control if it is valid, or {@code null} if it is not. * The return value is given as a length-2 {@code int} array, where the first entry is the * two-digit month (from 01-12) and the second entry is the four-digit year (2017, not 17). * * @return an {@code int} array of the form {month, year} if the date is valid, or {@code null} * if it is not */ @Nullable @Size(2) public int[] getValidDateFields() { if (!mIsDateValid) { return null; } final int[] monthYearPair = new int[2]; final String rawNumericInput = getText().toString().replaceAll("/", ""); final String[] dateFields = DateUtils.separateDateStringParts(rawNumericInput); try { monthYearPair[0] = Integer.parseInt(dateFields[0]); final int twoDigitYear = Integer.parseInt(dateFields[1]); final int fourDigitYear = DateUtils.convertTwoDigitYearToFour(twoDigitYear); monthYearPair[1] = fourDigitYear; } catch (NumberFormatException numEx) { // Given that the date should already be valid when getting to this method, we should // not his this exception. Returning null to indicate error if we do. return null; } return monthYearPair; }
@Test public void convertTwoDigitYearToFour_whenDateIsEarlyCenturyAndYearIsLarge_addsLowerBase() { Calendar earlyCenturyCalendar = Calendar.getInstance(); earlyCenturyCalendar.set(Calendar.YEAR, 2502); // In the year 2502, when you say "95", you probably mean 2495. assertEquals(DateUtils.convertTwoDigitYearToFour(95, earlyCenturyCalendar), 2495); // A more practical test earlyCenturyCalendar.set(Calendar.YEAR, 2017); assertEquals(DateUtils.convertTwoDigitYearToFour(99, earlyCenturyCalendar), 1999); }
@Test public void convertTwoDigitYearToFour_whenDateIsNearCenturyButYearIsSmall_addsIncreasedBase() { Calendar lateCenturyCalendar = Calendar.getInstance(); lateCenturyCalendar.set(Calendar.YEAR, 2081); assertEquals(DateUtils.convertTwoDigitYearToFour(8, lateCenturyCalendar), 2108); }
@Test public void convertTwoDigitYearToFour_whenDateIsNearCenturyAndYearIsLarge_addsNormalBase() { Calendar lateCenturyCalendar = Calendar.getInstance(); lateCenturyCalendar.set(Calendar.YEAR, 2088); assertEquals(DateUtils.convertTwoDigitYearToFour(95, lateCenturyCalendar), 2095); }
@Test public void convertTwoDigitYearToFour_whenCurrentYearIsLessThanEighty_addsNormalBase() { Calendar earlyCenturyCalendar = Calendar.getInstance(); earlyCenturyCalendar.set(Calendar.YEAR, 2017); assertEquals(DateUtils.convertTwoDigitYearToFour(19, earlyCenturyCalendar), 2019); }
@Test public void convertTwoDigitYearToFour_whenDateIsMidCenturyAndYearIsLarge_addsNormalBase() { Calendar midCenturyCalendar = Calendar.getInstance(); midCenturyCalendar.set(Calendar.YEAR, 3535); assertEquals(DateUtils.convertTwoDigitYearToFour(99, midCenturyCalendar), 3599); }