Class DateBetween
java.lang.Object
cloud.opencode.base.date.between.DateBetween
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 TypeMethodDescriptionlongabsDays()Gets the absolute number of days between start and end 获取开始和结束之间的天数绝对值longGets the absolute number of months between start and end 获取开始和结束之间的月数绝对值longabsWeeks()Gets the absolute number of weeks between start and end 获取开始和结束之间的周数绝对值longabsYears()Gets the absolute number of years between start and end 获取开始和结束之间的年数绝对值static DateBetweenCreates a DateBetween from any two Temporal objects 从任意两个Temporal对象创建DateBetweenlongdays()Gets the number of days between start and end (signed) 获取开始和结束之间的天数(带符号)booleangetEnd()Gets the end temporal 获取结束时间getStart()Gets the start temporal 获取开始时间inthashCode()longhours()Gets the number of hours between start and end (signed) 获取开始和结束之间的小时数(带符号)longmillis()Gets the number of milliseconds between start and end (signed) 获取开始和结束之间的毫秒数(带符号)longminutes()Gets the number of minutes between start and end (signed) 获取开始和结束之间的分钟数(带符号)longmonths()Gets the number of complete months between start and end (signed) 获取开始和结束之间的完整月数(带符号)static DateBetweenCreates a DateBetween from two LocalDate objects 从两个LocalDate对象创建DateBetweenstatic DateBetweenof(LocalDateTime start, LocalDateTime end) Creates a DateBetween from two LocalDateTime objects 从两个LocalDateTime对象创建DateBetweenlongseconds()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)toPeriod()Converts to a Period (for LocalDate only) 转换为Period(仅用于LocalDate)toString()longweeks()Gets the number of complete weeks between start and end (signed) 获取开始和结束之间的完整周数(带符号)longyears()Gets the number of complete years between start and end (signed) 获取开始和结束之间的完整年数(带符号)
-
Method Details
-
of
Creates a DateBetween from two LocalDate objects 从两个LocalDate对象创建DateBetween- Parameters:
start- the start date | 开始日期end- the end date | 结束日期- Returns:
- the DateBetween instance | DateBetween实例
-
of
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
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
Converts to a Period (for LocalDate only) 转换为Period(仅用于LocalDate)- Returns:
- the Period | Period对象
- Throws:
ClassCastException- if temporals are not LocalDate | 如果temporal不是LocalDate则抛出异常
-
toDuration
Converts to a Duration (for LocalDateTime only) 转换为Duration(仅用于LocalDateTime)- Returns:
- the Duration | Duration对象
- Throws:
ClassCastException- if temporals are not LocalDateTime | 如果temporal不是LocalDateTime则抛出异常
-
toDateDiff
Gets a detailed breakdown of the difference 获取差异的详细分解- Returns:
- the DateDiff with detailed breakdown | 包含详细分解的DateDiff
-
getStart
-
getEnd
-
equals
-
hashCode
-
toString
-