Class DateStreams

java.lang.Object
cloud.opencode.base.date.DateStreams

public final class DateStreams extends Object
Utility for Creating Streams of Date/Time Objects 日期时间对象流创建工具

This utility class provides factory methods for creating Stream instances that produce sequences of date and time objects. Streams are lazily evaluated, making them efficient for large date ranges.

此工具类提供工厂方法来创建产生日期时间对象序列的 Stream 实例。 流采用惰性求值,因此在处理大日期范围时非常高效。

Features | 主要功能:

  • Day-by-day streams with optional custom step - 逐日流(可自定义步长)
  • Week-by-week streams (Monday-aligned) - 逐周流(周一对齐)
  • Month-by-month streams - 逐月流
  • Hour-by-hour streams - 逐小时流
  • Generic iterate with any Duration step - 使用任意 Duration 步长的通用迭代
  • Weekend and weekday filtered streams - 周末和工作日过滤流

Usage Examples | 使用示例:

// All days in January 2026
DateStreams.days(LocalDate.of(2026, 1, 1), LocalDate.of(2026, 2, 1))
    .forEach(System.out::println);

// Every other day
DateStreams.days(LocalDate.of(2026, 1, 1), LocalDate.of(2026, 2, 1), Period.ofDays(2))
    .forEach(System.out::println);

// All weekends in 2026
DateStreams.weekends(LocalDate.of(2026, 1, 1), LocalDate.of(2027, 1, 1))
    .count();

// Every 30 minutes in a day
DateStreams.iterate(
        LocalDateTime.of(2026, 1, 1, 0, 0),
        LocalDateTime.of(2026, 1, 2, 0, 0),
        Duration.ofMinutes(30))
    .forEach(System.out::println);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes (throws on null) - 空值安全: 是(遇 null 抛异常)
