/** * Determines whether or not the input year has already passed. * * @param year the input year, as a two or four-digit integer * @param now, the current time * @return {@code true} if the input year has passed the year of the specified current time * {@code false} otherwise. */ static boolean hasYearPassed(int year, Calendar now) { int normalized = normalizeYear(year, now); return normalized < now.get(Calendar.YEAR); }
/** * Determines whether the input year-month pair has passed. * * @param year the input year, as a two or four-digit integer * @param month the input month * @param now the current time * @return {@code true} if the input time has passed the specified current time, * {@code false} otherwise. */ @SuppressWarnings("WrongConstant") static boolean hasMonthPassed(int year, int month, Calendar now) { if (hasYearPassed(year, now)) { return true; } // Expires at end of specified month, Calendar month starts at 0 return normalizeYear(year, now) == now.get(Calendar.YEAR) && month < (now.get(Calendar.MONTH) + 1); }
@Test public void normalizeSameCenturyShouldPass() { Calendar now = Calendar.getInstance(); int year = 1997; now.set(Calendar.YEAR, year); assertEquals(ModelUtils.normalizeYear(97, now), year); }
@Test public void normalizeDifferentCenturyShouldFail() { Calendar now = Calendar.getInstance(); int year = 1997; now.set(Calendar.YEAR, year); assertNotEquals(ModelUtils.normalizeYear(97, now), 2097); } }