Class HolidayUtil

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

public final class HolidayUtil extends Object
Utility class for holiday operations 节假日工具类

This class provides static methods for working with holidays, including checking if a date is a holiday, calculating workdays, and more.

此类提供处理节假日的静态方法,包括检查日期是否为节假日、计算工作日等。

Features | 主要功能:

  • Check if date is holiday/workday - 检查日期是否为节假日/工作日
  • Calculate workdays between dates - 计算两个日期之间的工作日
  • Add/subtract workdays - 加减工作日
  • Support for China holidays - 支持中国节假日

Usage Examples | 使用示例:

// Check if date is holiday
boolean isHoliday = HolidayUtil.isHoliday(LocalDate.of(2024, 10, 1));

// Check if date is workday
boolean isWorkday = HolidayUtil.isWorkday(LocalDate.of(2024, 10, 8));

// Add workdays
LocalDate result = HolidayUtil.plusWorkdays(LocalDate.now(), 5);

// Calculate workdays between dates
long workdays = HolidayUtil.workdaysBetween(start, end);

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap for caching) - 线程安全: 是(使用ConcurrentHashMap进行缓存)
  • Null-safe: Yes (with explicit null checks) - 空值安全: 是(有明确的空值检查)

Performance | 性能特性:

  • Time complexity: O(n) for workdaysBetween and plusWorkdays where n=number of days iterated; O(k) for nextWorkday/previousWorkday where k=skip count - 时间复杂度: workdaysBetween 和 plusWorkdays 为 O(n),n 为迭代天数;nextWorkday/previousWorkday 为 O(k)
  • Space complexity: O(1) - holiday lookups use a ConcurrentHashMap cache with O(1) amortized access - 空间复杂度: O(1) - 节假日查找使用 ConcurrentHashMap 缓存,摊销 O(1) 访问
Since:
JDK 25, opencode-base-date V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • setDefaultProvider

      public static void setDefaultProvider(HolidayProvider provider)
      Sets the default holiday provider 设置默认节假日提供者
      Parameters:
      provider - the provider | 提供者
    • getDefaultProvider

      public static HolidayProvider getDefaultProvider()
      Gets the default holiday provider 获取默认节假日提供者
      Returns:
      the default provider | 默认提供者
    • registerProvider

      public static void registerProvider(HolidayProvider provider)
      Registers a holiday provider 注册节假日提供者
      Parameters:
      provider - the provider | 提供者
    • getProvider

      public static Optional<HolidayProvider> getProvider(String countryCode)
      Gets a provider by country code 按国家代码获取提供者
      Parameters:
      countryCode - the country code | 国家代码
      Returns:
      the provider, or empty if not found | 提供者,如果未找到则为空
    • isHoliday

      public static 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
    • isHoliday

      public static boolean isHoliday(LocalDate date, HolidayProvider provider)
      Checks if a date is a holiday using specified provider 使用指定提供者检查日期是否为节假日
      Parameters:
      date - the date to check | 要检查的日期
      provider - the provider | 提供者
      Returns:
      true if it's a holiday | 如果是节假日返回true
    • getHoliday

      public static Optional<Holiday> getHoliday(LocalDate date)
      Gets the holiday for a specific date 获取指定日期的节假日
      Parameters:
      date - the date | 日期
      Returns:
      the holiday, or empty if not a holiday | 节假日,如果不是节假日则为空
    • isWorkday

      public static 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
    • isWorkday

      public static boolean isWorkday(LocalDate date, HolidayProvider provider)
      Checks if a date is a workday using specified provider 使用指定提供者检查日期是否为工作日
      Parameters:
      date - the date to check | 要检查的日期
      provider - the provider | 提供者
      Returns:
      true if it's a workday | 如果是工作日返回true
    • isWeekend

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

      public static boolean isAdjustedWorkday(LocalDate date)
      Checks if a date is an adjusted workday (补班日) 检查日期是否为调休工作日(补班日)
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if it's an adjusted workday | 如果是调休工作日返回true
    • plusWorkdays

      public static LocalDate plusWorkdays(LocalDate date, int workdays)
      Adds workdays to a date 向日期添加工作日
      Parameters:
      date - the start date | 起始日期
      workdays - the number of workdays to add | 要添加的工作日数
      Returns:
      the resulting date | 结果日期
    • minusWorkdays

      public static LocalDate minusWorkdays(LocalDate date, int workdays)
      Subtracts workdays from a date 从日期减去工作日
      Parameters:
      date - the start date | 起始日期
      workdays - the number of workdays to subtract | 要减去的工作日数
      Returns:
      the resulting date | 结果日期
    • workdaysBetween

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

      public static LocalDate nextWorkday(LocalDate date)
      Gets the next workday after the given date 获取给定日期之后的下一个工作日
      Parameters:
      date - the date | 日期
      Returns:
      the next workday | 下一个工作日
    • previousWorkday

      public static LocalDate previousWorkday(LocalDate date)
      Gets the previous workday before the given date 获取给定日期之前的上一个工作日
      Parameters:
      date - the date | 日期
      Returns:
      the previous workday | 上一个工作日
    • nextOrSameWorkday

      public static LocalDate nextOrSameWorkday(LocalDate date)
      Gets the next workday on or after the given date 获取给定日期当天或之后的下一个工作日
      Parameters:
      date - the date | 日期
      Returns:
      the next workday (or same date if it's a workday) | 下一个工作日(如果当天是工作日则返回当天)
    • getHolidays

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

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