Class ChinaHolidayCalendar

java.lang.Object
cloud.opencode.base.date.holiday.ChinaHolidayCalendar
All Implemented Interfaces:
HolidayProvider

public final class ChinaHolidayCalendar extends Object implements HolidayProvider
China-specific holiday calendar driven by State-Council-issued closed-interval ranges. 由国务院发布的闭区间日期段驱动的中国节假日日历。

Input model:

输入模型:

  • Holiday closed-interval ranges (放假闭区间), e.g. May Day, National Day, Qingming, Mid-Autumn. 一个或多个放假闭区间,例如:五一、十一、清明、中秋。
  • Makeup-workday closed-interval ranges (补班闭区间), where weekends become workdays to compensate the holiday. 一个或多个补班闭区间,将周末调班为工作日。

Resolution rules (in order):

判定规则(按顺序):

  1. Date ∈ any makeup-workday range → workday (overrides weekend). 日期落入补班区间 → 工作日(覆盖周末)。
  2. Date ∈ any holiday range → holiday, not a workday. 日期落入放假区间 → 假日,非工作日。
  3. Otherwise → normal: weekend = non-workday, weekday = workday. 否则按正常处理:周末为非工作日,工作日为工作日。

Usage Examples | 使用示例:

// 2026 Labor Day: 5/1-5/5 holiday, 5/9 (Sat) makeup workday
ChinaHolidayCalendar cal = ChinaHolidayCalendar.builder()
        .addEvent(ChinaHolidayEvent.builder()
                .name("Labor Day").chineseName("劳动节")
                .addHolidayRange(LocalDate.of(2026, 5, 1), LocalDate.of(2026, 5, 5))
                .addMakeupWorkdayRange(LocalDate.of(2026, 5, 9), LocalDate.of(2026, 5, 9))
                .build())
        .build();

cal.isHoliday(LocalDate.of(2026, 5, 1));     // true
cal.isWorkday(LocalDate.of(2026, 5, 1));     // false
cal.isMakeupWorkday(LocalDate.of(2026, 5, 9)); // true
cal.isWorkday(LocalDate.of(2026, 5, 9));     // true (Saturday but makeup)
cal.isWorkday(LocalDate.of(2026, 6, 10));    // true (irrelevant date — normal weekday)
cal.isWorkday(LocalDate.of(2026, 6, 13));    // false (irrelevant date — normal Saturday)

