Class LocalDateTimeRange

java.lang.Object
cloud.opencode.base.date.extra.LocalDateTimeRange
All Implemented Interfaces:
Serializable

public final class LocalDateTimeRange extends Object implements Serializable
DateTime Range representing a span of LocalDateTimes 日期时间范围,表示LocalDateTime的跨度

This class represents a range of date-times from a start to an end (end exclusive). It provides operations for containment, overlap, intersection, and conversion to other range types.

此类表示从起始到结束(结束不包含)的日期时间范围。 提供包含、重叠、交集和转换为其他范围类型的操作。

Features | 主要功能:

  • Create from start/end LocalDateTimes - 从起始/结束LocalDateTime创建
  • Create from start and Duration - 从起始和时长创建
  • Containment and overlap checking - 包含和重叠检查
  • Convert to LocalDateRange or Interval - 转换为日期范围或Interval

Usage Examples | 使用示例:

// Create LocalDateTimeRange
LocalDateTimeRange range = LocalDateTimeRange.of(
    LocalDateTime.of(2024, 1, 1, 9, 0),
    LocalDateTime.of(2024, 1, 1, 17, 0)
);

// Create from duration
LocalDateTimeRange range2 = LocalDateTimeRange.of(
    LocalDateTime.now(),
    Duration.ofHours(2)
);

// Check containment
boolean contains = range.contains(LocalDateTime.of(2024, 1, 1, 12, 0));

// Get duration
Duration duration = range.toDuration();

// Convert to Interval
Interval interval = range.toInterval(ZoneId.systemDefault());

Performance | 性能特性:

  • Immutable and thread-safe - 不可变且线程安全
  • All operations are O(1) - 所有操作都是O(1)

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 LocalDateTimeRange of(LocalDateTime start, LocalDateTime end)
      Creates a LocalDateTimeRange from start and end DateTimes 从起始和结束日期时间创建LocalDateTimeRange
      Parameters:
      start - the start datetime (inclusive) | 起始日期时间(包含)
      end - the end datetime (exclusive) | 结束日期时间(不包含)
      Returns:
      the LocalDateTimeRange instance | LocalDateTimeRange实例
      Throws:
      NullPointerException - if start or end is null | 如果起始或结束为null则抛出异常
      OpenDateException - if start is after end | 如果起始在结束之后则抛出异常
    • of

      public static LocalDateTimeRange of(LocalDateTime start, Duration duration)
      Creates a LocalDateTimeRange from start DateTime and Duration 从起始日期时间和时长创建LocalDateTimeRange
      Parameters:
      start - the start datetime (inclusive) | 起始日期时间(包含)
      duration - the duration | 时长
      Returns:
      the LocalDateTimeRange instance | LocalDateTimeRange实例
      Throws:
      NullPointerException - if start or duration is null | 如果起始或时长为null则抛出异常
      OpenDateException - if duration is negative | 如果时长为负则抛出异常
    • ofDay

      public static LocalDateTimeRange ofDay(LocalDate date)
      Creates a LocalDateTimeRange for an entire day 创建整天的LocalDateTimeRange
      Parameters:
      date - the date | 日期
      Returns:
      the LocalDateTimeRange covering the entire day | 覆盖整天的LocalDateTimeRange
    • empty

      public static LocalDateTimeRange empty(LocalDateTime dateTime)
      Creates an empty LocalDateTimeRange at the specified datetime 在指定日期时间创建空的LocalDateTimeRange
      Parameters:
      dateTime - the datetime | 日期时间
      Returns:
      an empty range | 空范围
    • parse

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

      Supported format: "2024-01-01T09:00:00/2024-01-01T17:00:00"

      支持的格式:"2024-01-01T09:00:00/2024-01-01T17:00:00"

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

      public LocalDateTime getStart()
      Gets the start datetime (inclusive) 获取起始日期时间(包含)
      Returns:
      the start datetime | 起始日期时间
    • getEnd

      public LocalDateTime getEnd()
      Gets the end datetime (exclusive) 获取结束日期时间(不包含)
      Returns:
      the end datetime | 结束日期时间
    • toDuration

      public Duration toDuration()
      Gets the duration of this range 获取此范围的时长
      Returns:
      the duration | 时长
    • toHours

      public long toHours()
      Gets the length in hours 获取小时数
      Returns:
      the number of hours | 小时数
    • toMinutes

      public long toMinutes()
      Gets the length in minutes 获取分钟数
      Returns:
      the number of minutes | 分钟数
    • toSeconds

      public long toSeconds()
      Gets the length in seconds 获取秒数
      Returns:
      the number of seconds | 秒数
    • isEmpty

      public boolean isEmpty()
      Checks if this range is empty (start equals end) 检查此范围是否为空(起始等于结束)
      Returns:
      true if empty | 如果为空返回true
    • contains

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

      public boolean encloses(LocalDateTimeRange 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(LocalDateTimeRange other)
      Checks if this range overlaps with the specified range 检查此范围是否与指定范围重叠
      Parameters:
      other - the other range | 另一个范围
      Returns:
      true if overlapping | 如果重叠返回true
    • abuts

      public boolean abuts(LocalDateTimeRange other)
      Checks if this range abuts (is adjacent to) the specified range 检查此范围是否与指定范围相邻
      Parameters:
      other - the other range | 另一个范围
      Returns:
      true if abutting | 如果相邻返回true
    • isBefore

      public boolean isBefore(LocalDateTimeRange other)
      Checks if this range is before the specified range 检查此范围是否在指定范围之前
      Parameters:
      other - the other range | 另一个范围
      Returns:
      true if before | 如果在之前返回true
    • isAfter

      public boolean isAfter(LocalDateTimeRange other)
      Checks if this range is after the specified range 检查此范围是否在指定范围之后
      Parameters:
      other - the other range | 另一个范围
      Returns:
      true if after | 如果在之后返回true
    • intersection

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

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

      The ranges must overlap or abut.

      范围必须重叠或相邻。

      Parameters:
      other - the other range | 另一个范围
      Returns:
      the union | 并集
      Throws:
      OpenDateException - if the ranges do not overlap or abut | 如果范围不重叠且不相邻则抛出异常
    • gap

      Gets the gap between this range and the specified range 获取此范围与指定范围之间的间隙
      Parameters:
      other - the other range | 另一个范围
      Returns:
      the gap, or empty if overlapping or abutting | 间隙,如果重叠或相邻则为空
    • withStart

      public LocalDateTimeRange withStart(LocalDateTime start)
      Creates a new range with a different start 创建具有不同起始的新范围
      Parameters:
      start - the new start | 新的起始
      Returns:
      a new LocalDateTimeRange | 新的LocalDateTimeRange
      Throws:
      OpenDateException - if the new start is after the end | 如果新起始在结束之后则抛出异常
    • withEnd

      public LocalDateTimeRange withEnd(LocalDateTime end)
      Creates a new range with a different end 创建具有不同结束的新范围
      Parameters:
      end - the new end | 新的结束
      Returns:
      a new LocalDateTimeRange | 新的LocalDateTimeRange
      Throws:
      OpenDateException - if the start is after the new end | 如果起始在新结束之后则抛出异常
    • toLocalDateRange

      public LocalDateRange toLocalDateRange()
      Converts this range to a LocalDateRange 将此范围转换为LocalDateRange
      Returns:
      the LocalDateRange | LocalDateRange
    • toInterval

      public Interval toInterval(ZoneId zone)
      Converts this range to an Interval using the specified zone 使用指定时区将此范围转换为Interval
      Parameters:
      zone - the zone | 时区
      Returns:
      the Interval | Interval
    • 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