Class PeriodDuration
java.lang.Object
cloud.opencode.base.date.extra.PeriodDuration
- All Implemented Interfaces:
Serializable, TemporalAmount
Period and Duration combination representing both date-based and time-based amounts
周期和时长的组合,表示基于日期和基于时间的数量
This class represents a combination of Period (years, months, days) and Duration (hours, minutes, seconds, nanos). It is modeled after ThreeTen-Extra's PeriodDuration.
此类表示Period(年、月、日)和Duration(小时、分钟、秒、纳秒)的组合。 设计参考ThreeTen-Extra的PeriodDuration。
Features | 主要功能:
- Combine Period and Duration - 组合Period和Duration
- Parse ISO 8601 format - 解析ISO 8601格式
- Calculate between two DateTimes - 计算两个日期时间之间的差
- Add/subtract from Temporal - 对Temporal进行加减
Usage Examples | 使用示例:
// Create PeriodDuration
PeriodDuration pd = PeriodDuration.of(Period.ofMonths(2), Duration.ofHours(5));
// Parse from ISO format
PeriodDuration pd2 = PeriodDuration.parse("P1Y2M3DT4H5M6S");
// Calculate between dates
LocalDateTime start = LocalDateTime.of(2024, 1, 1, 9, 0);
LocalDateTime end = LocalDateTime.of(2024, 3, 15, 14, 30);
PeriodDuration between = PeriodDuration.between(start, end);
// Apply to temporal
LocalDateTime result = pd.addTo(start);
Performance | 性能特性:
- Immutable and thread-safe - 不可变且线程安全
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Immutable: Yes - 不可变: 是
- Since:
- JDK 25, opencode-base-date V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic PeriodDurationbetween(LocalDateTime start, LocalDateTime end) Calculates the PeriodDuration between two LocalDateTimes 计算两个LocalDateTime之间的PeriodDurationbooleanlongget(TemporalUnit unit) Gets the duration part 获取时长部分Gets the period part 获取周期部分getUnits()inthashCode()booleanChecks if this is negative 检查是否为负booleanisZero()Checks if this is zero 检查是否为零minus(PeriodDuration other) Subtracts another PeriodDuration from this 减去另一个PeriodDurationmultipliedBy(int scalar) Multiplies this PeriodDuration by a scalar 乘以标量negated()Negates this PeriodDuration 取负Normalizes this PeriodDuration 标准化此PeriodDurationstatic PeriodDurationCreates a PeriodDuration from Period and Duration 从Period和Duration创建PeriodDurationstatic PeriodDurationofDuration(Duration duration) Creates a PeriodDuration from Duration only 仅从Duration创建PeriodDurationstatic PeriodDurationCreates a PeriodDuration from Period only 仅从Period创建PeriodDurationstatic PeriodDurationparse(CharSequence text) Parses an ISO 8601 duration string 解析ISO 8601时长字符串plus(PeriodDuration other) Adds another PeriodDuration to this 加上另一个PeriodDurationsubtractFrom(Temporal temporal) toString()
-
Field Details
-
ZERO
Zero constant
-
-
Method Details
-
of
Creates a PeriodDuration from Period and Duration 从Period和Duration创建PeriodDuration- Parameters:
period- the period | 周期duration- the duration | 时长- Returns:
- the PeriodDuration instance | PeriodDuration实例
- Throws:
NullPointerException- if period or duration is null | 如果周期或时长为null则抛出异常
-
ofPeriod
Creates a PeriodDuration from Period only 仅从Period创建PeriodDuration- Parameters:
period- the period | 周期- Returns:
- the PeriodDuration instance | PeriodDuration实例
-
ofDuration
Creates a PeriodDuration from Duration only 仅从Duration创建PeriodDuration- Parameters:
duration- the duration | 时长- Returns:
- the PeriodDuration instance | PeriodDuration实例
-
parse
Parses an ISO 8601 duration string 解析ISO 8601时长字符串Format: P[n]Y[n]M[n]DT[n]H[n]M[n]S (e.g., "P1Y2M3DT4H5M6S")
格式:P[n]Y[n]M[n]DT[n]H[n]M[n]S(例如:"P1Y2M3DT4H5M6S")
- Parameters:
text- the text to parse | 要解析的文本- Returns:
- the parsed PeriodDuration | 解析后的PeriodDuration
- Throws:
OpenDateException- if the text cannot be parsed | 如果文本无法解析则抛出异常
-
between
Calculates the PeriodDuration between two LocalDateTimes 计算两个LocalDateTime之间的PeriodDuration- Parameters:
start- the start datetime | 起始日期时间end- the end datetime | 结束日期时间- Returns:
- the PeriodDuration between them | 它们之间的PeriodDuration
-
getPeriod
-
getDuration
-
isZero
public boolean isZero()Checks if this is zero 检查是否为零- Returns:
- true if zero | 如果为零返回true
-
isNegative
public boolean isNegative()Checks if this is negative 检查是否为负- Returns:
- true if negative | 如果为负返回true
-
plus
Adds another PeriodDuration to this 加上另一个PeriodDuration- Parameters:
other- the other PeriodDuration | 另一个PeriodDuration- Returns:
- a new PeriodDuration | 新的PeriodDuration
-
minus
Subtracts another PeriodDuration from this 减去另一个PeriodDuration- Parameters:
other- the other PeriodDuration | 另一个PeriodDuration- Returns:
- a new PeriodDuration | 新的PeriodDuration
-
negated
Negates this PeriodDuration 取负- Returns:
- a new negated PeriodDuration | 新的取负后的PeriodDuration
-
multipliedBy
Multiplies this PeriodDuration by a scalar 乘以标量- Parameters:
scalar- the scalar | 标量- Returns:
- a new PeriodDuration | 新的PeriodDuration
-
normalized
Normalizes this PeriodDuration 标准化此PeriodDuration- Returns:
- a new normalized PeriodDuration | 新的标准化后的PeriodDuration
-
get
- Specified by:
getin interfaceTemporalAmount
-
getUnits
- Specified by:
getUnitsin interfaceTemporalAmount
-
addTo
- Specified by:
addToin interfaceTemporalAmount
-
subtractFrom
- Specified by:
subtractFromin interfaceTemporalAmount
-
equals
-
hashCode
-
toString
-