Class AbstractDuration
- java.lang.Object
-
- org.joda.time.base.AbstractDuration
-
- All Implemented Interfaces:
java.lang.Comparable<ReadableDuration>
,ReadableDuration
- Direct Known Subclasses:
BaseDuration
public abstract class AbstractDuration extends java.lang.Object implements ReadableDuration
AbstractDuration provides the common behaviour for duration classes.This class should generally not be used directly by API users. The
ReadableDuration
interface should be used when different kinds of durations are to be referenced.AbstractDuration subclasses may be mutable and not thread-safe.
- Since:
- 1.0
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(ReadableDuration other)
Compares this duration with the specified duration based on length and direction.boolean
equals(java.lang.Object duration)
Compares this object with the specified object for equality based on the millisecond length.int
hashCode()
Gets a hash code for the duration that is compatible with the equals method.boolean
isEqual(ReadableDuration duration)
Is the length of this duration equal to the duration passed in.boolean
isLongerThan(ReadableDuration duration)
Is the length of this duration longer than the duration passed in.boolean
isShorterThan(ReadableDuration duration)
Is the length of this duration shorter than the duration passed in.Duration
toDuration()
Get this duration as an immutableDuration
object.Period
toPeriod()
Converts this duration to a Period instance using the standard period type and the ISO chronology.java.lang.String
toString()
Gets the value as a String in the ISO8601 duration format including only seconds and milliseconds.-
Methods inherited from interface org.joda.time.ReadableDuration
getMillis
-
-
-
-
Method Detail
-
toDuration
public Duration toDuration()
Get this duration as an immutableDuration
object.- Specified by:
toDuration
in interfaceReadableDuration
- Returns:
- a Duration created using the millisecond duration from this instance
-
toPeriod
public Period toPeriod()
Converts this duration to a Period instance using the standard period type and the ISO chronology.Only precise fields in the period type will be used. Thus, only the hour, minute, second and millisecond fields on the period will be used. The year, month, week and day fields will not be populated.
If the duration is small, less than one day, then this method will perform as you might expect and split the fields evenly. If the duration is larger than one day then all the remaining duration will be stored in the largest available field, hours in this case.
For example, a duration effectively equal to (365 + 60 + 5) days will be converted to ((365 + 60 + 5) * 24) hours by this constructor.
For more control over the conversion process, you must pair the duration with an instant, see
Period(ReadableInstant,ReadableDuration)
.- Specified by:
toPeriod
in interfaceReadableDuration
- Returns:
- a Period created using the millisecond duration from this instance
-
compareTo
public int compareTo(ReadableDuration other)
Compares this duration with the specified duration based on length and direction.The comparison takes into account the sign. As such, a duration of 5 seconds is longer than a duration of minus 7 seconds.
- Specified by:
compareTo
in interfacejava.lang.Comparable<ReadableDuration>
- Parameters:
other
- a duration to check against- Returns:
- negative value if this is less, 0 if equal, or positive value if greater
- Throws:
java.lang.NullPointerException
- if the object is nulljava.lang.ClassCastException
- if the given object is not supported
-
isEqual
public boolean isEqual(ReadableDuration duration)
Is the length of this duration equal to the duration passed in.The comparison takes into account the sign. As such, a duration of 5 seconds is not equal to a duration of minus 5 seconds.
- Specified by:
isEqual
in interfaceReadableDuration
- Parameters:
duration
- another duration to compare to, null means zero milliseconds- Returns:
- true if this duration is equal to than the duration passed in
-
isLongerThan
public boolean isLongerThan(ReadableDuration duration)
Is the length of this duration longer than the duration passed in.The comparison takes into account the sign. As such, a duration of 5 seconds is longer than a duration of minus 7 seconds.
- Specified by:
isLongerThan
in interfaceReadableDuration
- Parameters:
duration
- another duration to compare to, null means zero milliseconds- Returns:
- true if this duration is strictly longer than the duration passed in
-
isShorterThan
public boolean isShorterThan(ReadableDuration duration)
Is the length of this duration shorter than the duration passed in.The comparison takes into account the sign. As such, a duration of minus 5 seconds is shorter than a duration of 3 seconds.
- Specified by:
isShorterThan
in interfaceReadableDuration
- Parameters:
duration
- another duration to compare to, null means zero milliseconds- Returns:
- true if this duration is strictly shorter than the duration passed in
-
equals
public boolean equals(java.lang.Object duration)
Compares this object with the specified object for equality based on the millisecond length. All ReadableDuration instances are accepted.The comparison takes into account the sign. As such, a duration of 5 seconds is not equal to a duration of minus 5 seconds.
- Specified by:
equals
in interfaceReadableDuration
- Overrides:
equals
in classjava.lang.Object
- Parameters:
duration
- a readable duration to check against- Returns:
- true if the length of the duration is equal
-
hashCode
public int hashCode()
Gets a hash code for the duration that is compatible with the equals method.- Specified by:
hashCode
in interfaceReadableDuration
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- a hash code
-
toString
public java.lang.String toString()
Gets the value as a String in the ISO8601 duration format including only seconds and milliseconds.For example, "PT72.345S" represents 1 minute, 12 seconds and 345 milliseconds.
For more control over the output, see
PeriodFormatterBuilder
.- Specified by:
toString
in interfaceReadableDuration
- Overrides:
toString
in classjava.lang.Object
- Returns:
- the value as an ISO8601 string
-
-