java.lang.Object
javafx.util.Duration
- All Implemented Interfaces:
- Serializable,- Comparable<Duration>
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.
     Durations can be created using the constructor, or one of the static construction
     methods such as seconds(double) or minutes(double).
 
- Since:
- JavaFX 2.0
- See Also:
- Serialized Form
- 
Field Summary
- 
Constructor SummaryConstructors Constructor Description Duration(double millis)Creates a new Duration with potentially fractional millisecond resolution.
- 
Method SummaryModifier and Type Method Description Durationadd(Duration other)Add this instance and another Duration instance to return a new Duration instance.intcompareTo(Duration d)Compares durations represented by this object and the specified object.Durationdivide(double n)Divide this instance by a number to return a new Duration instance.Durationdivide(Duration other)Deprecated.This method produces surprising results by not taking units into account.booleanequals(Object obj)Indicates whether some other object is "equal to" this one.booleangreaterThan(Duration other)Returns true if the specified duration is greater than (>) this instance.booleangreaterThanOrEqualTo(Duration other)Returns true if the specified duration is greater than or equal to (>=) this instance.inthashCode()Returns a hash code for thisDurationobject.static Durationhours(double h)Factory method that returns a Duration instance representing the specified number of hours.booleanisIndefinite()Gets whether this Duration instance is Indefinite.booleanisUnknown()Gets whether this Duration instance is Unknown.booleanlessThan(Duration other)Returns true if the specified duration is less than (<) this instance.booleanlessThanOrEqualTo(Duration other)Returns true if the specified duration is less than or equal to (<=) this instance.static Durationmillis(double ms)Factory method that returns a Duration instance for a specified number of milliseconds.static Durationminutes(double m)Factory method that returns a Duration instance representing the specified number of minutes.Durationmultiply(double n)Multiply this instance with a number representing millis and return a new Duration.Durationmultiply(Duration other)Deprecated.This method produces surprising results by not taking units into account.Durationnegate()Return a new Duration instance which has a negative number of milliseconds from this instance.static Durationseconds(double s)Factory method that returns a Duration instance representing the specified number of seconds.Durationsubtract(Duration other)Subtract other Duration instance from this instance to return a new Duration instance.doubletoHours()Returns the number of hours in this period or Double.POSITIVE_INFINITY if the period is INDEFINITE or NaN if the period is UNKNOWN.doubletoMillis()Returns the number of milliseconds in this period or Double.POSITIVE_INFINITY if the period is INDEFINITE or NaN if the period is UNKNOWN.doubletoMinutes()Returns the number of minutes in this period or Double.POSITIVE_INFINITY if the period is INDEFINITE or NaN if the period is UNKNOWN.doubletoSeconds()Returns the number of seconds in this period or Double.POSITIVE_INFINITY if the period is INDEFINITE or NaN if the period is UNKNOWN.StringtoString()Returns a string representation of thisDurationobject.static DurationvalueOf(String time)Factory method that returns a Duration instance for a specified amount of time.
- 
Field Details- 
ZEROA Duration of 0 (no time).
- 
ONEA Duration of 1 millisecond.
- 
INDEFINITEAn Infinite Duration.
- 
UNKNOWNA Duration of some unknown amount of time.
 
- 
- 
Constructor Details- 
Durationpublic Duration(double millis)Creates a new Duration with potentially fractional millisecond resolution.- Parameters:
- millis- The number of milliseconds
 
 
- 
- 
Method Details- 
valueOfFactory method that returns a Duration instance for a specified amount of time. The syntax is "[number][ms|s|m|h]".- Parameters:
- time- A non-null string properly formatted. Leading or trailing spaces will not parse correctly. Throws a NullPointerException if time is null.
- Returns:
- a Duration which is represented by the time
 
- 
millisFactory method that returns a Duration instance for a specified number of milliseconds.- Parameters:
- ms- the number of milliseconds
- Returns:
- a Duration instance of the specified number of milliseconds
 
- 
secondsFactory method that returns a Duration instance representing the specified number of seconds.- Parameters:
- s- the number of seconds
- Returns:
- a Duration instance of the specified number of seconds
 
- 
minutesFactory method that returns a Duration instance representing the specified number of minutes.- Parameters:
- m- the number of minutes
- Returns:
- a Duration instance of the specified number of minutes
 
- 
hoursFactory method that returns a Duration instance representing the specified number of hours.- Parameters:
- h- the number of hours
- Returns:
- a Duration instance representing the specified number of hours
 
- 
toMillispublic 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.- Returns:
- the Duration in fractional milliseconds
 
- 
toSecondspublic 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.- Returns:
- the Duration in fractional seconds
 
- 
toMinutespublic 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.- Returns:
- the Duration in fractional minutes
 
- 
toHourspublic 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.- Returns:
- the Duration in fractional hours
 
