DateUtils { public static Date calculateNextTermBeginAfter(Date after, Date startDate, boolean excludeFirstTerm, ValidRuntimes runtimes) { if (after == null) { return null; } if (startDate == null) { return null; } if (runtimes == null) { return null; } Calendar afterCal = Calendar.getInstance(); afterCal.setTime(DateUtil.endOfDay(after)); Calendar calendar = Calendar.getInstance(); calendar.setTime(startDate); if (runtimes.fixedTerms) { switch (runtimes.firstMinRuntimeType) { case WEEKS: calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); calendar.add(Calendar.WEEK_OF_YEAR, 1); break; case MONTHS: calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.MONTH, 1); break; case YEARS: calendar.set(Calendar.DAY_OF_YEAR, 1); calendar.add(Calendar.YEAR, 1); break; default: } } boolean first = true; boolean validResult = true; while (validResult && (!calendar.after(afterCal) || (first && excludeFirstTerm))) { if (first) { validResult = addToCalendar(calendar, runtimes.firstMinRuntimeType, runtimes.firstMinRuntimeCount); first = false; } else { validResult = addToCalendar(calendar, runtimes.followingMinRuntimeType, runtimes.followingMinRuntimeCount); } } return validResult ? calendar.getTime() : null; }  static final boolean addToCalendar(Calendar calendar, IntervalType interval, int count); static Date calculateNextTermBeginAfter(Date after, Date startDate, boolean excludeFirstTerm, ValidRuntimes runtimes);  }
@Test public void testGetNextTermBegin() throws RemoteException { Date startDate = Date.from(ZonedDateTime .ofLocal(LocalDate.of(2016, 9, 23).atStartOfDay(), ZoneId.systemDefault(), null) .toInstant()); ValidRuntimes runtimes = new ValidRuntimes(); runtimes.firstMinRuntimeCount = 6; runtimes.firstMinRuntimeType = IntervalType.MONTHS; runtimes.followingMinRuntimeCount = 3; runtimes.followingMinRuntimeType = IntervalType.MONTHS; Date actual = DateUtils.calculateNextTermBeginAfter(now, startDate, false, runtimes); Date expected = Date.from(ZonedDateTime .ofLocal(LocalDate.of(2017, 3, 23).atStartOfDay(), ZoneId.systemDefault(), null) .toInstant()); assertEquals(expected, actual); }
@Test public void testGetNextTermBegin_FixedTerms_Months() throws RemoteException { Date startDate = Date.from(ZonedDateTime .ofLocal(LocalDate.of(2016, 9, 23).atStartOfDay(), ZoneId.systemDefault(), null) .toInstant()); ValidRuntimes runtimes = new ValidRuntimes(); runtimes.firstMinRuntimeCount = 6; runtimes.firstMinRuntimeType = IntervalType.MONTHS; runtimes.followingMinRuntimeCount = 3; runtimes.followingMinRuntimeType = IntervalType.MONTHS; runtimes.fixedTerms = true; Date actual = DateUtils.calculateNextTermBeginAfter(now, startDate, false, runtimes); Date expected = Date.from(ZonedDateTime .ofLocal(LocalDate.of(2017, 4, 1).atStartOfDay(), ZoneId.systemDefault(), null) .toInstant()); assertEquals(expected, actual); }
@Test public void testGetNextTermBegin_FixedTerms_Years() throws RemoteException { Date startDate = Date.from(ZonedDateTime .ofLocal(LocalDate.of(2016, 9, 23).atStartOfDay(), ZoneId.systemDefault(), null) .toInstant()); ValidRuntimes runtimes = new ValidRuntimes(); runtimes.firstMinRuntimeCount = 1; runtimes.firstMinRuntimeType = IntervalType.YEARS; runtimes.followingMinRuntimeCount = 6; runtimes.followingMinRuntimeType = IntervalType.MONTHS; runtimes.fixedTerms = true; Date actual = DateUtils.calculateNextTermBeginAfter(now, startDate, false, runtimes); Date expected = Date.from(ZonedDateTime .ofLocal(LocalDate.of(2017, 1, 1).atStartOfDay(), ZoneId.systemDefault(), null) .toInstant()); assertEquals(expected, actual); }
@Test public void testGetNextTermBeginAfter() throws RemoteException { Date startDate = Date.from(ZonedDateTime .ofLocal(LocalDate.of(2016, 9, 23).atStartOfDay(), ZoneId.systemDefault(), null) .toInstant()); ValidRuntimes runtimes = new ValidRuntimes(); runtimes.firstMinRuntimeCount = 6; runtimes.firstMinRuntimeType = IntervalType.MONTHS; runtimes.followingMinRuntimeCount = 3; runtimes.followingMinRuntimeType = IntervalType.MONTHS; Date actual = DateUtils.calculateNextTermBeginAfter(now, startDate, false, runtimes); actual = DateUtils.calculateNextTermBeginAfter(actual, startDate, false, runtimes); Date expected = Date.from(ZonedDateTime .ofLocal(LocalDate.of(2017, 6, 23).atStartOfDay(), ZoneId.systemDefault(), null) .toInstant()); assertEquals(expected, actual); }
@Test public void testCalculateNextTermBeginAfter_NullValues() throws RemoteException { Date startDate = Date.from(ZonedDateTime .ofLocal(LocalDate.of(2016, 9, 23).atStartOfDay(), ZoneId.systemDefault(), null) .toInstant()); ValidRuntimes runtimes = new ValidRuntimes(); runtimes.firstMinRuntimeCount = 6; runtimes.firstMinRuntimeType = IntervalType.MONTHS; runtimes.followingMinRuntimeCount = 3; runtimes.followingMinRuntimeType = IntervalType.MONTHS; Date result = DateUtils.calculateNextTermBeginAfter(null, startDate, false, runtimes); assertNull(result); result = DateUtils.calculateNextTermBeginAfter(now, null, false, runtimes); assertNull(result); result = DateUtils.calculateNextTermBeginAfter(now, startDate, false, null); assertNull(result); }