Module javafx.base
Package javafx.util

Class Duration

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

    Fields
    Modifier and Type Field 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 Summary

    Constructors
    Constructor Description
    Duration​(double millis)
    Creates a new Duration with potentially fractional millisecond resolution.
  • Method Summary

    Modifier and Type Method 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.
    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.
    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.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • ZERO

      public static final Duration ZERO
      A Duration of 0 (no time).
    • ONE

      public static final Duration ONE
      A Duration of 1 millisecond.
    • INDEFINITE

      public static final Duration INDEFINITE
      An Infinite Duration.
    • UNKNOWN

      public static final Duration UNKNOWN
      A Duration of some unknown amount of time.
  • Constructor Details

    • Duration

      public Duration​(double millis)
      Creates a new Duration with potentially fractional millisecond resolution.
      Parameters:
      millis - The number of milliseconds
  • Method Details

    • valueOf

      public static Duration valueOf​(String time)
      Factory 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
    • millis

      public static Duration millis​(double ms)
      Factory 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
    • seconds

      public static Duration seconds​(double s)
      Factory 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
    • minutes

      public static Duration minutes​(double m)
      Factory 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
    • hours

      public static Duration hours​(double h)
      Factory 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
    • toMillis

      public 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
    • toSeconds

      public 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
    • toMinutes

      public 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
    • toHours

      public 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
    • add

      public Duration add​(Duration other)
      Add 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
    • subtract

      public Duration subtract​(Duration other)
      Subtract 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
    • multiply

      @Deprecated public Duration multiply​(Duration other)
      Deprecated.
      This method produces surprising results by not taking units into account. Use multiply(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
    • multiply

      public Duration multiply​(double n)
      Multiply 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
    • divide

      public Duration divide​(double n)
      Divide 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
    • divide

      @Deprecated public Duration divide​(Duration other)
      Deprecated.
      This method produces surprising results by not taking units into account. Use divide(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
    • negate

      public Duration negate()
      Return 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
    • isIndefinite

      public 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.
    • isUnknown

      public 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)
    • lessThan

      public boolean lessThan​(Duration other)
      Returns 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
    • lessThanOrEqualTo

      public boolean lessThanOrEqualTo​(Duration other)
      Returns 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
    • greaterThan

      public boolean greaterThan​(Duration other)
      Returns 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
    • greaterThanOrEqualTo

      public boolean greaterThanOrEqualTo​(Duration other)
      Returns 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
    • toString

      public String toString()
      Returns a string representation of this Duration object.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this Duration object.
    • compareTo

      public int compareTo​(Duration d)
      Compares 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:
      compareTo in 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.
    • equals

      public boolean equals​(Object obj)
      Indicates whether some other object is "equal to" this one.
      Overrides:
      equals in class Object
      Parameters:
      obj - the reference object with which to compare.
      Returns:
      true if this object is equal to the obj argument; false otherwise.
    • hashCode

      public int hashCode()
      Returns a hash code for this Duration object.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code for this Duration object.