- 
addAdd this instance and another Duration instance to return a new Duration instance. If either instance is INDEFINITE, return INDEFINITE. If either instance is UNKNOWN, return UNKNOWN. This method does not change the value of the called Duration instance.- Parameters:
- other- must not be null
- Returns:
- the result of adding this duration to the other duration. This is the same as millis + other.millis using double arithmetic
 
- 
subtractSubtract other Duration instance from this instance to return a new Duration instance. If either instance is UNKNOWN, return UNKNOWN. Otherwise, if either instance is INDEFINITE, return INDEFINITE. This method does not change the value of the called Duration instance.- Parameters:
- other- must not be null
- Returns:
- the result of subtracting the other duration from this duration. This is the same as millis - other.millis using double arithmetic
 
- 
multiplyDeprecated.This method produces surprising results by not taking units into account. Usemultiply(double)instead.Multiply this instance with a number to return a new Duration instance. If either instance is INDEFINITE, return INDEFINITE. If either Duration instance is UNKNOWN, return UNKNOWN. This method does not change the value of the called Duration instance.- Parameters:
- other- must not be null
- Returns:
- the result of multiplying this duration with the other duration. This is the same as millis * other.millis using double arithmetic
 
- 
multiplyMultiply this instance with a number representing millis and return a new Duration. If the called Duration instance is INDEFINITE, return INDEFINITE. If the called Duration instance is UNKNOWN, return UNKNOWN. This method does not change the value of the called Duration instance.- Parameters:
- n- the amount to multiply by in fractional milliseconds
- Returns:
- the result of multiplying this duration with n. This is the same as millis * n using double arithmetic
 
- 
divideDivide this instance by a number to return a new Duration instance. If the called Duration instance is INDEFINITE, return INDEFINITE. If the called Duration instance is UNKNOWN, return UNKNOWN. This method does not change the value of the called Duration instance.- Parameters:
- n- the amount to divide by in fractional milliseconds
- Returns:
- the result of dividing this duration with n. This is the same as millis / n using double arithmetic
 
- 
divideDeprecated.This method produces surprising results by not taking units into account. Usedivide(double)instead.Divide this instance by another Duration to return the ratio. If both instances are INDEFINITE, return NaN. If this instance is INDEFINITE, return POSITIVE_INFINITY If the other instance is INDEFINITE, return 0.0. This function does not change the value of the called Duration instance.- Parameters:
- other- must not be null
- Returns:
- the result of dividing this duration by the other duration. This is the same as millis / other.millis using double arithmetic
 
- 
negateReturn a new Duration instance which has a negative number of milliseconds from this instance. For example,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.- Returns:
- the result of negating this duration. This is the same as -millis using double arithmetic
 
- 
isIndefinitepublic boolean isIndefinite()Gets whether this Duration instance is Indefinite. A Duration is Indefinite if it equals Duration.INDEFINITE.- Returns:
- true if this Duration is equivalent to Duration.INDEFINITE or Double.POSITIVE_INFINITY.
 
- 
isUnknownpublic boolean isUnknown()Gets whether this Duration instance is Unknown. A Duration is Unknown if it equals Duration.UNKNOWN.- Returns:
- true if this Duration is equivalent to Duration.UNKNOWN or Double.isNaN(millis)
 
- 
lessThanReturns true if the specified duration is less than (<) this instance. INDEFINITE is treated as if it were positive infinity.- Parameters:
- other- cannot be null
- Returns:
- true if millis < other.millis using double arithmetic
 
- 
lessThanOrEqualToReturns true if the specified duration is less than or equal to (<=) this instance. INDEFINITE is treated as if it were positive infinity.- Parameters:
- other- cannot be null
- Returns:
- true if millis <= other.millis using double arithmetic
 
- 
greaterThanReturns true if the specified duration is greater than (>) this instance. INDEFINITE is treated as if it were positive infinity.- Parameters:
- other- cannot be null
- Returns:
- true if millis > other.millis using double arithmetic
 
- 
greaterThanOrEqualToReturns true if the specified duration is greater than or equal to (>=) this instance. INDEFINITE is treated as if it were positive infinity.- Parameters:
- other- cannot be null
- Returns:
- true if millis >= other.millis using double arithmetic
 
- 
toStringReturns a string representation of thisDurationobject.
- 
compareToCompares durations represented by this object and the specified object. Returns a negative integer, zero, or a positive integer as this duration is less than, equal to, or greater than the specified duration.- Specified by:
- compareToin interface- Comparable<Duration>
- Parameters:
- d- the duration to be compared.
- Returns:
- a negative integer, zero, or a positive integer as this duration is less than, equal to, or greater than the specified duration.
 
- 
equalsIndicates whether some other object is "equal to" this one.
- 
hashCodepublic int hashCode()Returns a hash code for thisDurationobject.
 
-