@Test public void shouldGetNextApplicableDateIncludingFromDateBasedOnApplicableWeekDays() { DateTime oct1Sat2011 = new DateTime(2011, 10, 1, 0, 0); DateTime oct2Sun2011 = new DateTime(2011, 10, 2, 2, 0); DateTime oct3Mon2011 = new DateTime(2011, 10, 3, 0, 0); DateTime oct4Tue2011 = new DateTime(2011, 10, 4, 5, 0); DateTime oct6Thu2011 = new DateTime(2011, 10, 6, 3, 0); List<DayOfWeek> applicableDays = asList(Monday, Wednesday, DayOfWeek.Friday); assertThat(nextApplicableWeekDayIncludingFromDate(oct1Sat2011, applicableDays), is(oct1Sat2011.dayOfYear().addToCopy(2))); assertThat(nextApplicableWeekDayIncludingFromDate(oct2Sun2011, applicableDays), is(oct2Sun2011.dayOfYear().addToCopy(1))); assertThat(nextApplicableWeekDayIncludingFromDate(oct3Mon2011, applicableDays), is(oct3Mon2011)); assertThat(nextApplicableWeekDayIncludingFromDate(oct4Tue2011, applicableDays), is(oct4Tue2011.dayOfYear().addToCopy(1))); assertThat(nextApplicableWeekDayIncludingFromDate(oct6Thu2011, applicableDays), is(oct6Thu2011.dayOfYear().addToCopy(1))); }
public static DateTime nextApplicableWeekDayIncludingFromDate(DateTime fromDate, List<DayOfWeek> applicableDays) { if (CollectionUtils.isEmpty(applicableDays)) { throw new IllegalArgumentException("Applicable Days should not be empty"); } int dayOfWeek = fromDate.getDayOfWeek(); int noOfDaysToNearestCycleDate = 0; int weekMaxDay = DayOfWeek.Sunday.getValue(); int currentDayOfWeek = dayOfWeek; for (int dayCount = 0; dayCount <= weekMaxDay; dayCount++) { if (applicableDays.contains(getDayOfWeek(currentDayOfWeek))) { noOfDaysToNearestCycleDate = dayCount; break; } if (currentDayOfWeek == weekMaxDay) { currentDayOfWeek = 1; } else { currentDayOfWeek++; } } return fromDate.dayOfMonth().addToCopy(noOfDaysToNearestCycleDate); }
DateUtil { public static DateTime nextApplicableWeekDayIncludingFromDate(DateTime fromDate, List<DayOfWeek> applicableDays) { if (CollectionUtils.isEmpty(applicableDays)) { throw new IllegalArgumentException("Applicable Days should not be empty"); } int dayOfWeek = fromDate.getDayOfWeek(); int noOfDaysToNearestCycleDate = 0; int weekMaxDay = DayOfWeek.Sunday.getValue(); int currentDayOfWeek = dayOfWeek; for (int dayCount = 0; dayCount <= weekMaxDay; dayCount++) { if (applicableDays.contains(getDayOfWeek(currentDayOfWeek))) { noOfDaysToNearestCycleDate = dayCount; break; } if (currentDayOfWeek == weekMaxDay) { currentDayOfWeek = 1; } else { currentDayOfWeek++; } } return fromDate.dayOfMonth().addToCopy(noOfDaysToNearestCycleDate); } }
DateUtil { public static DateTime nextApplicableWeekDayIncludingFromDate(DateTime fromDate, List<DayOfWeek> applicableDays) { if (CollectionUtils.isEmpty(applicableDays)) { throw new IllegalArgumentException("Applicable Days should not be empty"); } int dayOfWeek = fromDate.getDayOfWeek(); int noOfDaysToNearestCycleDate = 0; int weekMaxDay = DayOfWeek.Sunday.getValue(); int currentDayOfWeek = dayOfWeek; for (int dayCount = 0; dayCount <= weekMaxDay; dayCount++) { if (applicableDays.contains(getDayOfWeek(currentDayOfWeek))) { noOfDaysToNearestCycleDate = dayCount; break; } if (currentDayOfWeek == weekMaxDay) { currentDayOfWeek = 1; } else { currentDayOfWeek++; } } return fromDate.dayOfMonth().addToCopy(noOfDaysToNearestCycleDate); } private  DateUtil(); }
DateUtil { public static DateTime nextApplicableWeekDayIncludingFromDate(DateTime fromDate, List<DayOfWeek> applicableDays) { if (CollectionUtils.isEmpty(applicableDays)) { throw new IllegalArgumentException("Applicable Days should not be empty"); } int dayOfWeek = fromDate.getDayOfWeek(); int noOfDaysToNearestCycleDate = 0; int weekMaxDay = DayOfWeek.Sunday.getValue(); int currentDayOfWeek = dayOfWeek; for (int dayCount = 0; dayCount <= weekMaxDay; dayCount++) { if (applicableDays.contains(getDayOfWeek(currentDayOfWeek))) { noOfDaysToNearestCycleDate = dayCount; break; } if (currentDayOfWeek == weekMaxDay) { currentDayOfWeek = 1; } else { currentDayOfWeek++; } } return fromDate.dayOfMonth().addToCopy(noOfDaysToNearestCycleDate); } private  DateUtil(); static DateTime now(); static DateTime nowUTC(); static LocalDate today(); static LocalDate tomorrow(); static LocalDate newDate(int year, int month, int day); static DateTime newDateTime(LocalDate localDate, int hour, int minute, int second); static DateTime setTimeZone(DateTime dateTime); static DateTime setTimeZoneUTC(DateTime dateTime); static DateTime newDateTime(Date date); static DateTime newDateTime(LocalDate date); static Time time(DateTime dateTime); static LocalDate newDate(Date date); static LocalDate newDate(DateTime dateTime); static int daysPast(LocalDate localDate, DayOfWeek dayOfWeek); static boolean isOnOrBefore(DateTime firstDate, DateTime secondDate); static boolean isOnOrAfter(DateTime firstDate, DateTime secondDate); static boolean inRange(DateTime reference, DateTime start, DateTime end); static int daysToCalendarWeekEnd(LocalDate date, int calendarWeekStartDay); static DateTime newDateTime(LocalDate localDate, Time time); static DateTime newDateTime(int year, int month, int day, Time time); static DateTime newDateTime(int year, int month, int day); static DateTime newDateTime(int year, int month, int day, int hour, int minute, int second); static int getDifferenceOfDatesInYears(Date startDate); static DateTime endOfDay(Date dateTime); static DateTime nextApplicableWeekDay(DateTime fromDate, List<DayOfWeek> applicableDays); static DateTime nextApplicableWeekDayIncludingFromDate(DateTime fromDate, List<DayOfWeek> applicableDays); static List<DateTime> greaterThanOrEqualTo(final DateTime date, final List<DateTime> dates); static List<DateTime> lessThan(final DateTime date, final List<DateTime> dates); static List<DayOfWeek> daysStarting(DayOfWeek day, int numberOfDays); static List<DateTime> datesToDateTimes(List<Date> dates); static Date toDate(DateTime dateTime); static DateTime toDateTimeAtStartOfDay(LocalDate localDate); }
DateUtil { public static DateTime nextApplicableWeekDayIncludingFromDate(DateTime fromDate, List<DayOfWeek> applicableDays) { if (CollectionUtils.isEmpty(applicableDays)) { throw new IllegalArgumentException("Applicable Days should not be empty"); } int dayOfWeek = fromDate.getDayOfWeek(); int noOfDaysToNearestCycleDate = 0; int weekMaxDay = DayOfWeek.Sunday.getValue(); int currentDayOfWeek = dayOfWeek; for (int dayCount = 0; dayCount <= weekMaxDay; dayCount++) { if (applicableDays.contains(getDayOfWeek(currentDayOfWeek))) { noOfDaysToNearestCycleDate = dayCount; break; } if (currentDayOfWeek == weekMaxDay) { currentDayOfWeek = 1; } else { currentDayOfWeek++; } } return fromDate.dayOfMonth().addToCopy(noOfDaysToNearestCycleDate); } private  DateUtil(); static DateTime now(); static DateTime nowUTC(); static LocalDate today(); static LocalDate tomorrow(); static LocalDate newDate(int year, int month, int day); static DateTime newDateTime(LocalDate localDate, int hour, int minute, int second); static DateTime setTimeZone(DateTime dateTime); static DateTime setTimeZoneUTC(DateTime dateTime); static DateTime newDateTime(Date date); static DateTime newDateTime(LocalDate date); static Time time(DateTime dateTime); static LocalDate newDate(Date date); static LocalDate newDate(DateTime dateTime); static int daysPast(LocalDate localDate, DayOfWeek dayOfWeek); static boolean isOnOrBefore(DateTime firstDate, DateTime secondDate); static boolean isOnOrAfter(DateTime firstDate, DateTime secondDate); static boolean inRange(DateTime reference, DateTime start, DateTime end); static int daysToCalendarWeekEnd(LocalDate date, int calendarWeekStartDay); static DateTime newDateTime(LocalDate localDate, Time time); static DateTime newDateTime(int year, int month, int day, Time time); static DateTime newDateTime(int year, int month, int day); static DateTime newDateTime(int year, int month, int day, int hour, int minute, int second); static int getDifferenceOfDatesInYears(Date startDate); static DateTime endOfDay(Date dateTime); static DateTime nextApplicableWeekDay(DateTime fromDate, List<DayOfWeek> applicableDays); static DateTime nextApplicableWeekDayIncludingFromDate(DateTime fromDate, List<DayOfWeek> applicableDays); static List<DateTime> greaterThanOrEqualTo(final DateTime date, final List<DateTime> dates); static List<DateTime> lessThan(final DateTime date, final List<DateTime> dates); static List<DayOfWeek> daysStarting(DayOfWeek day, int numberOfDays); static List<DateTime> datesToDateTimes(List<Date> dates); static Date toDate(DateTime dateTime); static DateTime toDateTimeAtStartOfDay(LocalDate localDate);  }