Class AbstractPeriod
- java.lang.Object
-
- org.joda.time.base.AbstractPeriod
-
- All Implemented Interfaces:
ReadablePeriod
- Direct Known Subclasses:
BasePeriod
public abstract class AbstractPeriod extends java.lang.Object implements ReadablePeriod
AbstractPeriod provides the common behaviour for period classes.This class should generally not be used directly by API users. The
ReadablePeriodinterface should be used when different kinds of periods are to be referenced.AbstractPeriod subclasses may be mutable and not thread-safe.
- Since:
- 1.0
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object period)Compares this object with the specified object for equality based on the value of each field.intget(DurationFieldType type)Gets the value of one of the fields.DurationFieldTypegetFieldType(int index)Gets the field type at the specified index.DurationFieldType[]getFieldTypes()Gets an array of the field types that this period supports.int[]getValues()Gets an array of the value of each of the fields that this period supports.inthashCode()Gets a hash code for the period as defined by ReadablePeriod.intindexOf(DurationFieldType type)Gets the index of the field in this period.booleanisSupported(DurationFieldType type)Checks whether the field specified is supported by this period.intsize()Gets the number of fields that this period supports.MutablePeriodtoMutablePeriod()Get this object as aMutablePeriod.PeriodtoPeriod()Get this period as an immutablePeriodobject.java.lang.StringtoString()Gets the value as a String in the ISO8601 duration format.java.lang.StringtoString(PeriodFormatter formatter)Uses the specified formatter to convert this period to a String.-
Methods inherited from interface org.joda.time.ReadablePeriod
getPeriodType, getValue
-
-
-
-
Method Detail
-
size
public int size()
Gets the number of fields that this period supports.- Specified by:
sizein interfaceReadablePeriod- Returns:
- the number of fields supported
- Since:
- 2.0 (previously on BasePeriod)
-
getFieldType
public DurationFieldType getFieldType(int index)
Gets the field type at the specified index.- Specified by:
getFieldTypein interfaceReadablePeriod- Parameters:
index- the index to retrieve- Returns:
- the field at the specified index
- Throws:
java.lang.IndexOutOfBoundsException- if the index is invalid- Since:
- 2.0 (previously on BasePeriod)
-
getFieldTypes
public DurationFieldType[] getFieldTypes()
Gets an array of the field types that this period supports.The fields are returned largest to smallest, for example Hours, Minutes, Seconds.
- Returns:
- the fields supported in an array that may be altered, largest to smallest
-
getValues
public int[] getValues()
Gets an array of the value of each of the fields that this period supports.The fields are returned largest to smallest, for example Hours, Minutes, Seconds. Each value corresponds to the same array index as
getFields()- Returns:
- the current values of each field in an array that may be altered, largest to smallest
-
get
public int get(DurationFieldType type)
Gets the value of one of the fields.If the field type specified is not supported by the period then zero is returned.
- Specified by:
getin interfaceReadablePeriod- Parameters:
type- the field type to query, null returns zero- Returns:
- the value of that field, zero if field not supported
-
isSupported
public boolean isSupported(DurationFieldType type)
Checks whether the field specified is supported by this period.- Specified by:
isSupportedin interfaceReadablePeriod- Parameters:
type- the type to check, may be null which returns false- Returns:
- true if the field is supported
-
indexOf
public int indexOf(DurationFieldType type)
Gets the index of the field in this period.- Parameters:
type- the type to check, may be null which returns -1- Returns:
- the index of -1 if not supported
-
toPeriod
public Period toPeriod()
Get this period as an immutablePeriodobject.- Specified by:
toPeriodin interfaceReadablePeriod- Returns:
- a Period using the same field set and values
-
toMutablePeriod
public MutablePeriod toMutablePeriod()
Get this object as aMutablePeriod.This will always return a new
MutablePeriodwith the same fields.- Specified by:
toMutablePeriodin interfaceReadablePeriod- Returns:
- a MutablePeriod using the same field set and values
-
equals
public boolean equals(java.lang.Object period)
Compares this object with the specified object for equality based on the value of each 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.- Specified by:
equalsin interfaceReadablePeriod- Overrides:
equalsin classjava.lang.Object- Parameters:
period- a readable period to check against- Returns:
- true if all the field values are equal, false if not or the period is null or of an incorrect type
-
hashCode
public int hashCode()
Gets a hash code for the period as defined by ReadablePeriod.- Specified by:
hashCodein interfaceReadablePeriod- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash code
-
toString
public java.lang.String toString()
Gets the value as a String in the ISO8601 duration format.For example, "PT6H3M7S" represents 6 hours, 3 minutes, 7 seconds.
For more control over the output, see
PeriodFormatterBuilder.- Specified by:
toStringin interfaceReadablePeriod- Overrides:
toStringin classjava.lang.Object- Returns:
- the value as an ISO8601 string
-
toString
public java.lang.String toString(PeriodFormatter formatter)
Uses the specified formatter to convert this period to a String.- Parameters:
formatter- the formatter to use, null means usetoString().- Returns:
- the formatted string
- Since:
- 1.5
-
-