Class Duration
- java.lang.Object
-
- org.joda.time.base.AbstractDuration
-
- org.joda.time.base.BaseDuration
-
- org.joda.time.Duration
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable,ReadableDuration
public final class Duration extends BaseDuration implements ReadableDuration, java.io.Serializable
An immutable duration specifying a length of time in milliseconds.A duration is defined by a fixed number of milliseconds. There is no concept of fields, such as days or seconds, as these fields can vary in length. A duration may be converted to a
Periodto obtain field values. This conversion will typically cause a loss of precision however.Duration is thread-safe and immutable.
- Since:
- 1.0
- Author:
- Brian S O'Neill, Stephen Colebourne
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Duration(long duration)Creates a duration from the given millisecond duration.Duration(long startInstant, long endInstant)Creates a duration from the given interval endpoints.Duration(java.lang.Object duration)Creates a duration from the specified object using theConverterManager.Duration(ReadableInstant start, ReadableInstant end)Creates a duration from the given interval endpoints.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description longgetStandardSeconds()Gets the length of this duration in seconds assuming 1000 milliseconds in a second.Durationminus(long amount)Returns a new duration with this length minus that specified.Durationminus(ReadableDuration amount)Returns a new duration with this length minus that specified.Durationplus(long amount)Returns a new duration with this length plus that specified.Durationplus(ReadableDuration amount)Returns a new duration with this length plus that specified.static DurationstandardDays(long days)Create a duration with the specified number of days assuming that there are the standard number of milliseconds in a day.static DurationstandardHours(long hours)Create a duration with the specified number of hours assuming that there are the standard number of milliseconds in an hour.static DurationstandardMinutes(long minutes)Create a duration with the specified number of minutes assuming that there are the standard number of milliseconds in a minute.static DurationstandardSeconds(long seconds)Create a duration with the specified number of seconds assuming that there are the standard number of milliseconds in a second.DurationtoDuration()Get this duration as an immutableDurationobject by returningthis.SecondstoStandardSeconds()Converts this duration to a period in seconds assuming 1000 milliseconds in a second.DurationwithDurationAdded(long durationToAdd, int scalar)Returns a new duration with this length plus that specified multiplied by the scalar.DurationwithDurationAdded(ReadableDuration durationToAdd, int scalar)Returns a new duration with this length plus that specified multiplied by the scalar.DurationwithMillis(long duration)Creates a new Duration instance with a different milisecond length.-
Methods inherited from class org.joda.time.base.BaseDuration
getMillis, setMillis, toIntervalFrom, toIntervalTo, toPeriod, toPeriod, toPeriod, toPeriodFrom, toPeriodFrom, toPeriodTo, toPeriodTo
-
Methods inherited from class org.joda.time.base.AbstractDuration
compareTo, equals, hashCode, isEqual, isLongerThan, isShorterThan, toPeriod, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.joda.time.ReadableDuration
compareTo, equals, getMillis, hashCode, isEqual, isLongerThan, isShorterThan, toPeriod, toString
-
-
-
-
Field Detail
-
ZERO
public static final Duration ZERO
Constant representing zero millisecond duration
-
-
Constructor Detail
-
Duration
public Duration(long duration)
Creates a duration from the given millisecond duration.- Parameters:
duration- the duration, in milliseconds
-
Duration
public Duration(long startInstant, long endInstant)Creates a duration from the given interval endpoints.- Parameters:
startInstant- interval start, in millisecondsendInstant- interval end, in milliseconds- Throws:
java.lang.ArithmeticException- if the duration exceeds a 64 bit long
-
Duration
public Duration(ReadableInstant start, ReadableInstant end)
Creates a duration from the given interval endpoints.- Parameters:
start- interval start, null means nowend- interval end, null means now- Throws:
java.lang.ArithmeticException- if the duration exceeds a 64 bit long
-
Duration
public Duration(java.lang.Object duration)
Creates a duration from the specified object using theConverterManager.- Parameters:
duration- duration to convert- Throws:
java.lang.IllegalArgumentException- if duration is invalid
-
-
Method Detail
-
standardDays
public static Duration standardDays(long days)
Create a duration with the specified number of days assuming that there are the standard number of milliseconds in a day.This method assumes that there are 24 hours in a day, 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in a second. This will be true for most days, however days with Daylight Savings changes will not have 24 hours, so use this method with care.
A Duration is a representation of an amount of time. If you want to express the concepts of 'days' you should consider using the
Daysclass.- Parameters:
days- the number of standard days in this duration- Returns:
- the duration, never null
- Throws:
java.lang.ArithmeticException- if the days value is too large- Since:
- 1.6
-
standardHours
public static Duration standardHours(long hours)
Create a duration with the specified number of hours assuming that there are the standard number of milliseconds in an hour.This method assumes that there are 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in a second. All currently supplied chronologies use this definition.
A Duration is a representation of an amount of time. If you want to express the concepts of 'hours' you should consider using the
Hoursclass.- Parameters:
hours- the number of standard hours in this duration- Returns:
- the duration, never null
- Throws:
java.lang.ArithmeticException- if the hours value is too large- Since:
- 1.6
-
standardMinutes
public static Duration standardMinutes(long minutes)
Create a duration with the specified number of minutes assuming that there are the standard number of milliseconds in a minute.This method assumes that there are 60 seconds in a minute and 1000 milliseconds in a second. All currently supplied chronologies use this definition.
A Duration is a representation of an amount of time. If you want to express the concepts of 'minutes' you should consider using the
Minutesclass.- Parameters:
minutes- the number of standard minutes in this duration- Returns:
- the duration, never null
- Throws:
java.lang.ArithmeticException- if the minutes value is too large- Since:
- 1.6
-
standardSeconds
public static Duration standardSeconds(long seconds)
Create a duration with the specified number of seconds assuming that there are the standard number of milliseconds in a second.This method assumes that there are 1000 milliseconds in a second. All currently supplied chronologies use this definition.
A Duration is a representation of an amount of time. If you want to express the concepts of 'seconds' you should consider using the
Secondsclass.- Parameters:
seconds- the number of standard seconds in this duration- Returns:
- the duration, never null
- Throws:
java.lang.ArithmeticException- if the seconds value is too large- Since:
- 1.6
-
getStandardSeconds
public long getStandardSeconds()
Gets the length of this duration in seconds assuming 1000 milliseconds in a second.This returns
getMillis() / 1000. The result is an integer division, so 2999 millis returns 2 seconds.- Returns:
- the length of the duration in standard seconds
- Since:
- 1.6
-
toDuration
public Duration toDuration()
Get this duration as an immutableDurationobject by returningthis.- Specified by:
toDurationin interfaceReadableDuration- Overrides:
toDurationin classAbstractDuration- Returns:
this
-
toStandardSeconds
public Seconds toStandardSeconds()
Converts this duration to a period in seconds assuming 1000 milliseconds in a second.This method allows you to convert between a duration and a period.
- Returns:
- a period representing the number of standard seconds in this period, never null
- Throws:
java.lang.ArithmeticException- if the number of seconds is too large to be represented- Since:
- 1.6
-
withMillis
public Duration withMillis(long duration)
Creates a new Duration instance with a different milisecond length.- Parameters:
duration- the new length of the duration- Returns:
- the new duration instance
-
withDurationAdded
public Duration withDurationAdded(long durationToAdd, int scalar)
Returns a new duration with this length plus that specified multiplied by the scalar. This instance is immutable and is not altered.If the addition is zero, this instance is returned.
- Parameters:
durationToAdd- the duration to add to this onescalar- the amount of times to add, such as -1 to subtract once- Returns:
- the new duration instance
-
withDurationAdded
public Duration withDurationAdded(ReadableDuration durationToAdd, int scalar)
Returns a new duration with this length plus that specified multiplied by the scalar. This instance is immutable and is not altered.If the addition is zero, this instance is returned.
- Parameters:
durationToAdd- the duration to add to this one, null means zeroscalar- the amount of times to add, such as -1 to subtract once- Returns:
- the new duration instance
-
plus
public Duration plus(long amount)
Returns a new duration with this length plus that specified. This instance is immutable and is not altered.If the addition is zero, this instance is returned.
- Parameters:
amount- the duration to add to this one- Returns:
- the new duration instance
-
plus
public Duration plus(ReadableDuration amount)
Returns a new duration with this length plus that specified. This instance is immutable and is not altered.If the amount is zero, this instance is returned.
- Parameters:
amount- the duration to add to this one, null means zero- Returns:
- the new duration instance
-
minus
public Duration minus(long amount)
Returns a new duration with this length minus that specified. This instance is immutable and is not altered.If the addition is zero, this instance is returned.
- Parameters:
amount- the duration to take away from this one- Returns:
- the new duration instance
-
minus
public Duration minus(ReadableDuration amount)
Returns a new duration with this length minus that specified. This instance is immutable and is not altered.If the amount is zero, this instance is returned.
- Parameters:
amount- the duration to take away from this one, null means zero- Returns:
- the new duration instance
-
-