/** * Gets the stream of holidays between the two dates. * <p> * This method will treat weekends as holidays. * If the dates are equal, an empty stream is returned. * If the end is before the start, an exception is thrown. * * @param startInclusive the start date * @param endExclusive the end date * @return the stream of holidays * @throws IllegalArgumentException if either date is outside the supported range */ public default Stream<LocalDate> holidays(LocalDate startInclusive, LocalDate endExclusive) { ArgChecker.inOrderOrEqual(startInclusive, endExclusive, "startInclusive", "endExclusive"); return LocalDateUtils.stream(startInclusive, endExclusive) .filter(this::isHoliday); }
/** * Gets the stream of business days between the two dates. * <p> * This method will treat weekends as holidays. * If the dates are equal, an empty stream is returned. * If the end is before the start, an exception is thrown. * * @param startInclusive the start date * @param endExclusive the end date * @return the stream of business days * @throws IllegalArgumentException if either date is outside the supported range */ public default Stream<LocalDate> businessDays(LocalDate startInclusive, LocalDate endExclusive) { ArgChecker.inOrderOrEqual(startInclusive, endExclusive, "startInclusive", "endExclusive"); return LocalDateUtils.stream(startInclusive, endExclusive) .filter(this::isBusinessDay); }
/** * Calculates the number of business days between two dates. * <p> * This calculates the number of business days within the range. * If the dates are equal, zero is returned. * If the end is before the start, an exception is thrown. * * @param startInclusive the start date * @param endExclusive the end date * @return the total number of business days between the start and end date * @throws IllegalArgumentException if either date is outside the supported range */ public default int daysBetween(LocalDate startInclusive, LocalDate endExclusive) { ArgChecker.inOrderOrEqual(startInclusive, endExclusive, "startInclusive", "endExclusive"); return Math.toIntExact(LocalDateUtils.stream(startInclusive, endExclusive) .filter(this::isBusinessDay) .count()); }
public void test_NO_HOLIDAYS() { HolidayCalendar test = HolidayCalendars.NO_HOLIDAYS; LocalDateUtils.stream(LocalDate.of(2011, 1, 1), LocalDate.of(2015, 1, 31)).forEach(date -> { assertEquals(test.isBusinessDay(date), true); assertEquals(test.isHoliday(date), false); }); assertEquals(test.getName(), "NoHolidays"); assertEquals(test.toString(), "HolidayCalendar[NoHolidays]"); }
public void test_SAT_SUN() { HolidayCalendar test = HolidayCalendars.SAT_SUN; LocalDateUtils.stream(LocalDate.of(2011, 1, 1), LocalDate.of(2015, 1, 31)).forEach(date -> { boolean isBusinessDay = date.getDayOfWeek() != SATURDAY && date.getDayOfWeek() != SUNDAY; assertEquals(test.isBusinessDay(date), isBusinessDay); assertEquals(test.isHoliday(date), !isBusinessDay); }); assertEquals(test.getName(), "Sat/Sun"); assertEquals(test.toString(), "HolidayCalendar[Sat/Sun]"); }
public void test_THU_FRI() { HolidayCalendar test = HolidayCalendars.THU_FRI; LocalDateUtils.stream(LocalDate.of(2011, 1, 1), LocalDate.of(2015, 1, 31)).forEach(date -> { boolean isBusinessDay = date.getDayOfWeek() != THURSDAY && date.getDayOfWeek() != FRIDAY; assertEquals(test.isBusinessDay(date), isBusinessDay); assertEquals(test.isHoliday(date), !isBusinessDay); }); assertEquals(test.getName(), "Thu/Fri"); assertEquals(test.toString(), "HolidayCalendar[Thu/Fri]"); }
public void test_FRI_SAT() { HolidayCalendar test = HolidayCalendars.FRI_SAT; LocalDateUtils.stream(LocalDate.of(2011, 1, 1), LocalDate.of(2015, 1, 31)).forEach(date -> { boolean isBusinessDay = date.getDayOfWeek() != FRIDAY && date.getDayOfWeek() != SATURDAY; assertEquals(test.isBusinessDay(date), isBusinessDay); assertEquals(test.isHoliday(date), !isBusinessDay); }); assertEquals(test.getName(), "Fri/Sat"); assertEquals(test.toString(), "HolidayCalendar[Fri/Sat]"); }