Security | 安全性:

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

    • builder

      public static ChinaHolidayCalendar.Builder builder()
      Creates a builder. 创建构建器。
      Returns:
      a new builder | 新构建器
    • of

      Creates a calendar from a collection of events. 通过事件集合创建日历。
      Parameters:
      events - the events | 事件集合
      Returns:
      the calendar | 日历
    • getEvents

      public List<ChinaHolidayEvent> getEvents()
      Returns the configured events. 返回配置的所有事件。
      Returns:
      immutable list of events | 不可变事件列表
    • classify

      public ChinaHolidayCalendar.DayInfo classify(LocalDate date)
      Classifies a single date into a ChinaHolidayCalendar.DayInfo via algorithm + configured data. 通过算法 + 已配置数据将单个日期分类为 ChinaHolidayCalendar.DayInfo

      Resolution order: makeup-workday range → holiday range → weekend default.

      判定顺序:补班区间 → 放假区间 → 周末默认。

      Parameters:
      date - the date | 日期
      Returns:
      the classification | 分类信息
    • getYearCalendar

      public List<ChinaHolidayCalendar.DayInfo> getYearCalendar(int year)
      Generates a full-year per-day calendar (Jan 1 .. Dec 31, inclusive). 生成整年的逐日日历(1月1日 至 12月31日,闭区间)。
      Parameters:
      year - the year | 年份
      Returns:
      ordered list of ChinaHolidayCalendar.DayInfo | 按日期排序的 ChinaHolidayCalendar.DayInfo 列表
    • getCalendar

      public List<ChinaHolidayCalendar.DayInfo> getCalendar(LocalDate start, LocalDate end)
      Generates a per-day calendar for a closed-interval date range. 生成闭区间日期范围内的逐日日历。
      Parameters:
      start - start date (inclusive) | 起始日期(包含)
      end - end date (inclusive) | 结束日期(包含)
      Returns:
      ordered list of ChinaHolidayCalendar.DayInfo | 按日期排序的 ChinaHolidayCalendar.DayInfo 列表
    • getYearWorkdays

      public Map<LocalDate, ChinaHolidayCalendar.DayInfo> getYearWorkdays(int year)
      Returns a map of every workday in the given year to its classification. 返回指定年份所有工作日及其分类信息的映射。
      Parameters:
      year - the year | 年份
      Returns:
      ordered map of workdays | 按日期排序的工作日映射
    • getYearRestDays

      public Map<LocalDate, ChinaHolidayCalendar.DayInfo> getYearRestDays(int year)
      Returns a map of every rest day in the given year to its classification. 返回指定年份所有休息日及其分类信息的映射。
      Parameters:
      year - the year | 年份
      Returns:
      ordered map of rest days | 按日期排序的休息日映射
    • getMonthCalendar

      public List<ChinaHolidayCalendar.DayInfo> getMonthCalendar(YearMonth yearMonth)
      Generates a per-day calendar for an entire month. 生成整月的逐日日历。
      Parameters:
      yearMonth - the year-month | 年月
      Returns:
      ordered list of ChinaHolidayCalendar.DayInfo | 按日期排序的 ChinaHolidayCalendar.DayInfo 列表
    • getMonthCalendar

      public List<ChinaHolidayCalendar.DayInfo> getMonthCalendar(int year, int month)
      Generates a per-day calendar for an entire month. 生成整月的逐日日历。
      Parameters:
      year - the year | 年份
      month - the month (1-12) | 月份(1-12)
      Returns:
      ordered list of ChinaHolidayCalendar.DayInfo | 按日期排序的 ChinaHolidayCalendar.DayInfo 列表
    • getYearWorkdayDates

      public List<LocalDate> getYearWorkdayDates(int year)
      Lists all workday dates in the given year (in date order). 列出指定年份的所有工作日日期(按日期排序)。
      Parameters:
      year - the year | 年份
      Returns:
      immutable list of workday dates | 不可变工作日日期列表
    • getYearRestDayDates

      public List<LocalDate> getYearRestDayDates(int year)
      Lists all rest day dates in the given year (in date order). 列出指定年份的所有休息日日期(按日期排序)。
      Parameters:
      year - the year | 年份
      Returns:
      immutable list of rest day dates | 不可变休息日日期列表
    • getMonthWorkdayDates

      public List<LocalDate> getMonthWorkdayDates(YearMonth yearMonth)
      Lists all workday dates in the given month (in date order). 列出指定月份的所有工作日日期(按日期排序)。
      Parameters:
      yearMonth - the year-month | 年月
      Returns:
      immutable list of workday dates | 不可变工作日日期列表
    • getMonthWorkdayDates

      public List<LocalDate> getMonthWorkdayDates(int year, int month)
      Lists all workday dates in the given month (in date order). 列出指定月份的所有工作日日期(按日期排序)。
      Parameters:
      year - the year | 年份
      month - the month (1-12) | 月份(1-12)
      Returns:
      immutable list of workday dates | 不可变工作日日期列表
    • getMonthRestDayDates

      public List<LocalDate> getMonthRestDayDates(YearMonth yearMonth)
      Lists all rest day dates in the given month (in date order). 列出指定月份的所有休息日日期(按日期排序)。
      Parameters:
      yearMonth - the year-month | 年月
      Returns:
      immutable list of rest day dates | 不可变休息日日期列表
    • getMonthRestDayDates

      public List<LocalDate> getMonthRestDayDates(int year, int month)
      Lists all rest day dates in the given month (in date order). 列出指定月份的所有休息日日期(按日期排序)。
      Parameters:
      year - the year | 年份
      month - the month (1-12) | 月份(1-12)
      Returns:
      immutable list of rest day dates | 不可变休息日日期列表
    • countWorkdaysInYear

      public int countWorkdaysInYear(int year)
      Counts workdays in the given year. 统计指定年份的工作日天数。
      Parameters:
      year - the year | 年份
      Returns:
      workday count | 工作日天数
    • countRestDaysInYear

      public int countRestDaysInYear(int year)
      Counts rest days in the given year. 统计指定年份的休息日天数。
      Parameters:
      year - the year | 年份
      Returns:
      rest day count | 休息日天数
    • countWorkdaysInMonth

      public int countWorkdaysInMonth(YearMonth yearMonth)
      Counts workdays in the given month. 统计指定月份的工作日天数。
      Parameters:
      yearMonth - the year-month | 年月
      Returns:
      workday count | 工作日天数
    • countWorkdaysInMonth

      public int countWorkdaysInMonth(int year, int month)
      Counts workdays in the given month. 统计指定月份的工作日天数。
      Parameters:
      year - the year | 年份
      month - the month (1-12) | 月份(1-12)
      Returns:
      workday count | 工作日天数
    • countRestDaysInMonth

      public int countRestDaysInMonth(YearMonth yearMonth)
      Counts rest days in the given month. 统计指定月份的休息日天数。
      Parameters:
      yearMonth - the year-month | 年月
      Returns:
      rest day count | 休息日天数
    • countRestDaysInMonth

      public int countRestDaysInMonth(int year, int month)
      Counts rest days in the given month. 统计指定月份的休息日天数。
      Parameters:
      year - the year | 年份
      month - the month (1-12) | 月份(1-12)
      Returns:
      rest day count | 休息日天数
    • getRows

      Outputs a calendar listing for a closed-interval date range with all four columns. 输出闭区间日期范围内的日历清单(包含全部 4 列)。
      Parameters:
      start - start date (inclusive) | 起始日期(包含)
      end - end date (inclusive) | 结束日期(包含)
      Returns:
      list of rows, one per day | 每日一行的清单
    • getRows

      Outputs a calendar listing for a closed-interval date range, with caller-selected columns. 输出闭区间日期范围内的日历清单,由调用方选择需要返回的列。

      Unselected columns are returned as null. Passing null or an empty field set returns all columns.

      未选中的列以 null 返回。传入 null 或空集合等同于全部列。

      Parameters:
      start - start date (inclusive) | 起始日期(包含)
      end - end date (inclusive) | 结束日期(包含)
      fields - columns to populate | 要填充的列
      Returns:
      immutable list of rows | 不可变清单
    • getYearRows

      public List<ChinaHolidayCalendar.CalendarRow> getYearRows(int year)
      Outputs a full-year calendar listing with all four columns. 输出整年日历清单(包含全部 4 列)。
      Parameters:
      year - the year | 年份
      Returns:
      list of rows | 清单
    • getYearRows

      Outputs a full-year calendar listing with caller-selected columns. 输出整年日历清单,由调用方选择列。
      Parameters:
      year - the year | 年份
      fields - columns to populate | 要填充的列
      Returns:
      immutable list of rows | 不可变清单
    • getMonthRows

      public List<ChinaHolidayCalendar.CalendarRow> getMonthRows(YearMonth yearMonth)
      Outputs a month calendar listing with all four columns. 输出整月日历清单(包含全部 4 列)。
      Parameters:
      yearMonth - the year-month | 年月
      Returns:
      list of rows | 清单
    • getMonthRows

      Outputs a month calendar listing with caller-selected columns. 输出整月日历清单,由调用方选择列。
      Parameters:
      yearMonth - the year-month | 年月
      fields - columns to populate | 要填充的列
      Returns:
      immutable list of rows | 不可变清单
    • getMonthRows

      public List<ChinaHolidayCalendar.CalendarRow> getMonthRows(int year, int month)
      Outputs a month calendar listing with all four columns. 输出整月日历清单(包含全部 4 列)。
      Parameters:
      year - the year | 年份
      month - the month (1-12) | 月份(1-12)
      Returns:
      list of rows | 清单
    • getMonthRows

      public List<ChinaHolidayCalendar.CalendarRow> getMonthRows(int year, int month, Set<ChinaHolidayCalendar.CalendarField> fields)
      Outputs a month calendar listing with caller-selected columns. 输出整月日历清单,由调用方选择列。
      Parameters:
      year - the year | 年份
      month - the month (1-12) | 月份(1-12)
      fields - columns to populate | 要填充的列
      Returns:
      immutable list of rows | 不可变清单
    • getWeekRows

      public List<ChinaHolidayCalendar.CalendarRow> getWeekRows(LocalDate dateInWeek)
      Outputs the ISO week (Monday through Sunday) containing the given date, with all four columns. 输出包含指定日期的 ISO 周(周一至周日)日历清单,包含全部 4 列。
      Parameters:
      dateInWeek - any date within the target week | 目标周内的任一日期
      Returns:
      list of 7 rows in date order | 7 行(按日期排序)
    • getWeekRows

      Outputs the ISO week (Monday through Sunday) containing the given date, with caller-selected columns. 输出包含指定日期的 ISO 周(周一至周日)日历清单,由调用方选择列。
      Parameters:
      dateInWeek - any date within the target week | 目标周内的任一日期
      fields - columns to populate | 要填充的列
      Returns:
      immutable list of 7 rows in date order | 不可变 7 行清单(按日期排序)
    • isHoliday

      public boolean isHoliday(LocalDate date)
      Returns true if the date falls in any configured holiday range. 如果日期落在任一已配置的放假区间内,返回 true。
      Specified by:
      isHoliday in interface HolidayProvider
      Parameters:
      date - the date | 日期
      Returns:
      true if it's a holiday | 如果是节假日返回 true
    • isMakeupWorkday

      public boolean isMakeupWorkday(LocalDate date)
      Returns true if the date is a configured makeup workday (补班日). 如果日期为已配置的调休补班日,返回 true。
      Parameters:
      date - the date | 日期
      Returns:
      true if makeup workday | 如果是补班日返回 true
    • isAdjustedWorkday

      public boolean isAdjustedWorkday(LocalDate date)
      Alias for isMakeupWorkday(LocalDate) matching the HolidayProvider contract. 与 HolidayProvider 接口对齐的别名方法。
      Specified by:
      isAdjustedWorkday in interface HolidayProvider
      Parameters:
      date - the date | 日期
      Returns:
      true if adjusted workday | 如果是调休工作日返回 true
    • isWeekend

      public boolean isWeekend(LocalDate date)
      Returns true if the date is Saturday or Sunday. Null-safe (returns false). 当日期为星期六或星期日时返回 true。Null 安全(返回 false)。
      Parameters:
      date - the date (nullable) | 日期(可为 null)
      Returns:
      true if weekend | 如果是周末返回 true
    • isWorkday

      public boolean isWorkday(LocalDate date)
      Returns true if the date is a workday under State-Council holiday rules. Null-safe (returns false). 在国务院假期规则下判断日期是否为工作日。Null 安全(返回 false)。

      Resolution order: makeup-workday range > holiday range > weekend default.

      判定顺序:补班区间 > 放假区间 > 周末默认。

      Specified by:
      isWorkday in interface HolidayProvider
      Parameters:
      date - the date (nullable) | 日期(可为 null)
      Returns:
      true if workday | 如果是工作日返回 true
    • getHoliday

      public Optional<Holiday> getHoliday(LocalDate date)
      Gets the holiday for a date, if any. 获取该日期对应的节假日(若存在)。
      Specified by:
      getHoliday in interface HolidayProvider
      Parameters:
      date - the date | 日期
      Returns:
      optional holiday | 可选节假日
    • getHolidays

      public List<Holiday> getHolidays(int year)
      Gets all holidays for a specific year. 获取指定年份的所有节假日。
      Specified by:
      getHolidays in interface HolidayProvider
      Parameters:
      year - the year | 年份
      Returns:
      list of holidays in date order | 按日期排序的节假日列表
    • getHolidays

      public List<Holiday> getHolidays(LocalDate start, LocalDate end)
      Gets holidays in a closed-interval date range. 获取闭区间日期范围内的节假日。
      Specified by:
      getHolidays in interface HolidayProvider
      Parameters:
      start - start date (inclusive) | 起始日期(包含)
      end - end date (inclusive) | 结束日期(包含)
      Returns:
      list of holidays in date order | 按日期排序的节假日列表
    • getAdjustedWorkdays

      public Set<LocalDate> getAdjustedWorkdays(int year)
      Gets all configured makeup workdays for a specific year. 获取指定年份的所有补班日。
      Specified by:
      getAdjustedWorkdays in interface HolidayProvider
      Parameters:
      year - the year | 年份
      Returns:
      immutable set of makeup workdays | 不可变补班日集合
    • getAllMakeupWorkdays

      public Set<LocalDate> getAllMakeupWorkdays()
      Gets all configured makeup workdays. 获取所有补班日。
      Returns:
      immutable set | 不可变集合
    • getName

      public String getName()
      Description copied from interface: HolidayProvider
      Gets the provider name 获取提供者名称
      Specified by:
      getName in interface HolidayProvider
      Returns:
      the provider name | 提供者名称
    • getCountryCode

      public String getCountryCode()
      Description copied from interface: HolidayProvider
      Gets the country/region code 获取国家/地区代码
      Specified by:
      getCountryCode in interface HolidayProvider
      Returns:
      the country code (e.g., "CN", "US") | 国家代码(如"CN"、"US")
    • getSupportedYearRange

      public int[] getSupportedYearRange()
      Gets the supported year range, computed from the configured events. 获取受支持的年份范围(基于已配置事件计算)。

      If no events are configured, falls back to [1970, 2100]: 1970 = Unix epoch (lower historical bound) and 2100 matches the upper bound used by sibling lunar/holiday data tables in this project. The fallback exists so HolidayProvider.isYearSupported(int) keeps a sensible default on an empty calendar; once any event is added, the range is the actual min/max of holiday + makeup-workday years.

      无配置事件时回退至 [1970, 2100]:1970 为 Unix 纪元下界,2100 与本项目阴历/节假日 数据表上界一致;该回退保证空日历下 HolidayProvider.isYearSupported(int) 仍有合理默认值。一旦添加事件, 实际范围即为放假/补班年份的最小/最大值。

      Specified by:
      getSupportedYearRange in interface HolidayProvider
      Returns:
      [minYear, maxYear] inclusive | 含端点的 [minYear, maxYear]
    • toHolidayCalendar

      public HolidayCalendar toHolidayCalendar()
      Converts this calendar into a generic HolidayCalendar (without country binding). 将此日历转换为通用 HolidayCalendar(不绑定国家代码)。
      Returns:
      the generic holiday calendar | 通用节假日日历
    • toString

      public String toString()
      Overrides:
      toString in class Object