Class Interval
java.lang.Object
cloud.opencode.base.date.extra.Interval
- All Implemented Interfaces:
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 TypeMethodDescriptionbooleanChecks if this interval abuts (is adjacent to) the specified interval 检查此区间是否与指定区间相邻booleanChecks if this interval contains the specified instant 检查此区间是否包含指定时刻static IntervalCreates an empty Interval at the specified instant 在指定时刻创建空IntervalbooleanChecks if this interval encloses (fully contains) the specified interval 检查此区间是否包围(完全包含)指定区间booleanExpands this interval by the specified duration on both ends 在两端扩展此区间指定的时长Gets the gap between this interval and the specified interval 获取此区间与指定区间之间的间隙getEnd()Gets the end instant (exclusive) 获取结束时刻(不包含)getStart()Gets the start instant (inclusive) 获取起始时刻(包含)inthashCode()intersection(Interval other) Gets the intersection of this interval with the specified interval 获取此区间与指定区间的交集booleanChecks if this interval is after the specified interval 检查此区间是否在指定区间之后booleanChecks if this interval is after the specified instant 检查此区间是否在指定时刻之后booleanChecks if this interval is before the specified interval 检查此区间是否在指定区间之前booleanChecks if this interval is before the specified instant 检查此区间是否在指定时刻之前booleanisEmpty()Checks if this interval is empty (start equals end) 检查此区间是否为空(起始等于结束)static IntervalCreates an Interval from end Instant and Duration 从结束Instant和时长创建Intervalstatic IntervalCreates an Interval from start Instant and Duration 从起始Instant和时长创建Intervalstatic IntervalCreates an Interval from start and end Instants 从起始和结束Instant创建IntervalbooleanChecks if this interval overlaps with the specified interval 检查此区间是否与指定区间重叠static Intervalparse(CharSequence text) Parses an ISO 8601 interval string 解析ISO 8601区间字符串Gets the duration of this interval 获取此区间的时长toLocalDateRange(ZoneId zone) Converts this interval to a LocalDateRange using the specified zone 使用指定时区将此区间转换为LocalDateRangetoLocalDateTimeRange(ZoneId zone) Converts this interval to a LocalDateTimeRange using the specified zone 使用指定时区将此区间转换为LocalDateTimeRangetoString()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 创建具有不同起始的新区间
-
Method Details
-
of
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
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
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
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
-
getStart
Gets the start instant (inclusive) 获取起始时刻(包含)- Returns:
- the start instant | 起始时刻
-
getEnd
Gets the end instant (exclusive) 获取结束时刻(不包含)- Returns:
- the end instant | 结束时刻
-
toDuration
-
isEmpty
public boolean isEmpty()Checks if this interval is empty (start equals end) 检查此区间是否为空(起始等于结束)- Returns:
- true if empty | 如果为空返回true
-
contains
Checks if this interval contains the specified instant 检查此区间是否包含指定时刻- Parameters:
instant- the instant to check | 要检查的时刻- Returns:
- true if contained | 如果包含返回true
-
encloses
Checks if this interval encloses (fully contains) the specified interval 检查此区间是否包围(完全包含)指定区间- Parameters:
other- the other interval | 另一个区间- Returns:
- true if enclosed | 如果包围返回true
-
overlaps
Checks if this interval overlaps with the specified interval 检查此区间是否与指定区间重叠- Parameters:
other- the other interval | 另一个区间- Returns:
- true if overlapping | 如果重叠返回true
-
abuts
Checks if this interval abuts (is adjacent to) the specified interval 检查此区间是否与指定区间相邻- Parameters:
other- the other interval | 另一个区间- Returns:
- true if abutting | 如果相邻返回true
-
isBefore
Checks if this interval is before the specified interval 检查此区间是否在指定区间之前- Parameters:
other- the other interval | 另一个区间- Returns:
- true if before | 如果在之前返回true
-
isBefore
Checks if this interval is before the specified instant 检查此区间是否在指定时刻之前- Parameters:
instant- the instant | 时刻- Returns:
- true if before | 如果在之前返回true
-
isAfter
Checks if this interval is after the specified interval 检查此区间是否在指定区间之后- Parameters:
other- the other interval | 另一个区间- Returns:
- true if after | 如果在之后返回true
-
isAfter
Checks if this interval is after the specified instant 检查此区间是否在指定时刻之后- Parameters:
instant- the instant | 时刻- Returns:
- true if after | 如果在之后返回true
-
intersection
-
union
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
-
withStart
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
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
-
toLocalDateRange
Converts this interval to a LocalDateRange using the specified zone 使用指定时区将此区间转换为LocalDateRange- Parameters:
zone- the zone | 时区- Returns:
- the LocalDateRange | LocalDateRange
-
toLocalDateTimeRange
Converts this interval to a LocalDateTimeRange using the specified zone 使用指定时区将此区间转换为LocalDateTimeRange- Parameters:
zone- the zone | 时区- Returns:
- the LocalDateTimeRange | LocalDateTimeRange
-
equals
-
hashCode
-
toString
-