Class DateBetween

java.lang.Object
cloud.opencode.base.date.between.DateBetween

public final class DateBetween extends Object
Calculates the difference between two dates or date-times 计算两个日期或日期时间之间的差异

This class provides various methods to calculate the difference between two temporal objects in different units (days, weeks, months, years, hours, minutes, seconds, etc.).

此类提供多种方法来计算两个时间对象之间的差异,支持不同的时间单位 (天、周、月、年、小时、分钟、秒等)。

Features | 主要功能:

  • Calculate differences in various units - 计算各种时间单位的差异
  • Support for LocalDate and LocalDateTime - 支持LocalDate和LocalDateTime
  • Absolute and signed differences - 绝对值和带符号的差异
  • Period and Duration extraction - Period和Duration提取

Usage Examples | 使用示例:

LocalDate start = LocalDate.of(2024, 1, 1);
LocalDate end = LocalDate.of(2024, 12, 31);

DateBetween between = DateBetween.of(start, end);
long days = between.days();           // 365
long weeks = between.weeks();         // 52
long months = between.months();       // 11
Period period = between.toPeriod();   // P11M30D

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:
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Gets the absolute number of days between start and end 获取开始和结束之间的天数绝对值
    long
    Gets the absolute number of months between start and end 获取开始和结束之间的月数绝对值
    long
    Gets the absolute number of weeks between start and end 获取开始和结束之间的周数绝对值
    long
    Gets the absolute number of years between start and end 获取开始和结束之间的年数绝对值
    between(Temporal start, Temporal end)
    Creates a DateBetween from any two Temporal objects 从任意两个Temporal对象创建DateBetween
    long
    Gets the number of days between start and end (signed) 获取开始和结束之间的天数(带符号)
    boolean
     
    Gets the end temporal 获取结束时间
    Gets the start temporal 获取开始时间
    int
     
    long
    Gets the number of hours between start and end (signed) 获取开始和结束之间的小时数(带符号)
    long
    Gets the number of milliseconds between start and end (signed) 获取开始和结束之间的毫秒数(带符号)
    long
    Gets the number of minutes between start and end (signed) 获取开始和结束之间的分钟数(带符号)
    long
    Gets the number of complete months between start and end (signed) 获取开始和结束之间的完整月数(带符号)
    of(LocalDate start, LocalDate end)
    Creates a DateBetween from two LocalDate objects 从两个LocalDate对象创建DateBetween
    Creates a DateBetween from two LocalDateTime objects 从两个LocalDateTime对象创建DateBetween
    long
    Gets the number of seconds between start and end (signed) 获取开始和结束之间的秒数(带符号)
    Gets a detailed breakdown of the difference 获取差异的详细分解
    Converts to a Duration (for LocalDateTime only) 转换为Duration(仅用于LocalDateTime)
    Converts to a Period (for LocalDate only) 转换为Period(仅用于LocalDate)
     
    long
    Gets the number of complete weeks between start and end (signed) 获取开始和结束之间的完整周数(带符号)
    long
    Gets the number of complete years between start and end (signed) 获取开始和结束之间的完整年数(带符号)

    Methods inherited from class Object

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

    • of

      public static DateBetween of(LocalDate start, LocalDate end)
      Creates a DateBetween from two LocalDate objects 从两个LocalDate对象创建DateBetween
      Parameters:
      start - the start date | 开始日期
      end - the end date | 结束日期
      Returns:
      the DateBetween instance | DateBetween实例
    • of

      public static DateBetween of(LocalDateTime start, LocalDateTime end)
      Creates a DateBetween from two LocalDateTime objects 从两个LocalDateTime对象创建DateBetween
      Parameters:
      start - the start date-time | 开始日期时间
      end - the end date-time | 结束日期时间
      Returns:
      the DateBetween instance | DateBetween实例
    • between

      public static DateBetween between(Temporal start, Temporal end)
      Creates a DateBetween from any two Temporal objects 从任意两个Temporal对象创建DateBetween
      Parameters:
      start - the start temporal | 开始时间
      end - the end temporal | 结束时间
      Returns:
      the DateBetween instance | DateBetween实例
    • days

      public long days()
      Gets the number of days between start and end (signed) 获取开始和结束之间的天数(带符号)
      Returns:
      the number of days | 天数
    • absDays

      public long absDays()
      Gets the absolute number of days between start and end 获取开始和结束之间的天数绝对值
      Returns:
      the absolute number of days | 天数绝对值
    • weeks

      public long weeks()
      Gets the number of complete weeks between start and end (signed) 获取开始和结束之间的完整周数(带符号)
      Returns:
      the number of weeks | 周数
    • absWeeks

      public long absWeeks()
      Gets the absolute number of weeks between start and end 获取开始和结束之间的周数绝对值
      Returns:
      the absolute number of weeks | 周数绝对值
    • months

      public long months()
      Gets the number of complete months between start and end (signed) 获取开始和结束之间的完整月数(带符号)
      Returns:
      the number of months | 月数
    • absMonths

      public long absMonths()
      Gets the absolute number of months between start and end 获取开始和结束之间的月数绝对值
      Returns:
      the absolute number of months | 月数绝对值
    • years

      public long years()
      Gets the number of complete years between start and end (signed) 获取开始和结束之间的完整年数(带符号)
      Returns:
      the number of years | 年数
    • absYears

      public long absYears()
      Gets the absolute number of years between start and end 获取开始和结束之间的年数绝对值
      Returns:
      the absolute number of years | 年数绝对值
    • hours

      public long hours()
      Gets the number of hours between start and end (signed) 获取开始和结束之间的小时数(带符号)
      Returns:
      the number of hours | 小时数
    • minutes

      public long minutes()
      Gets the number of minutes between start and end (signed) 获取开始和结束之间的分钟数(带符号)
      Returns:
      the number of minutes | 分钟数
    • seconds

      public long seconds()
      Gets the number of seconds between start and end (signed) 获取开始和结束之间的秒数(带符号)
      Returns:
      the number of seconds | 秒数
    • millis

      public long millis()
      Gets the number of milliseconds between start and end (signed) 获取开始和结束之间的毫秒数(带符号)
      Returns:
      the number of milliseconds | 毫秒数
    • toPeriod

      public Period toPeriod()
      Converts to a Period (for LocalDate only) 转换为Period(仅用于LocalDate)
      Returns:
      the Period | Period对象
      Throws:
      ClassCastException - if temporals are not LocalDate | 如果temporal不是LocalDate则抛出异常
    • toDuration

      public Duration toDuration()
      Converts to a Duration (for LocalDateTime only) 转换为Duration(仅用于LocalDateTime)
      Returns:
      the Duration | Duration对象
      Throws:
      ClassCastException - if temporals are not LocalDateTime | 如果temporal不是LocalDateTime则抛出异常
    • toDateDiff

      public DateDiff toDateDiff()
      Gets a detailed breakdown of the difference 获取差异的详细分解
      Returns:
      the DateDiff with detailed breakdown | 包含详细分解的DateDiff
    • getStart

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

      public Temporal getEnd()
      Gets the end temporal 获取结束时间
      Returns:
      the end temporal | 结束时间
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object