Class HolidayUtil
java.lang.Object
cloud.opencode.base.date.holiday.HolidayUtil
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 Summary
Modifier and TypeMethodDescriptionstatic HolidayProviderGets the default holiday provider 获取默认节假日提供者getHoliday(LocalDate date) Gets the holiday for a specific date 获取指定日期的节假日getHolidays(int year) Gets all holidays for a specific year 获取指定年份的所有节假日getHolidays(LocalDate start, LocalDate end) Gets holidays in a date range 获取日期范围内的节假日static Optional<HolidayProvider> getProvider(String countryCode) Gets a provider by country code 按国家代码获取提供者static booleanisAdjustedWorkday(LocalDate date) Checks if a date is an adjusted workday (补班日) 检查日期是否为调休工作日(补班日)static booleanChecks if a date is a holiday 检查日期是否为节假日static booleanisHoliday(LocalDate date, HolidayProvider provider) Checks if a date is a holiday using specified provider 使用指定提供者检查日期是否为节假日static booleanChecks if a date is a weekend (Saturday or Sunday) 检查日期是否为周末(星期六或星期日)static booleanChecks if a date is a workday 检查日期是否为工作日static booleanisWorkday(LocalDate date, HolidayProvider provider) Checks if a date is a workday using specified provider 使用指定提供者检查日期是否为工作日static LocalDateminusWorkdays(LocalDate date, int workdays) Subtracts workdays from a date 从日期减去工作日static LocalDatenextOrSameWorkday(LocalDate date) Gets the next workday on or after the given date 获取给定日期当天或之后的下一个工作日static LocalDatenextWorkday(LocalDate date) Gets the next workday after the given date 获取给定日期之后的下一个工作日static LocalDateplusWorkdays(LocalDate date, int workdays) Adds workdays to a date 向日期添加工作日static LocalDatepreviousWorkday(LocalDate date) Gets the previous workday before the given date 获取给定日期之前的上一个工作日static voidregisterProvider(HolidayProvider provider) Registers a holiday provider 注册节假日提供者static voidsetDefaultProvider(HolidayProvider provider) Sets the default holiday provider 设置默认节假日提供者static longworkdaysBetween(LocalDate start, LocalDate end) Calculates the number of workdays between two dates 计算两个日期之间的工作日数
-
Method Details
-
setDefaultProvider
Sets the default holiday provider 设置默认节假日提供者- Parameters:
provider- the provider | 提供者
-
getDefaultProvider
Gets the default holiday provider 获取默认节假日提供者- Returns:
- the default provider | 默认提供者
-
registerProvider
Registers a holiday provider 注册节假日提供者- Parameters:
provider- the provider | 提供者
-
getProvider
Gets a provider by country code 按国家代码获取提供者- Parameters:
countryCode- the country code | 国家代码- Returns:
- the provider, or empty if not found | 提供者,如果未找到则为空
-
isHoliday
Checks if a date is a holiday 检查日期是否为节假日- Parameters:
date- the date to check | 要检查的日期- Returns:
- true if it's a holiday | 如果是节假日返回true
-
isHoliday
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
-
isWorkday
Checks if a date is a workday 检查日期是否为工作日- Parameters:
date- the date to check | 要检查的日期- Returns:
- true if it's a workday | 如果是工作日返回true
-
isWorkday
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
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
Checks if a date is an adjusted workday (补班日) 检查日期是否为调休工作日(补班日)- Parameters:
date- the date to check | 要检查的日期- Returns:
- true if it's an adjusted workday | 如果是调休工作日返回true
-
plusWorkdays
-
minusWorkdays
-
workdaysBetween
-
nextWorkday
-
previousWorkday
-
nextOrSameWorkday
-
getHolidays
-
getHolidays
-