public class Duration extends Object implements Comparable<Duration>, Serializable
A class that defines a duration of time. Duration instances are immutable,
and are therefore replaced rather than modified, similar to BigDecimal.
Duration's can be created using the constructor, or one of the static construction
methods such as seconds(double) or minutes(double).
| Modifier and Type | Field and Description |
|---|---|
static Duration |
INDEFINITE
An Infinite Duration.
|
static Duration |
ONE
A Duration of 1 millisecond.
|
static Duration |
UNKNOWN
A Duration of some unknown amount of time.
|
static Duration |
ZERO
A Duration of 0 (no time).
|
| Constructor and Description |
|---|
Duration(double millis)
Creates a new Duration with potentially fractional millisecond resolution.
|
| Modifier and Type | Method and Description |
|---|---|
Duration |
add(Duration other)
Add this instance and another Duration instance to return a new Duration instance.
|
int |
compareTo(Duration d)
Compares durations represented by this object and the specified object.
|
Duration |
divide(double n)
Divide this instance by a number to return a new Duration instance.
|
Duration |
divide(Duration other)
Deprecated.
This method produces surprising results by not taking units into
account. Use
divide(double) instead. |
boolean |
equals(Object obj)
Indicates whether some other object is "equal to" this one.
|
boolean |
greaterThan(Duration other)
Returns true if the specified duration is greater than (>) this instance.
|
boolean |
greaterThanOrEqualTo(Duration other)
Returns true if the specified duration is greater than or equal to (>=) this instance.
|
int |
hashCode()
Returns a hash code for this
Duration object. |
static Duration |
hours(double h)
Factory method that returns a Duration instance representing the specified
number of hours.
|
boolean |
isIndefinite()
Gets whether this Duration instance is Indefinite.
|
boolean |
isUnknown()
Gets whether this Duration instance is Unknown.
|
boolean |
lessThan(Duration other)
Returns true if the specified duration is less than (<) this instance.
|
boolean |
lessThanOrEqualTo(Duration other)
Returns true if the specified duration is less than or equal to (<=) this instance.
|
static Duration |
millis(double ms)
Factory method that returns a Duration instance for a specified
number of milliseconds.
|
static Duration |
minutes(double m)
Factory method that returns a Duration instance representing the specified
number of minutes.
|
Duration |
multiply(double n)
Multiply this instance with a number representing millis and return a new Duration.
|
Duration |
multiply(Duration other)
Deprecated.
This method produces surprising results by not taking units into
account. Use
multiply(double) instead. |
Duration |
negate()
Return a new Duration instance which has a negative number of milliseconds
from this instance.
|
static Duration |
seconds(double s)
Factory method that returns a Duration instance representing the specified
number of seconds.
|
Duration |
subtract(Duration other)
Subtract other Duration instance from this instance to return a new Duration instance.
|
double |
toHours()
Returns the number of hours in this period or Double.POSITIVE_INFINITY
if the period is INDEFINITE or NaN if the period is UNKNOWN.
|
double |
toMillis()
Returns the number of milliseconds in this period or Double.POSITIVE_INFINITY
if the period is INDEFINITE or NaN if the period is UNKNOWN.
|
double |
toMinutes()
Returns the number of minutes in this period or Double.POSITIVE_INFINITY
if the period is INDEFINITE or NaN if the period is UNKNOWN.
|
double |
toSeconds()
Returns the number of seconds in this period or Double.POSITIVE_INFINITY
if the period is INDEFINITE or NaN if the period is UNKNOWN.
|
String |
toString()
Returns a string representation of this
Duration object. |
static Duration |
valueOf(String time)
Factory method that returns a Duration instance for a specified
amount of time.
|
public static final Duration ZERO
public static final Duration ONE
public static final Duration INDEFINITE
public static final Duration UNKNOWN
public Duration(double millis)
millis - The number of millisecondspublic static Duration valueOf(String time)
time - A non-null string properly formatted. Leading or trailing
spaces will not parse correctly. Throws a NullPointerException if
time is null.timepublic static Duration millis(double ms)
ms - the number of millisecondspublic static Duration seconds(double s)
s - the number of secondspublic static Duration minutes(double m)
m - the number of minutespublic static Duration hours(double h)
h - the number of hourspublic double toMillis()
public double toSeconds()
public double toMinutes()
public double toHours()
public Duration add(Duration other)
other - must not be nullpublic Duration subtract(Duration other)
other - must not be null@Deprecated public Duration multiply(Duration other)
multiply(double) instead.other - must not be nullpublic Duration multiply(double n)
n - the amount to multiply by in fractional millisecondspublic Duration divide(double n)
n - the amount to divide by in fractional milliseconds@Deprecated public Duration divide(Duration other)
divide(double) instead.other - must not be nullpublic Duration negate()
Duration.millis(50).negate() returns
a Duration of -50 milliseconds.
If the called Duration instance is INDEFINITE, return INDEFINITE.
This function does not change the value of the called Duration instance.public boolean isIndefinite()
public boolean isUnknown()
public boolean lessThan(Duration other)
other - cannot be nullpublic boolean lessThanOrEqualTo(Duration other)
other - cannot be nullpublic boolean greaterThan(Duration other)
other - cannot be nullpublic boolean greaterThanOrEqualTo(Duration other)
other - cannot be nullpublic String toString()
Duration object.public int compareTo(Duration d)
compareTo in interface Comparable<Duration>d - the duration to be compared.public boolean equals(Object obj)
Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.