DateTimes { public static long getNextOccurrence(long now, DayOfWeek dayOfWeek, int hour, int minute) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(now); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute); if (calendar.getTimeInMillis() <= now) { calendar.add(Calendar.DATE, 1); } while (calendar.get(Calendar.DAY_OF_WEEK) != dayOfWeek.calendarConst) { calendar.add(Calendar.DATE, 1); } return calendar.getTimeInMillis(); } private  DateTimes(); static long getNextOccurrence(long now, DayOfWeek dayOfWeek, int hour, int minute);  }
@Test public void testGetNextOccurrence_fallBack() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); cal.set(2016, Calendar.OCTOBER, 29, 13, 0, 0); cal.set(Calendar.MILLISECOND, 0); Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); expected.set(2016, Calendar.NOVEMBER, 1, 13, 0, 0); expected.set(Calendar.MILLISECOND, 0); assertEquals(expected.getTimeInMillis(), DateTimes.getNextOccurrence(cal.getTimeInMillis(), DayOfWeek.TUESDAY, 13, 0)); }
@Test public void testGetNextOccurrence_laterToday() throws Exception { Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); expected.set(2016, Calendar.JULY, 1, 14, 0, 0); expected.set(Calendar.MILLISECOND, 0); assertEquals(expected.getTimeInMillis(), DateTimes.getNextOccurrence(JULY_FIRST_2016, DayOfWeek.FRIDAY, 14, 0)); }
@Test public void testGetNextOccurrence_tomorrow() throws Exception { Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); expected.set(2016, Calendar.JULY, 2, 9, 0, 0); expected.set(Calendar.MILLISECOND, 0); assertEquals(expected.getTimeInMillis(), DateTimes.getNextOccurrence(JULY_FIRST_2016, DayOfWeek.SATURDAY, 9, 0)); }
@Test public void testGetNextOccurrence_overmorrow() throws Exception { Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); expected.set(2016, Calendar.JULY, 3, 9, 0, 0); expected.set(Calendar.MILLISECOND, 0); assertEquals(expected.getTimeInMillis(), DateTimes.getNextOccurrence(JULY_FIRST_2016, DayOfWeek.SUNDAY, 9, 0)); }
@Test public void testGetNextOccurrence_earlierToday() throws Exception { Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); expected.set(2016, Calendar.JULY, 8, 9, 0, 0); expected.set(Calendar.MILLISECOND, 0); assertEquals(expected.getTimeInMillis(), DateTimes.getNextOccurrence(JULY_FIRST_2016, DayOfWeek.FRIDAY, 9, 0)); }
@Test public void testGetNextOccurrence_now() throws Exception { Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); expected.set(2016, Calendar.JULY, 8, 13, 0, 0); expected.set(Calendar.MILLISECOND, 0); assertEquals(expected.getTimeInMillis(), DateTimes.getNextOccurrence(JULY_FIRST_2016, DayOfWeek.FRIDAY, 13, 0)); }
@Test public void testGetNextOccurrence_springForward() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); cal.set(2016, Calendar.MARCH, 26, 13, 0, 0); cal.set(Calendar.MILLISECOND, 0); Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); expected.set(2016, Calendar.MARCH, 28, 13, 0, 0); expected.set(Calendar.MILLISECOND, 0); assertEquals(expected.getTimeInMillis(), DateTimes.getNextOccurrence(cal.getTimeInMillis(), DayOfWeek.MONDAY, 13, 0)); }
@Test public void testGetNextOccurrence_nextMonth() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); cal.set(2016, Calendar.JULY, 29, 13, 0, 0); cal.set(Calendar.MILLISECOND, 0); Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); expected.set(2016, Calendar.AUGUST, 1, 13, 0, 0); expected.set(Calendar.MILLISECOND, 0); assertEquals(expected.getTimeInMillis(), DateTimes.getNextOccurrence(cal.getTimeInMillis(), DayOfWeek.MONDAY, 13, 0)); }