Class LocalTimeRange
java.lang.Object
cloud.opencode.base.date.extra.LocalTimeRange
- All Implemented Interfaces:
Serializable
A time range representing a span between two LocalTime values
表示两个LocalTime值之间跨度的时间范围
This class represents a range of time within a single day. It supports ranges that cross midnight (e.g., 22:00 to 06:00).
此类表示单日内的时间范围。支持跨越午夜的范围(如22:00到06:00)。
Features | 主要功能:
- Time range representation - 时间范围表示
- Midnight crossing support - 跨午夜支持
- Overlap and intersection - 重叠和交集
- Duration calculation - 持续时间计算
Usage Examples | 使用示例:
// Business hours
LocalTimeRange businessHours = LocalTimeRange.of(
LocalTime.of(9, 0),
LocalTime.of(17, 0)
);
// Check if a time is within range
boolean open = businessHours.contains(LocalTime.of(12, 0)); // true
// Night shift (crosses midnight)
LocalTimeRange nightShift = LocalTimeRange.of(
LocalTime.of(22, 0),
LocalTime.of(6, 0)
);
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Null-safe: Yes (with explicit null checks) - 空值安全: 是(有明确的空值检查)
- Since:
- JDK 25, opencode-base-date V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final LocalTimeRangeAfternoon (12:00 - 18:00)static final LocalTimeRangeA range representing the entire daystatic final LocalTimeRangeCommon business hours (9:00 - 17:00)static final LocalTimeRangeEvening (18:00 - 22:00)static final LocalTimeRangeMorning (6:00 - 12:00) -
Method Summary
Modifier and TypeMethodDescriptionbooleancontains(LocalTimeRange other) Checks if this range fully contains another range 检查此范围是否完全包含另一个范围booleanChecks if this range contains the specified time 检查此范围是否包含指定时间booleanChecks if the range crosses midnight 检查范围是否跨越午夜booleanExpands the range by the specified duration on both ends 在两端按指定持续时间扩展范围format(DateTimeFormatter formatter) Formats the range using the specified formatter 使用指定的格式化器格式化范围Formats as a compact string (HH:mm-HH:mm) 格式化为紧凑字符串Gets the duration of this range 获取此范围的持续时间getEnd()Gets the end time 获取结束时间longgetHours()Gets the duration in hours 获取小时数longGets the duration in minutes 获取分钟数getStart()Gets the start time 获取开始时间inthashCode()intersection(LocalTimeRange other) Gets the intersection with another range 获取与另一个范围的交集static LocalTimeRangeCreates a LocalTimeRange from two times 从两个时间创建LocalTimeRangestatic LocalTimeRangeofDuration(LocalTime start, Duration duration) Creates a LocalTimeRange from start time and duration 从开始时间和持续时间创建LocalTimeRangestatic LocalTimeRangeofHours(int startHour, int endHour) Creates a LocalTimeRange from hours 从小时创建LocalTimeRangebooleanoverlaps(LocalTimeRange other) Checks if this range overlaps with another range 检查此范围是否与另一个范围重叠static LocalTimeRangeParses a LocalTimeRange from a string 从字符串解析LocalTimeRangeShifts the range by the specified duration 按指定持续时间移动范围toString()Returns a new range with the end adjusted 返回调整结束时间后的新范围Returns a new range with the start adjusted 返回调整开始时间后的新范围
-
Field Details
-
ALL_DAY
A range representing the entire day -
BUSINESS_HOURS
Common business hours (9:00 - 17:00) -
MORNING
Morning (6:00 - 12:00) -
AFTERNOON
Afternoon (12:00 - 18:00) -
EVENING
Evening (18:00 - 22:00)
-
-
Method Details
-
of
Creates a LocalTimeRange from two times 从两个时间创建LocalTimeRange- Parameters:
start- the start time (inclusive) | 开始时间(包含)end- the end time (exclusive) | 结束时间(不包含)- Returns:
- the LocalTimeRange | LocalTimeRange
-
ofHours
Creates a LocalTimeRange from hours 从小时创建LocalTimeRange- Parameters:
startHour- the start hour | 开始小时endHour- the end hour | 结束小时- Returns:
- the LocalTimeRange | LocalTimeRange
-
ofDuration
Creates a LocalTimeRange from start time and duration 从开始时间和持续时间创建LocalTimeRange- Parameters:
start- the start time | 开始时间duration- the duration | 持续时间- Returns:
- the LocalTimeRange | LocalTimeRange
-
parse
Parses a LocalTimeRange from a string 从字符串解析LocalTimeRange- Parameters:
text- the text to parse (e.g., "09:00-17:00") | 要解析的文本- Returns:
- the LocalTimeRange | LocalTimeRange
-
getStart
-
getEnd
-
crossesMidnight
public boolean crossesMidnight()Checks if the range crosses midnight 检查范围是否跨越午夜- Returns:
- true if crosses midnight | 如果跨越午夜返回true
-
contains
Checks if this range contains the specified time 检查此范围是否包含指定时间- Parameters:
time- the time to check | 要检查的时间- Returns:
- true if contained | 如果包含返回true
-
contains
Checks if this range fully contains another range 检查此范围是否完全包含另一个范围- Parameters:
other- the other range | 另一个范围- Returns:
- true if fully contained | 如果完全包含返回true
-
overlaps
Checks if this range overlaps with another range 检查此范围是否与另一个范围重叠- Parameters:
other- the other range | 另一个范围- Returns:
- true if overlaps | 如果重叠返回true
-
intersection
Gets the intersection with another range 获取与另一个范围的交集- Parameters:
other- the other range | 另一个范围- Returns:
- the intersection, or null if no overlap | 交集,如果没有重叠则返回null
-
getDuration
Gets the duration of this range 获取此范围的持续时间- Returns:
- the duration | 持续时间
-
getHours
public long getHours()Gets the duration in hours 获取小时数- Returns:
- the hours | 小时数
-
getMinutes
public long getMinutes()Gets the duration in minutes 获取分钟数- Returns:
- the minutes | 分钟数
-
withStart
Returns a new range with the start adjusted 返回调整开始时间后的新范围- Parameters:
newStart- the new start time | 新开始时间- Returns:
- the new range | 新范围
-
withEnd
Returns a new range with the end adjusted 返回调整结束时间后的新范围- Parameters:
newEnd- the new end time | 新结束时间- Returns:
- the new range | 新范围
-
shift
Shifts the range by the specified duration 按指定持续时间移动范围- Parameters:
duration- the duration to shift | 移动的持续时间- Returns:
- the shifted range | 移动后的范围
-
expand
Expands the range by the specified duration on both ends 在两端按指定持续时间扩展范围- Parameters:
duration- the duration to expand | 扩展的持续时间- Returns:
- the expanded range | 扩展后的范围
-
format
Formats the range using the specified formatter 使用指定的格式化器格式化范围- Parameters:
formatter- the formatter | 格式化器- Returns:
- the formatted string | 格式化的字符串
-
formatCompact
Formats as a compact string (HH:mm-HH:mm) 格式化为紧凑字符串- Returns:
- the compact string | 紧凑字符串
-
equals
-
hashCode
-
toString
-