Class PeriodDuration

java.lang.Object
cloud.opencode.base.date.extra.PeriodDuration
All Implemented Interfaces:
Serializable, TemporalAmount

public final class PeriodDuration extends Object implements TemporalAmount, Serializable
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 Details

  • Method Details

    • of

      public static PeriodDuration of(Period period, Duration duration)
      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

      public static PeriodDuration ofPeriod(Period period)
      Creates a PeriodDuration from Period only 仅从Period创建PeriodDuration
      Parameters:
      period - the period | 周期
      Returns:
      the PeriodDuration instance | PeriodDuration实例
    • ofDuration

      public static PeriodDuration ofDuration(Duration duration)
      Creates a PeriodDuration from Duration only 仅从Duration创建PeriodDuration
      Parameters:
      duration - the duration | 时长
      Returns:
      the PeriodDuration instance | PeriodDuration实例
    • parse

      public static PeriodDuration parse(CharSequence text)
      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

      public static PeriodDuration between(LocalDateTime start, LocalDateTime end)
      Calculates the PeriodDuration between two LocalDateTimes 计算两个LocalDateTime之间的PeriodDuration
      Parameters:
      start - the start datetime | 起始日期时间
      end - the end datetime | 结束日期时间
      Returns:
      the PeriodDuration between them | 它们之间的PeriodDuration
    • getPeriod

      public Period getPeriod()
      Gets the period part 获取周期部分
      Returns:
      the period | 周期
    • getDuration

      public Duration getDuration()
      Gets the duration part 获取时长部分
      Returns:
      the duration | 时长
    • 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

      public PeriodDuration plus(PeriodDuration other)
      Adds another PeriodDuration to this 加上另一个PeriodDuration
      Parameters:
      other - the other PeriodDuration | 另一个PeriodDuration
      Returns:
      a new PeriodDuration | 新的PeriodDuration
    • minus

      public PeriodDuration minus(PeriodDuration other)
      Subtracts another PeriodDuration from this 减去另一个PeriodDuration
      Parameters:
      other - the other PeriodDuration | 另一个PeriodDuration
      Returns:
      a new PeriodDuration | 新的PeriodDuration
    • negated

      public PeriodDuration negated()
      Negates this PeriodDuration 取负
      Returns:
      a new negated PeriodDuration | 新的取负后的PeriodDuration
    • multipliedBy

      public PeriodDuration multipliedBy(int scalar)
      Multiplies this PeriodDuration by a scalar 乘以标量
      Parameters:
      scalar - the scalar | 标量
      Returns:
      a new PeriodDuration | 新的PeriodDuration
    • normalized

      public PeriodDuration normalized()
      Normalizes this PeriodDuration 标准化此PeriodDuration
      Returns:
      a new normalized PeriodDuration | 新的标准化后的PeriodDuration
    • get

      public long get(TemporalUnit unit)
      Specified by:
      get in interface TemporalAmount
    • getUnits

      public List<TemporalUnit> getUnits()
      Specified by:
      getUnits in interface TemporalAmount
    • addTo

      public Temporal addTo(Temporal temporal)
      Specified by:
      addTo in interface TemporalAmount
    • subtractFrom

      public Temporal subtractFrom(Temporal temporal)
      Specified by:
      subtractFrom in interface TemporalAmount
    • 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