Package org.joda.time

Interface ReadablePeriod

All Known Subinterfaces:
ReadWritablePeriod
All Known Implementing Classes:
AbstractPeriod, BasePeriod, BaseSingleFieldPeriod, Days, Hours, Minutes, Months, MutablePeriod, Period, Seconds, Weeks, Years

public interface ReadablePeriod
Defines a time period specified in terms of individual duration fields such as years and days.

The implementation of this interface may be mutable or immutable. This interface only gives access to retrieve data, never to change it.

Periods are split up into multiple fields, for example days and seconds. Implementations are not required to evenly distribute the values across the fields. The value for each field may be positive or negative.

When a time period is added to an instant, the effect is to add each field in turn. For example, a time period could be defined as 3 months, 2 days and -1 hours. In most circumstances this would be the same as 3 months, 1 day, and 23 hours. However, when adding across a daylight savings boundary, a day may be 23 or 25 hours long. Thus, the time period is always added field by field to the datetime.

Periods are independent of chronology, and can only be treated as durations when paired with a time via an interval.

Since:
1.0
Author:
Brian S O'Neill, Stephen Colebourne
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    equals(Object readablePeriod)
    Compares this object with the specified object for equality based on the value and type of each supported field.
    int
    Gets the value of one of the fields.
    getFieldType(int index)
    Gets the field type at the specified index.
    Gets the period type that defines which fields are included in the period.
    int
    getValue(int index)
    Gets the value at the specified index.
    int
    Gets a hash code for the period that is compatible with the equals method.
    boolean
    Checks whether the field type specified is supported by this period.
    int
    Gets the number of fields that this period supports.
    Get this object as a MutablePeriod.
    Get this period as an immutable Period object.
    Gets the value as a String in the style of the ISO8601 duration format.
  • Method Details

    • getPeriodType

      PeriodType getPeriodType()
      Gets the period type that defines which fields are included in the period.
      Returns:
      the period type
    • size

      int size()
      Gets the number of fields that this period supports.
      Returns:
      the number of fields supported
    • getFieldType

      DurationFieldType getFieldType(int index)
      Gets the field type at the specified index.
      Parameters:
      index - the index to retrieve
      Returns:
      the field at the specified index
      Throws:
      IndexOutOfBoundsException - if the index is invalid
    • getValue

      int getValue(int index)
      Gets the value at the specified index.
      Parameters:
      index - the index to retrieve
      Returns:
      the value of the field at the specified index
      Throws:
      IndexOutOfBoundsException - if the index is invalid
    • get

      int get(DurationFieldType field)
      Gets the value of one of the fields.

      If the field type specified is not supported by the period then zero is returned.

      Parameters:
      field - the field type to query, null returns zero
      Returns:
      the value of that field, zero if field not supported
    • isSupported

      boolean isSupported(DurationFieldType field)
      Checks whether the field type specified is supported by this period.
      Parameters:
      field - the field to check, may be null which returns false
      Returns:
      true if the field is supported
    • toPeriod

      Period toPeriod()
      Get this period as an immutable Period object.

      This will either typecast this instance, or create a new Period.

      Returns:
      a Duration using the same field set and values
    • toMutablePeriod

      MutablePeriod toMutablePeriod()
      Get this object as a MutablePeriod.

      This will always return a new MutablePeriod with the same fields.

      Returns:
      a MutablePeriod using the same field set and values
    • equals

      boolean equals(Object readablePeriod)
      Compares this object with the specified object for equality based on the value and type of each supported field. All ReadablePeriod instances are accepted.

      Note that a period of 1 day is not equal to a period of 24 hours, nor is 1 hour equal to 60 minutes. Only periods with the same amount in each field are equal.

      This is because periods represent an abstracted definition of a time period (eg. a day may not actually be 24 hours, it might be 23 or 25 at daylight savings boundary).

      To compare the actual duration of two periods, convert both to Durations, an operation that emphasises that the result may differ according to the date you choose.

      Overrides:
      equals in class Object
      Parameters:
      readablePeriod - a readable period to check against
      Returns:
      true if all the field values and types are equal, false if not or the period is null or of an incorrect type
    • hashCode

      int hashCode()
      Gets a hash code for the period that is compatible with the equals method. The hashcode is calculated as follows:
        int total = 17;
        for (int i = 0; i invalid input: '<' fields.length; i++) {
            total = 27 * total + getValue(i);
            total = 27 * total + getFieldType(i).hashCode();
        }
        return total;
       
      Overrides:
      hashCode in class Object
      Returns:
      a hash code
    • toString

      String toString()
      Gets the value as a String in the style of the ISO8601 duration format. Technically, the output can breach the ISO specification as weeks may be included.

      For example, "PT6H3M5S" represents 6 hours, 3 minutes, 5 seconds.

      Overrides:
      toString in class Object
      Returns:
      the value as an ISO8601 style string