Class Interval

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

public final class Interval extends Object implements Serializable
Time Interval representing a span between two Instants 时间区间,表示两个Instant之间的时间跨度

This class represents an interval of time between two Instants (start and end). It is modeled after ThreeTen-Extra's Interval class and provides operations for containment, overlap, intersection, and union.

此类表示两个Instant(起始和结束)之间的时间区间。 设计参考ThreeTen-Extra的Interval类,提供包含、重叠、交集和并集操作。

Features | 主要功能:

  • Create from start/end Instants - 从起始/结束Instant创建
  • Create from start Instant and Duration - 从起始Instant和时长创建
  • Check containment and overlap - 检查包含和重叠
  • Calculate intersection and union - 计算交集和并集
  • Convert to LocalDateRange/LocalDateTimeRange - 转换为日期/日期时间范围

Usage Examples | 使用示例:

// Create Interval
Instant start = Instant.now();
Instant end = start.plus(Duration.ofHours(2));
Interval interval = Interval.of(start, end);

// Create from duration
Interval interval2 = Interval.of(start, Duration.ofMinutes(30));

// Check containment
boolean contains = interval.contains(start.plusSeconds(60));  // true

// Check overlap
boolean overlaps = interval.overlaps(interval2);  // true

// Get intersection
Optional<Interval> intersection = interval.intersection(interval2);

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

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 Summary

    Modifier and Type
    Method
    Description
    boolean
    abuts(Interval other)
    Checks if this interval abuts (is adjacent to) the specified interval 检查此区间是否与指定区间相邻
    boolean
    contains(Instant instant)
    Checks if this interval contains the specified instant 检查此区间是否包含指定时刻
    static Interval
    empty(Instant instant)
    Creates an empty Interval at the specified instant 在指定时刻创建空Interval
    boolean
    Checks if this interval encloses (fully contains) the specified interval 检查此区间是否包围(完全包含)指定区间
    boolean
     
    expand(Duration duration)
    Expands this interval by the specified duration on both ends 在两端扩展此区间指定的时长
    gap(Interval other)
    Gets the gap between this interval and the specified interval 获取此区间与指定区间之间的间隙
    Gets the end instant (exclusive) 获取结束时刻(不包含)
    Gets the start instant (inclusive) 获取起始时刻(包含)
    int
     
    Gets the intersection of this interval with the specified interval 获取此区间与指定区间的交集
    boolean
    Checks if this interval is after the specified interval 检查此区间是否在指定区间之后
    boolean
    isAfter(Instant instant)
    Checks if this interval is after the specified instant 检查此区间是否在指定时刻之后
    boolean
    Checks if this interval is before the specified interval 检查此区间是否在指定区间之前
    boolean
    isBefore(Instant instant)
    Checks if this interval is before the specified instant 检查此区间是否在指定时刻之前
    boolean
    Checks if this interval is empty (start equals end) 检查此区间是否为空(起始等于结束)
    static Interval
    of(Duration duration, Instant end)
    Creates an Interval from end Instant and Duration 从结束Instant和时长创建Interval
    static Interval
    of(Instant start, Duration duration)
    Creates an Interval from start Instant and Duration 从起始Instant和时长创建Interval
    static Interval
    of(Instant start, Instant end)
    Creates an Interval from start and end Instants 从起始和结束Instant创建Interval
    boolean
    Checks if this interval overlaps with the specified interval 检查此区间是否与指定区间重叠
    static Interval
    Parses an ISO 8601 interval string 解析ISO 8601区间字符串
    Gets the duration of this interval 获取此区间的时长
    Converts this interval to a LocalDateRange using the specified zone 使用指定时区将此区间转换为LocalDateRange
    Converts this interval to a LocalDateTimeRange using the specified zone 使用指定时区将此区间转换为LocalDateTimeRange
     
    union(Interval other)
    Gets the union of this interval with the specified interval 获取此区间与指定区间的并集
    Creates a new interval with a different end 创建具有不同结束的新区间
    Creates a new interval with a different start 创建具有不同起始的新区间

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • of

      public static Interval of(Instant start, Instant end)
      Creates an Interval from start and end Instants 从起始和结束Instant创建Interval
      Parameters:
      start - the start instant (inclusive) | 起始时刻(包含)
      end - the end instant (exclusive) | 结束时刻(不包含)
      Returns:
      the Interval instance | Interval实例
      Throws:
      NullPointerException - if start or end is null | 如果起始或结束为null则抛出异常
      OpenDateException - if start is after end | 如果起始在结束之后则抛出异常
    • of

      public static Interval of(Instant start, Duration duration)
      Creates an Interval from start Instant and Duration 从起始Instant和时长创建Interval
      Parameters:
      start - the start instant (inclusive) | 起始时刻(包含)
      duration - the duration | 时长
      Returns:
      the Interval instance | Interval实例
      Throws:
      NullPointerException - if start or duration is null | 如果起始或时长为null则抛出异常
      OpenDateException - if duration is negative | 如果时长为负则抛出异常
    • of

      public static Interval of(Duration duration, Instant end)
      Creates an Interval from end Instant and Duration 从结束Instant和时长创建Interval
      Parameters:
      duration - the duration | 时长
      end - the end instant (exclusive) | 结束时刻(不包含)
      Returns:
      the Interval instance | Interval实例
      Throws:
      NullPointerException - if duration or end is null | 如果时长或结束为null则抛出异常
      OpenDateException - if duration is negative | 如果时长为负则抛出异常
    • parse

      public static Interval parse(CharSequence text)
      Parses an ISO 8601 interval string 解析ISO 8601区间字符串

      Supported formats:

      • start/end: "2024-01-01T00:00:00Z/2024-01-02T00:00:00Z"
      • start/duration: "2024-01-01T00:00:00Z/PT1H"
      • duration/end: "PT1H/2024-01-01T00:00:00Z"
      Parameters:
      text - the text to parse | 要解析的文本
      Returns:
      the parsed Interval | 解析后的Interval
      Throws:
      OpenDateException - if the text cannot be parsed | 如果文本无法解析则抛出异常
    • empty

      public static Interval empty(Instant instant)
      Creates an empty Interval at the specified instant 在指定时刻创建空Interval
      Parameters:
      instant - the instant | 时刻
      Returns:
      an empty Interval | 空Interval
    • getStart

      public Instant getStart()
      Gets the start instant (inclusive) 获取起始时刻(包含)
      Returns:
      the start instant | 起始时刻
    • getEnd

      public Instant getEnd()
      Gets the end instant (exclusive) 获取结束时刻(不包含)
      Returns:
      the end instant | 结束时刻
    • toDuration

      public Duration toDuration()
      Gets the duration of this interval 获取此区间的时长
      Returns:
      the duration | 时长
    • isEmpty

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

      public boolean contains(Instant instant)
      Checks if this interval contains the specified instant 检查此区间是否包含指定时刻
      Parameters:
      instant - the instant to check | 要检查的时刻
      Returns:
      true if contained | 如果包含返回true
    • encloses

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

      public boolean overlaps(Interval other)
      Checks if this interval overlaps with the specified interval 检查此区间是否与指定区间重叠
      Parameters:
      other - the other interval | 另一个区间
      Returns:
      true if overlapping | 如果重叠返回true
    • abuts

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

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

      public boolean isBefore(Instant instant)
      Checks if this interval is before the specified instant 检查此区间是否在指定时刻之前
      Parameters:
      instant - the instant | 时刻
      Returns:
      true if before | 如果在之前返回true
    • isAfter

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

      public boolean isAfter(Instant instant)
      Checks if this interval is after the specified instant 检查此区间是否在指定时刻之后
      Parameters:
      instant - the instant | 时刻
      Returns:
      true if after | 如果在之后返回true
    • intersection

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

      public Interval union(Interval other)
      Gets the union of this interval with the specified interval 获取此区间与指定区间的并集

      The intervals must overlap or abut.

      区间必须重叠或相邻。

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

      public Optional<Interval> gap(Interval other)
      Gets the gap between this interval and the specified interval 获取此区间与指定区间之间的间隙
      Parameters:
      other - the other interval | 另一个区间
      Returns:
      the gap, or empty if overlapping or abutting | 间隙,如果重叠或相邻则为空
    • withStart

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

      public Interval withEnd(Instant end)
      Creates a new interval with a different end 创建具有不同结束的新区间
      Parameters:
      end - the new end | 新的结束
      Returns:
      a new Interval | 新的Interval
      Throws:
      OpenDateException - if the start is after the new end | 如果起始在新结束之后则抛出异常
    • expand

      public Interval expand(Duration duration)
      Expands this interval by the specified duration on both ends 在两端扩展此区间指定的时长
      Parameters:
      duration - the duration to expand | 要扩展的时长
      Returns:
      a new Interval | 新的Interval
    • toLocalDateRange

      public LocalDateRange toLocalDateRange(ZoneId zone)
      Converts this interval to a LocalDateRange using the specified zone 使用指定时区将此区间转换为LocalDateRange
      Parameters:
      zone - the zone | 时区
      Returns:
      the LocalDateRange | LocalDateRange
    • toLocalDateTimeRange

      public LocalDateTimeRange toLocalDateTimeRange(ZoneId zone)
      Converts this interval to a LocalDateTimeRange using the specified zone 使用指定时区将此区间转换为LocalDateTimeRange
      Parameters:
      zone - the zone | 时区
      Returns:
      the LocalDateTimeRange | LocalDateTimeRange
    • 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