@Override public double calculateYearFraction(LocalDate firstDate, LocalDate secondDate, ScheduleInfo scheduleInfo) { return daysBetween(firstDate, secondDate) / 365.25d; }
@Override public LocalDate next() { LocalDate result = current; current = plusDays(current, 1); return result; }
/** * 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); }
@Override public double calculateYearFraction(LocalDate firstDate, LocalDate secondDate, ScheduleInfo scheduleInfo) { int y1 = firstDate.getYear(); int y2 = secondDate.getYear(); double firstYearLength = firstDate.lengthOfYear(); if (y1 == y2) { double actualDays = doy(secondDate) - doy(firstDate); return actualDays / firstYearLength; } double firstRemainderOfYear = firstYearLength - doy(firstDate) + 1; double secondRemainderOfYear = doy(secondDate) - 1; double secondYearLength = secondDate.lengthOfYear(); return firstRemainderOfYear / firstYearLength + secondRemainderOfYear / secondYearLength + (y2 - y1 - 1); }
/** * 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); }
/** * Returns the number of days between two dates. * <p> * Faster than the JDK method. * * @param firstDate the first date * @param secondDate the second date, after the first * @return the new date */ static long daysBetween(LocalDate firstDate, LocalDate secondDate) { int firstYear = firstDate.getYear(); int secondYear = secondDate.getYear(); if (firstYear == secondYear) { return doy(secondDate) - doy(firstDate); } if ((firstYear + 1) == secondYear) { return (firstDate.lengthOfYear() - doy(firstDate)) + doy(secondDate); } return secondDate.toEpochDay() - firstDate.toEpochDay(); }
@Override public double calculateYearFraction(LocalDate firstDate, LocalDate secondDate, ScheduleInfo scheduleInfo) { return daysBetween(firstDate, secondDate) / 364d; }
@Override public LocalDate shift(LocalDate date, int amount) { return plusDays(date, amount); }
/** * 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_dayOfYear() { LocalDate date = LocalDate.of(2012, 1, 1); for (int i = 0; i < 366 * 4; i++) { assertEquals(LocalDateUtils.doy(date), date.getDayOfYear()); date = date.plusDays(1); } }
@Override public double calculateYearFraction(LocalDate firstDate, LocalDate secondDate, ScheduleInfo scheduleInfo) { return daysBetween(firstDate, secondDate) / 360d; }
@Override public LocalDate next(LocalDate date) { return plusDays(date, 1); }
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]"); }
@Override public double calculateYearFraction(LocalDate firstDate, LocalDate secondDate, ScheduleInfo scheduleInfo) { return daysBetween(firstDate, secondDate) / 365d; }
@Override public LocalDate previous(LocalDate date) { return plusDays(date, -1); }
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]"); }
@Override public int calculateDays(LocalDate firstDate, LocalDate secondDate) { long actualDays = daysBetween(firstDate, secondDate); return toIntExact(actualDays); } },
/** * Finds the next date in the sequence, always returning a date later than the input date. * <p> * Given an input date, this method returns the next date after it from the sequence. * * @param date the input date * @return the next sequence date after the input date * @throws IllegalArgumentException if there are no more sequence dates */ public default LocalDate next(LocalDate date) { LocalDate next = plusDays(date, 1); return nextOrSame(next); }
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]"); }
@Override public int calculateDays(LocalDate firstDate, LocalDate secondDate) { long actualDays = daysBetween(firstDate, secondDate); return toIntExact(actualDays); } },