Class LocalDateRange

java.lang.Object
cloud.opencode.base.date.extra.LocalDateRange
All Implemented Interfaces:
Serializable, Iterable<LocalDate>

public final class LocalDateRange extends Object implements Iterable<LocalDate>, Serializable
Date Range representing a span of LocalDates 日期范围,表示LocalDate的跨度

This class represents a range of dates from a start date to an end date (inclusive). It is modeled after ThreeTen-Extra's LocalDateRange and provides iteration, streaming, and set operations.

此类表示从起始日期到结束日期(包含)的日期范围。 设计参考ThreeTen-Extra的LocalDateRange,提供迭代、流式处理和集合操作。

Features | 主要功能:

  • Create from start/end dates - 从起始/结束日期创建
  • Iterate over all dates in range - 遍历范围内的所有日期
  • Stream-based operations - 基于流的操作
  • Containment and overlap checking - 包含和重叠检查
  • Split by week/month - 按周/月分割

Usage Examples | 使用示例:

// Create LocalDateRange
LocalDateRange range = LocalDateRange.of(
    LocalDate.of(2024, 1, 1),
    LocalDate.of(2024, 1, 31)
);

// Iterate over dates
for (LocalDate date : range) {
    System.out.println(date);
}

// Stream operations
long weekdays = range.stream()
    .filter(d -> d.getDayOfWeek().getValue() < 6)
    .count();

// Check containment
boolean contains = range.contains(LocalDate.of(2024, 1, 15));

// Split by week
List<LocalDateRange> weeks = range.splitByWeek();

Performance | 性能特性:

  • Immutable and thread-safe - 不可变且线程安全
  • Lazy iteration - 延迟迭代

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Immutable: Yes - 不可变: 是
Since:
JDK 25, opencode-base-date V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

      public static LocalDateRange of(LocalDate start, LocalDate end)
      Creates a LocalDateRange from start and end dates (both inclusive) 从起始和结束日期创建LocalDateRange(两端包含)
      Parameters:
      start - the start date (inclusive) | 起始日期(包含)
      end - the end date (inclusive) | 结束日期(包含)
      Returns:
      the LocalDateRange instance | LocalDateRange实例
      Throws:
      NullPointerException - if start or end is null | 如果起始或结束为null则抛出异常
      OpenDateException - if start is after end | 如果起始在结束之后则抛出异常
    • ofExclusive

      public static LocalDateRange ofExclusive(LocalDate start, LocalDate endExclusive)
      Creates a LocalDateRange from start and end dates (end exclusive) 从起始和结束日期创建LocalDateRange(结束不包含)
      Parameters:
      start - the start date (inclusive) | 起始日期(包含)
      endExclusive - the end date (exclusive) | 结束日期(不包含)
      Returns:
      the LocalDateRange instance | LocalDateRange实例
      Throws:
      NullPointerException - if start or endExclusive is null | 如果起始或结束为null则抛出异常
      OpenDateException - if start is after or equals end | 如果起始在结束之后或相等则抛出异常
    • empty

      public static LocalDateRange empty()
      Creates an empty LocalDateRange 创建空的LocalDateRange
      Returns:
      an empty range | 空范围
    • parse

      public static LocalDateRange parse(CharSequence text)
      Parses a string to LocalDateRange 解析字符串为LocalDateRange

      Supported format: "2024-01-01/2024-01-31"

      支持的格式:"2024-01-01/2024-01-31"

      Parameters:
      text - the text to parse | 要解析的文本
      Returns:
      the parsed LocalDateRange | 解析后的LocalDateRange
      Throws:
      OpenDateException - if the text cannot be parsed | 如果文本无法解析则抛出异常
    • getStart

      public LocalDate getStart()
      Gets the start date (inclusive) 获取起始日期(包含)
      Returns:
      the start date, or null if empty | 起始日期,如果为空则返回null
    • getEnd

      public LocalDate getEnd()
      Gets the end date (inclusive) 获取结束日期(包含)
      Returns:
      the end date, or null if empty | 结束日期,如果为空则返回null
    • lengthInDays

      public long lengthInDays()
      Gets the length in days (inclusive count) 获取天数(包含计数)
      Returns:
      the number of days in this range | 此范围内的天数
    • isEmpty

      public boolean isEmpty()
      Checks if this range is empty 检查此范围是否为空
      Returns:
      true if empty | 如果为空返回true
    • contains

      public boolean contains(LocalDate date)
      Checks if this range contains the specified date 检查此范围是否包含指定日期
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if contained | 如果包含返回true
    • encloses

      public boolean encloses(LocalDateRange other)
      Checks if this range encloses (fully contains) the specified range 检查此范围是否包围(完全包含)指定范围
      Parameters:
      other - the other range | 另一个范围
      Returns:
      true if enclosed | 如果包围返回true
    • overlaps

      public boolean overlaps(LocalDateRange other)
      Checks if this range overlaps with the specified range 检查此范围是否与指定范围重叠
      Parameters:
      other - the other range | 另一个范围
      Returns:
      true if overlapping | 如果重叠返回true
    • isConnected

      public boolean isConnected(LocalDateRange other)
      Checks if this range is connected (adjacent or overlapping) with the specified range 检查此范围是否与指定范围相连(相邻或重叠)
      Parameters:
      other - the other range | 另一个范围
      Returns:
      true if connected | 如果相连返回true
    • intersection

      public Optional<LocalDateRange> intersection(LocalDateRange other)
      Gets the intersection of this range with the specified range 获取此范围与指定范围的交集
      Parameters:
      other - the other range | 另一个范围
      Returns:
      the intersection, or empty if no overlap | 交集,如果无重叠则为空
    • span

      public LocalDateRange span(LocalDateRange other)
      Gets the span (union) of this range with the specified range 获取此范围与指定范围的跨度(并集)

      Unlike union, span does not require the ranges to be connected.

      与并集不同,跨度不要求范围相连。

      Parameters:
      other - the other range | 另一个范围
      Returns:
      the span | 跨度
    • iterator

      public Iterator<LocalDate> iterator()
      Specified by:
      iterator in interface Iterable<LocalDate>
    • stream

      public Stream<LocalDate> stream()
      Returns a stream of all dates in this range 返回此范围内所有日期的流
      Returns:
      the stream of dates | 日期流
    • stream

      public Stream<LocalDate> stream(Period step)
      Returns a stream of dates in this range with the specified step 返回此范围内按指定步长的日期流
      Parameters:
      step - the step period | 步长周期
      Returns:
      the stream of dates | 日期流
    • toList

      public List<LocalDate> toList()
      Converts this range to a list of dates 将此范围转换为日期列表
      Returns:
      the list of dates | 日期列表
    • splitByWeek

      public List<LocalDateRange> splitByWeek()
      Splits this range by week (Monday to Sunday) 按周分割此范围(周一到周日)
      Returns:
      list of weekly ranges | 周范围列表
    • splitByMonth

      public List<LocalDateRange> splitByMonth()
      Splits this range by month 按月分割此范围
      Returns:
      list of monthly ranges | 月范围列表
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object