Class WorkdayAdjuster

java.lang.Object
cloud.opencode.base.date.adjuster.WorkdayAdjuster
All Implemented Interfaces:
TemporalAdjuster

public final class WorkdayAdjuster extends Object implements TemporalAdjuster
Temporal adjuster for workday calculations 工作日计算的时间调整器

This class provides adjusters for calculating workdays, taking into account weekends and optionally holidays.

此类提供计算工作日的调整器,考虑周末和可选的假日。

Features | 主要功能:

  • Add/subtract workdays - 添加/减去工作日
  • Find next/previous workday - 查找下一个/上一个工作日
  • Configurable weekend days - 可配置的周末天数
  • Custom holiday support - 自定义假日支持

Usage Examples | 使用示例:

LocalDate date = LocalDate.of(2024, 1, 5); // Friday

// Add 3 workdays
LocalDate after = date.with(WorkdayAdjuster.plusDays(3));
// 2024-01-10 (Wednesday, skips weekend)

// Next workday
LocalDate next = date.with(WorkdayAdjuster.nextWorkday());
// 2024-01-08 (Monday)

// With custom holidays
Set<LocalDate> holidays = Set.of(LocalDate.of(2024, 1, 8));
LocalDate after2 = date.with(WorkdayAdjuster.plusDays(1, holidays));
// 2024-01-09 (Tuesday, skips weekend and holiday)

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
  • 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

    • plusDays

      public static WorkdayAdjuster plusDays(int days)
      Creates an adjuster that adds the specified number of workdays 创建添加指定工作日数的调整器
      Parameters:
      days - the number of workdays to add | 要添加的工作日数
      Returns:
      the adjuster | 调整器
    • plusDays

      public static WorkdayAdjuster plusDays(int days, Set<LocalDate> holidays)
      Creates an adjuster that adds workdays with custom holidays 创建添加工作日并考虑自定义假日的调整器
      Parameters:
      days - the number of workdays to add | 要添加的工作日数
      holidays - the set of holidays | 假日集合
      Returns:
      the adjuster | 调整器
    • plusDays

      public static WorkdayAdjuster plusDays(int days, Predicate<LocalDate> holidayPredicate)
      Creates an adjuster that adds workdays with custom holiday predicate 创建添加工作日并使用自定义假日谓词的调整器
      Parameters:
      days - the number of workdays to add | 要添加的工作日数
      holidayPredicate - predicate to check if a date is a holiday | 检查日期是否为假日的谓词
      Returns:
      the adjuster | 调整器
    • minusDays

      public static WorkdayAdjuster minusDays(int days)
      Creates an adjuster that subtracts the specified number of workdays 创建减去指定工作日数的调整器
      Parameters:
      days - the number of workdays to subtract | 要减去的工作日数
      Returns:
      the adjuster | 调整器
    • nextWorkday

      public static WorkdayAdjuster nextWorkday()
      Creates an adjuster that finds the next workday 创建查找下一个工作日的调整器
      Returns:
      the adjuster | 调整器
    • nextWorkday

      public static WorkdayAdjuster nextWorkday(Set<LocalDate> holidays)
      Creates an adjuster that finds the next workday with custom holidays 创建查找下一个工作日并考虑自定义假日的调整器
      Parameters:
      holidays - the set of holidays | 假日集合
      Returns:
      the adjuster | 调整器
    • previousWorkday

      public static WorkdayAdjuster previousWorkday()
      Creates an adjuster that finds the previous workday 创建查找上一个工作日的调整器
      Returns:
      the adjuster | 调整器
    • nearestWorkday

      public static TemporalAdjuster nearestWorkday()
      Creates an adjuster that finds the nearest workday (or same if already a workday) 创建查找最近工作日的调整器(如果已是工作日则返回原日期)
      Returns:
      the adjuster | 调整器
    • builder

      public static WorkdayAdjuster.Builder builder()
      Creates a builder for customized workday adjusters 创建自定义工作日调整器的构建器
      Returns:
      the builder | 构建器
    • adjustInto

      public Temporal adjustInto(Temporal temporal)
      Specified by:
      adjustInto in interface TemporalAdjuster
    • 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
    • countWorkdays

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