Since:
JDK 25, opencode-base-date V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • days

      public static Stream<LocalDate> days(LocalDate startInclusive, LocalDate endExclusive)
      Creates a stream of consecutive days from start (inclusive) to end (exclusive). 创建从起始日(包含)到结束日(不包含)的连续日期流。
      Parameters:
      startInclusive - the first date (inclusive) | 起始日期(包含)
      endExclusive - the end date (exclusive) | 结束日期(不包含)
      Returns:
      a stream of LocalDate | LocalDate
      Throws:
      NullPointerException - if any argument is null | 如果任何参数为 null 则抛出异常
    • days

      public static Stream<LocalDate> days(LocalDate startInclusive, LocalDate endExclusive, Period step)
      Creates a stream of dates from start (inclusive) to end (exclusive) with a custom period step. 创建从起始日(包含)到结束日(不包含)的日期流,使用自定义周期步长。
      Parameters:
      startInclusive - the first date (inclusive) | 起始日期(包含)
      endExclusive - the end date (exclusive) | 结束日期(不包含)
      step - the period between each date in the stream | 流中每个日期之间的周期
      Returns:
      a stream of LocalDate | LocalDate
      Throws:
      NullPointerException - if any argument is null | 如果任何参数为 null 则抛出异常
      OpenDateException - if the step is zero or negative | 如果步长为零或负值则抛出异常
    • weeks

      public static Stream<LocalDate> weeks(LocalDate startInclusive, LocalDate endExclusive)
      Creates a stream of Monday-aligned week start dates from start (inclusive) to end (exclusive). The first element is the Monday on or after startInclusive; subsequent elements are each successive Monday. 创建从起始日(包含)到结束日(不包含)的周一对齐的每周起始日期流。 第一个元素是 startInclusive 当天或之后的周一;后续元素为每个连续的周一。
      Parameters:
      startInclusive - the first date (inclusive) | 起始日期(包含)
      endExclusive - the end date (exclusive) | 结束日期(不包含)
      Returns:
      a stream of Monday-aligned LocalDate | 周一对齐的 LocalDate
      Throws:
      NullPointerException - if any argument is null | 如果任何参数为 null 则抛出异常
    • months

      public static Stream<YearMonth> months(YearMonth startInclusive, YearMonth endExclusive)
      Creates a stream of consecutive months from start (inclusive) to end (exclusive). 创建从起始月(包含)到结束月(不包含)的连续月份流。
      Parameters:
      startInclusive - the first month (inclusive) | 起始月份(包含)
      endExclusive - the end month (exclusive) | 结束月份(不包含)
      Returns:
      a stream of YearMonth | YearMonth
      Throws:
      NullPointerException - if any argument is null | 如果任何参数为 null 则抛出异常
    • months

      public static Stream<YearMonth> months(LocalDate startInclusive, LocalDate endExclusive)
      Creates a stream of consecutive months derived from a date range. The start and end dates are converted to their respective YearMonth values. 根据日期范围创建连续月份流。起始和结束日期将转换为各自的 YearMonth 值。
      Parameters:
      startInclusive - the first date (inclusive) | 起始日期(包含)
      endExclusive - the end date (exclusive) | 结束日期(不包含)
      Returns:
      a stream of YearMonth | YearMonth
      Throws:
      NullPointerException - if any argument is null | 如果任何参数为 null 则抛出异常
    • hours

      public static Stream<LocalDateTime> hours(LocalDateTime startInclusive, LocalDateTime endExclusive)
      Creates a stream of consecutive hours from start (inclusive) to end (exclusive). 创建从起始时间(包含)到结束时间(不包含)的连续小时流。
      Parameters:
      startInclusive - the first date-time (inclusive) | 起始日期时间(包含)
      endExclusive - the end date-time (exclusive) | 结束日期时间(不包含)
      Returns:
      a stream of LocalDateTime | LocalDateTime
      Throws:
      NullPointerException - if any argument is null | 如果任何参数为 null 则抛出异常
    • iterate

      public static Stream<LocalDateTime> iterate(LocalDateTime startInclusive, LocalDateTime endExclusive, Duration step)
      Creates a stream of date-times from start (inclusive) to end (exclusive) with a custom duration step. 创建从起始时间(包含)到结束时间(不包含)的日期时间流,使用自定义持续时间步长。
      Parameters:
      startInclusive - the first date-time (inclusive) | 起始日期时间(包含)
      endExclusive - the end date-time (exclusive) | 结束日期时间(不包含)
      step - the duration between each element | 每个元素之间的持续时间
      Returns:
      a stream of LocalDateTime | LocalDateTime
      Throws:
      NullPointerException - if any argument is null | 如果任何参数为 null 则抛出异常
      OpenDateException - if the step is zero or negative | 如果步长为零或负值则抛出异常
    • weekends

      public static Stream<LocalDate> weekends(LocalDate startInclusive, LocalDate endExclusive)
      Creates a stream of weekend days (Saturday and Sunday) from start (inclusive) to end (exclusive). 创建从起始日(包含)到结束日(不包含)的周末日期流(周六和周日)。
      Parameters:
      startInclusive - the first date (inclusive) | 起始日期(包含)
      endExclusive - the end date (exclusive) | 结束日期(不包含)
      Returns:
      a stream of weekend LocalDate | 周末 LocalDate
      Throws:
      NullPointerException - if any argument is null | 如果任何参数为 null 则抛出异常
    • weekdays

      public static Stream<LocalDate> weekdays(LocalDate startInclusive, LocalDate endExclusive)
      Creates a stream of weekdays (Monday through Friday) from start (inclusive) to end (exclusive). 创建从起始日(包含)到结束日(不包含)的工作日日期流(周一至周五)。
      Parameters:
      startInclusive - the first date (inclusive) | 起始日期(包含)
      endExclusive - the end date (exclusive) | 结束日期(不包含)
      Returns:
      a stream of weekday LocalDate | 工作日 LocalDate
      Throws:
      NullPointerException - if any argument is null | 如果任何参数为 null 则抛出异常