Class BusinessDays

java.lang.Object
cloud.opencode.base.date.between.BusinessDays

public final class BusinessDays extends Object
Utility for calculating business day differences and offsets 工作日差异和偏移量计算工具类

Provides methods to count business days between two dates and to add/subtract business days from a date, with support for custom holidays and weekend definitions.

提供计算两个日期间工作日数量以及从日期加减工作日的方法, 支持自定义假期和周末定义。

Features | 主要功能:

  • Count business days between two dates - 计算两个日期间的工作日数
  • Add/subtract business days from a date - 从日期加减工作日
  • Custom holiday sets - 自定义假期集合
  • Custom weekend day definitions - 自定义周末日定义

Usage Examples | 使用示例:

// Count business days (Mon-Fri, no holidays)
long days = BusinessDays.between(
    LocalDate.of(2026, 4, 6),   // Monday
    LocalDate.of(2026, 4, 13)); // next Monday
// days = 5

// Add 5 business days
LocalDate result = BusinessDays.addBusinessDays(
    LocalDate.of(2026, 4, 6), 5);
// result = 2026-04-13 (skips weekend)

// With holidays
Set<LocalDate> holidays = Set.of(LocalDate.of(2026, 4, 7));
long days2 = BusinessDays.between(
    LocalDate.of(2026, 4, 6),
    LocalDate.of(2026, 4, 13),
    holidays);
// days2 = 4 (Tuesday is a holiday)

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是(无状态)
  • Null-safe: No (throws NullPointerException for null args) - 空值安全: 否(null参数抛异常)
Since:
JDK 25, opencode-base-date V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • between

      public static long between(LocalDate startInclusive, LocalDate endExclusive)
      Counts business days between two dates, excluding weekends (Saturday and Sunday) 计算两个日期之间的工作日数,排除周末(周六和周日)
      Parameters:
      startInclusive - the start date, inclusive | 起始日期(包含)
      endExclusive - the end date, exclusive | 结束日期(不包含)
      Returns:
      the number of business days | 工作日数量
    • between

      public static long between(LocalDate startInclusive, LocalDate endExclusive, Set<LocalDate> holidays)
      Counts business days between two dates, excluding weekends and specified holidays 计算两个日期之间的工作日数,排除周末和指定假期
      Parameters:
      startInclusive - the start date, inclusive | 起始日期(包含)
      endExclusive - the end date, exclusive | 结束日期(不包含)
      holidays - the set of holiday dates to exclude | 要排除的假期日期集合
      Returns:
      the number of business days | 工作日数量
    • between

      public static long between(LocalDate startInclusive, LocalDate endExclusive, Set<LocalDate> holidays, Set<DayOfWeek> weekendDays)
      Counts business days between two dates with custom weekend and holiday definitions 使用自定义周末和假期定义计算两个日期之间的工作日数
      Parameters:
      startInclusive - the start date, inclusive | 起始日期(包含)
      endExclusive - the end date, exclusive | 结束日期(不包含)
      holidays - the set of holiday dates to exclude | 要排除的假期日期集合
      weekendDays - the set of days considered as weekend | 被视为周末的星期几集合
      Returns:
      the number of business days (non-negative) | 工作日数量(非负数)
    • addBusinessDays

      public static LocalDate addBusinessDays(LocalDate date, int days)
      Adds the specified number of business days to a date, skipping weekends 向日期添加指定数量的工作日,跳过周末
      Parameters:
      date - the starting date | 起始日期
      days - the number of business days to add (negative for subtraction) | 要添加的工作日数(负数为减少)
      Returns:
      the resulting date | 结果日期
    • addBusinessDays

      public static LocalDate addBusinessDays(LocalDate date, int days, Set<LocalDate> holidays)
      Adds business days with custom holidays 使用自定义假期添加工作日
      Parameters:
      date - the starting date | 起始日期
      days - the number of business days to add (negative for subtraction) | 要添加的工作日数
      holidays - the set of holiday dates to skip | 要跳过的假期日期集合
      Returns:
      the resulting date | 结果日期
    • addBusinessDays

      public static LocalDate addBusinessDays(LocalDate date, int days, Set<LocalDate> holidays, Set<DayOfWeek> weekendDays)
      Adds business days with custom holidays and weekend definitions 使用自定义假期和周末定义添加工作日
      Parameters:
      date - the starting date | 起始日期
      days - the number of business days to add (negative for subtraction) | 要添加的工作日数
      holidays - the set of holiday dates to skip | 要跳过的假期日期集合
      weekendDays - the set of days considered as weekend | 被视为周末的星期几集合
      Returns:
      the resulting date | 结果日期