Class LocalTimeRange

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

public final class LocalTimeRange extends Object implements 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 Details

    • ALL_DAY

      public static final LocalTimeRange ALL_DAY
      A range representing the entire day
    • BUSINESS_HOURS

      public static final LocalTimeRange BUSINESS_HOURS
      Common business hours (9:00 - 17:00)
    • MORNING

      public static final LocalTimeRange MORNING
      Morning (6:00 - 12:00)
    • AFTERNOON

      public static final LocalTimeRange AFTERNOON
      Afternoon (12:00 - 18:00)
    • EVENING

      public static final LocalTimeRange EVENING
      Evening (18:00 - 22:00)
  • Method Details

    • of

      public static LocalTimeRange of(LocalTime start, LocalTime end)
      Creates a LocalTimeRange from two times 从两个时间创建LocalTimeRange
      Parameters:
      start - the start time (inclusive) | 开始时间(包含)
      end - the end time (exclusive) | 结束时间(不包含)
      Returns:
      the LocalTimeRange | LocalTimeRange
    • ofHours

      public static LocalTimeRange ofHours(int startHour, int endHour)
      Creates a LocalTimeRange from hours 从小时创建LocalTimeRange
      Parameters:
      startHour - the start hour | 开始小时
      endHour - the end hour | 结束小时
      Returns:
      the LocalTimeRange | LocalTimeRange
    • ofDuration

      public static LocalTimeRange ofDuration(LocalTime start, Duration duration)
      Creates a LocalTimeRange from start time and duration 从开始时间和持续时间创建LocalTimeRange
      Parameters:
      start - the start time | 开始时间
      duration - the duration | 持续时间
      Returns:
      the LocalTimeRange | LocalTimeRange
    • parse

      public static LocalTimeRange parse(String text)
      Parses a LocalTimeRange from a string 从字符串解析LocalTimeRange
      Parameters:
      text - the text to parse (e.g., "09:00-17:00") | 要解析的文本
      Returns:
      the LocalTimeRange | LocalTimeRange
    • getStart

      public LocalTime getStart()
      Gets the start time 获取开始时间
      Returns:
      the start time | 开始时间
    • getEnd

      public LocalTime getEnd()
      Gets the end time 获取结束时间
      Returns:
      the end time | 结束时间
    • crossesMidnight

      public boolean crossesMidnight()
      Checks if the range crosses midnight 检查范围是否跨越午夜
      Returns:
      true if crosses midnight | 如果跨越午夜返回true
    • contains

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

      public boolean contains(LocalTimeRange other)
      Checks if this range fully contains another range 检查此范围是否完全包含另一个范围
      Parameters:
      other - the other range | 另一个范围
      Returns:
      true if fully contained | 如果完全包含返回true
    • overlaps

      public boolean overlaps(LocalTimeRange other)
      Checks if this range overlaps with another range 检查此范围是否与另一个范围重叠
      Parameters:
      other - the other range | 另一个范围
      Returns:
      true if overlaps | 如果重叠返回true
    • intersection

      public LocalTimeRange intersection(LocalTimeRange other)
      Gets the intersection with another range 获取与另一个范围的交集
      Parameters:
      other - the other range | 另一个范围
      Returns:
      the intersection, or null if no overlap | 交集,如果没有重叠则返回null
    • getDuration

      public Duration 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

      public LocalTimeRange withStart(LocalTime newStart)
      Returns a new range with the start adjusted 返回调整开始时间后的新范围
      Parameters:
      newStart - the new start time | 新开始时间
      Returns:
      the new range | 新范围
    • withEnd

      public LocalTimeRange withEnd(LocalTime newEnd)
      Returns a new range with the end adjusted 返回调整结束时间后的新范围
      Parameters:
      newEnd - the new end time | 新结束时间
      Returns:
      the new range | 新范围
    • shift

      public LocalTimeRange shift(Duration duration)
      Shifts the range by the specified duration 按指定持续时间移动范围
      Parameters:
      duration - the duration to shift | 移动的持续时间
      Returns:
      the shifted range | 移动后的范围
    • expand

      public LocalTimeRange expand(Duration duration)
      Expands the range by the specified duration on both ends 在两端按指定持续时间扩展范围
      Parameters:
      duration - the duration to expand | 扩展的持续时间
      Returns:
      the expanded range | 扩展后的范围
    • format

      public String format(DateTimeFormatter formatter)
      Formats the range using the specified formatter 使用指定的格式化器格式化范围
      Parameters:
      formatter - the formatter | 格式化器
      Returns:
      the formatted string | 格式化的字符串
    • formatCompact

      public String formatCompact()
      Formats as a compact string (HH:mm-HH:mm) 格式化为紧凑字符串
      Returns:
      the compact string | 紧凑字符串
    • 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