Class HolidayCalendar

java.lang.Object
cloud.opencode.base.date.holiday.HolidayCalendar

public final class HolidayCalendar extends Object
A calendar for managing and querying holidays 管理和查询假日的日历

This class provides a comprehensive holiday calendar with support for multiple years, holiday types, and workday calculations.

此类提供全面的假日日历,支持多年份、假日类型和工作日计算。

Features | 主要功能:

  • Holiday storage and retrieval - 假日存储和检索
  • Special workday (makeup day) support - 特殊工作日(调休日)支持
  • Workday calculations - 工作日计算
  • Year and month filtering - 年份和月份过滤

Usage Examples | 使用示例:

HolidayCalendar calendar = HolidayCalendar.builder()
    .addHoliday(Holiday.of(LocalDate.of(2024, 1, 1), "New Year"))
    .addHoliday(Holiday.of(LocalDate.of(2024, 12, 25), "Christmas"))
    .addSpecialWorkday(LocalDate.of(2024, 9, 29)) // Makeup day
    .build();

boolean isHoliday = calendar.isHoliday(LocalDate.of(2024, 1, 1)); // true
boolean isWorkday = calendar.isWorkday(LocalDate.of(2024, 1, 1)); // false

Security | 安全性:

  • Thread-safe: Yes (immutable after construction via builder) - 线程安全: 是(通过构建器构造后不可变)
  • Null-safe: Yes (with explicit null checks) - 空值安全: 是(有明确的空值检查)
Since:
JDK 25, opencode-base-date V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • empty

      public static HolidayCalendar empty()
      Creates an empty calendar 创建空日历
      Returns:
      the empty calendar | 空日历
    • of

      public static HolidayCalendar of(Collection<Holiday> holidays)
      Creates a calendar from a collection of holidays 从假日集合创建日历
      Parameters:
      holidays - the holidays | 假日集合
      Returns:
      the calendar | 日历
    • builder

      public static HolidayCalendar.Builder builder()
      Creates a builder 创建构建器
      Returns:
      the builder | 构建器
    • isHoliday

      public boolean isHoliday(LocalDate date)
      Checks if a date is a holiday 检查日期是否为假日
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if it's a holiday | 如果是假日返回true
    • isPublicHoliday

      public boolean isPublicHoliday(LocalDate date)
      Checks if a date is a public holiday 检查日期是否为公共假日
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if it's a public holiday | 如果是公共假日返回true
    • isSpecialWorkday

      public boolean isSpecialWorkday(LocalDate date)
      Checks if a date is a special workday 检查日期是否为特殊工作日
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if it's a special workday | 如果是特殊工作日返回true
    • isWeekend

      public boolean isWeekend(LocalDate date)
      Checks if a date is a weekend 检查日期是否为周末
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if it's a weekend | 如果是周末返回true
    • isWorkday

      public boolean isWorkday(LocalDate date)
      Checks if a date is a workday 检查日期是否为工作日
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if it's a workday | 如果是工作日返回true
    • getHoliday

      public Holiday getHoliday(LocalDate date)
      Gets the holiday for a date 获取日期的假日
      Parameters:
      date - the date | 日期
      Returns:
      the holiday, or null if not a holiday | 假日,如果不是假日则为null
    • findHoliday

      public Optional<Holiday> findHoliday(LocalDate date)
      Gets the holiday for a date as Optional 获取日期的假日(Optional形式)
      Parameters:
      date - the date | 日期
      Returns:
      the holiday optional | 假日Optional
    • getAllHolidays

      public List<Holiday> getAllHolidays()
      Gets all holidays 获取所有假日
      Returns:
      the list of holidays | 假日列表
    • getHolidays

      public List<Holiday> getHolidays(int year)
      Gets holidays for a specific year 获取特定年份的假日
      Parameters:
      year - the year | 年份
      Returns:
      the list of holidays | 假日列表
    • getHolidays

      public List<Holiday> getHolidays(int year, Month month)
      Gets holidays for a specific year and month 获取特定年份和月份的假日
      Parameters:
      year - the year | 年份
      month - the month | 月份
      Returns:
      the list of holidays | 假日列表
    • getHolidaysByType

      public List<Holiday> getHolidaysByType(Holiday.HolidayType type)
      Gets holidays of a specific type 获取特定类型的假日
      Parameters:
      type - the holiday type | 假日类型
      Returns:
      the list of holidays | 假日列表
    • getHolidaysInRange

      public List<Holiday> getHolidaysInRange(LocalDate start, LocalDate end)
      Gets holidays in a date range 获取日期范围内的假日
      Parameters:
      start - the start date (inclusive) | 开始日期(包含)
      end - the end date (inclusive) | 结束日期(包含)
      Returns:
      the list of holidays | 假日列表
    • nextWorkday

      public LocalDate nextWorkday(LocalDate date)
      Gets the next workday after the specified date 获取指定日期之后的下一个工作日
      Parameters:
      date - the starting date | 开始日期
      Returns:
      the next workday | 下一个工作日
    • previousWorkday

      public LocalDate previousWorkday(LocalDate date)
      Gets the previous workday before the specified date 获取指定日期之前的上一个工作日
      Parameters:
      date - the starting date | 开始日期
      Returns:
      the previous workday | 上一个工作日
    • addWorkdays

      public LocalDate addWorkdays(LocalDate date, int days)
      Adds workdays to a date 向日期添加工作日
      Parameters:
      date - the starting date | 开始日期
      days - the number of workdays to add | 要添加的工作日数
      Returns:
      the resulting date | 结果日期
    • countWorkdays

      public long countWorkdays(LocalDate start, LocalDate end)
      Counts workdays between two dates 计算两个日期之间的工作日数
      Parameters:
      start - the start date (inclusive) | 开始日期(包含)
      end - the end date (exclusive) | 结束日期(不包含)
      Returns:
      the number of workdays | 工作日数
    • getName

      public String getName()
      Gets the calendar name 获取日历名称
      Returns:
      the name | 名称
    • getSpecialWorkdays

      public Set<LocalDate> getSpecialWorkdays()
      Gets all special workdays 获取所有特殊工作日
      Returns:
      the set of special workdays | 特殊工作日集合
    • getWeekendDays

      public Set<DayOfWeek> getWeekendDays()
      Gets the weekend days 获取周末天数
      Returns:
      the weekend days | 周末天数
    • asHolidayPredicate

      public Predicate<LocalDate> asHolidayPredicate()
      Creates a predicate for checking holidays 创建检查假日的谓词
      Returns:
      the predicate | 谓词
    • asWorkdayPredicate

      public Predicate<LocalDate> asWorkdayPredicate()
      Creates a predicate for checking workdays 创建检查工作日的谓词
      Returns:
      the predicate | 谓词
    • toString

      public String toString()
      Overrides:
      toString in class Object