Class TimePeriod

java.lang.Object
org.apache.avro.util.TimePeriod
All Implemented Interfaces:
Serializable, TemporalAmount

public final class TimePeriod extends Object implements TemporalAmount, Serializable

A temporal amount to model an Avro duration (the logical type).

It consists of a number of months, days and milliseconds, all modelled as an unsigned integer.

Compared to java.time.Period, this class has a smaller range ('only' supporting a little less than 358 million years), and cannot support negative time periods.

Compared to java.time.Duration, this class has less precision (milliseconds compared to nanoseconds), cannot support negative durations, and has a much smaller range. Where java.time.Duration supports fixed ranges up to about 68 years, TimePeriod can only handle about 49 days.

Comparison with the regular java.time classes:

TimePeriod Period Duration
Precision milliseconds days nanoseconds
Time range (approx.) 0 - 49 days unsupported -68 - 68 years
Date range (approx.) 0 to 370 million years -2.3 to 2.3 billion years unsupported
See Also:
  • Method Details

    • from

      public static TimePeriod from(TemporalAmount amount)
      Create a TimePeriod from another TemporalAmount, such as a Period or a Duration.
      Parameters:
      amount - a temporal amount
      Returns:
      the corresponding TimePeriod
    • of

      public static TimePeriod of(long months, long days, long millis)
      Create a TimePeriod from a number of months, days and milliseconds
      Parameters:
      months - a number of months
      days - a number of days
      millis - a number of milliseconds
      Returns:
      the corresponding TimePeriod
      Throws:
      ArithmeticException - if any of the parameters does not fit an unsigned long (0..4294967296)
    • toDuration

      public Duration toDuration()
    • toPeriod

      public Period toPeriod()
    • isDateBased

      public boolean isDateBased()
      Determines if the TimePeriod is date based (i.e., if its milliseconds component is 0).
      Returns:
      true iff the TimePeriod is date based
    • isTimeBased

      public boolean isTimeBased()
      Determines if the TimePeriod is time based (i.e., if its months and days components are 0).
      Returns:
      true iff the TimePeriod is time based
    • getMonths

      public long getMonths()
    • getDays

      public long getDays()
    • getMillis

      public long getMillis()
    • 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 o)
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Overrides:
      toString in class Object