Interface Calendar

All Known Implementing Classes:
CompositeCalendar, ConfigurableCalendar, FinancialCalendars

public interface Calendar
A calendar that handles date calculations, taking bank Holidays into account. Days that are not bank holidays are called business days.

Subclasses of this interface are expected to be immutable and thread-safe.

Predefined calendars are available in FinancialCalendars.

Usage :

 // Build a new calendar
 Calendar calendar = new ConfigurableCalendar(
     SATURDAY, SUNDAY, NEW_YEAR_DAY,
     new MonthDayHoliday(JULY, 14),
     new RelativeHoliday(WesternEaster.INSTANCE, 1) // easter monday
 );

 // Check holidays
 Assertion.assertTrue(calendar.isHoliday(LocalDate.of(2022, 1, 1))); // Saturday and New Year's Day
 Assertion.assertTrue(calendar.isHoliday(LocalDate.of(2022, 1, 2))); // Sunday
 Assertion.assertTrue(calendar.isHoliday(LocalDate.of(2022, 7, 14))); // Bastille day in France

 // Not an holiday
 Assertion.assertFalse(calendar.isHoliday(LocalDate.of(2022, 1, 11)));
 Assertion.assertTrue(calendar.isBusinessDay(LocalDate.of(2022, 1, 11)));

 // Get business days
 Assertion.assertEquals(LocalDate.of(2022, 1, 3), calendar.next(LocalDate.of(2022, 1, 1)));
 Assertion.assertEquals(
     Arrays.asList(LocalDate.of(2022, 1, 3), LocalDate.of(2022, 1, 4)),
     calendar.businessDaysWithin(LocalDate.of(2022, 1, 1), LocalDate.of(2022, 1, 4)));
 
Since:
2.1.0
See Also:
  • Field Details

    • MAX_ITERATIONS

      static final int MAX_ITERATIONS
      Maximum number of iteration for date calculations before giving up.
      See Also:
  • Method Details

    • isHoliday

      boolean isHoliday(LocalDate date)
      Check whether the given date is a public holiday.
      Parameters:
      date - a non-null date.
      Returns:
      true if the given date is a holiday, false otherwise.
    • getHolidaysFor

      Set<Holiday> getHolidaysFor(LocalDate date)
      Get all the Holidays matching with the given day.
      Parameters:
      date - a non-null and unmodifiable set of Holidays.
      Returns:
      true if the given date is a holiday, false otherwise.
    • isBusinessDay

      default boolean isBusinessDay(LocalDate date)
      Check whether the given date is a business day.
      Parameters:
      date - a non-null date.
      Returns:
      true if the given date is a business day, false otherwise.
    • shift

      default LocalDate shift(LocalDate date, int numberOfDays)
      Shifts the given date by the specified number of business days. If the given amount is
      • zero, the input date is returned,
      • positive, later business days are chosen
      • negative, earlier business days are chosen
      Parameters:
      date - the date to shift
      numberOfDays - the number of business days to adjust by
      Returns:
      a non-null date
      Throws:
      DateCalculationException - if no business day could be found in a reasonable time
    • previous

      default LocalDate previous(LocalDate date)
      Compute the previous business day before the given date (excluded).
      Parameters:
      date - a non-null date
      Returns:
      a non-null date
      Throws:
      DateCalculationException - if no business day could be found in a reasonable time
    • previousOrSame

      default LocalDate previousOrSame(LocalDate date)
      Compute the previous business day before the given date (included).
      Parameters:
      date - a non-null date
      Returns:
      a non-null date
      Throws:
      DateCalculationException - if no business day could be found in a reasonable time
    • next

      default LocalDate next(LocalDate date)
      Compute the next business day after the given date (excluded).
      Parameters:
      date - a non-null date
      Returns:
      a non-null date
      Throws:
      DateCalculationException - if no business day could be found in a reasonable time
    • nextOrSame

      default LocalDate nextOrSame(LocalDate date)
      Compute the next business day after the given date (included).
      Parameters:
      date - a non-null date
      Returns:
      a non-null date
      Throws:
      DateCalculationException - if no business day could be found in a reasonable time
    • holidaysWithin

      default List<LocalDate> holidaysWithin(LocalDate from, LocalDate to)
      Compute the holidays between from (inclusive) and to (inclusive).
      Parameters:
      from - a non-null date
      to - a non-null date
      Returns:
      a non-null and unmodifiable ordered list of dates
      Throws:
      IllegalArgumentException - if from is after to
    • businessDaysWithin

      default List<LocalDate> businessDaysWithin(LocalDate from, LocalDate to)
      Compute the business days between from (inclusive) and to (inclusive).
      Parameters:
      from - a non-null date
      to - a non-null date
      Returns:
      a non-null and unmodifiable ordered list of dates
      Throws:
      IllegalArgumentException - if from is after to