Class TimeSpan


  • public class TimeSpan
    extends Object
    Encode a span of time. The main purpose of this class is to provide a printing utility for time spans. E.g., 1081 ms should be printed as 1.081 s, 108101 ms should be printed as 1 min 48.101 s, etc.

    Note that the largest value you can represent with this class is 9223372036854775807 (Long.MAX_VALUE), or equivalently, 292471208 yrs 247 days 7 hrs 12 min 55.807 sec. Overflow is not handled gracefully by this class.

    Also note that for the purposes of this class, a year has 365 days. I.e., a year corresponds to 365 * 24 * 60 * 60 * 1000 ms.

    • Constructor Detail

      • TimeSpan

        public TimeSpan()
        Create an uninstantiated TimeSpan.
      • TimeSpan

        public TimeSpan​(long milliseconds)
        Create a TimeSpan from a ms interval.
        Parameters:
        milliseconds - The interval in ms. If milliseconds < 0, an uninstantiated TimeSpan is created.
    • Method Detail

      • isInstantiated

        public boolean isInstantiated()
        Returns:
        true, if the object has been instantiated with a legal interval; false, else.
      • setYears

        public boolean setYears​(int years)
        Set the year fraction of this TimeSpan.
        Parameters:
        years - The number of years.
        Returns:
        false, if years < 0; true, else.
      • setDays

        public boolean setDays​(int days)
        Set the day fraction of this TimeSpan.
        Parameters:
        days - The number of days.
        Returns:
        false, if days < 0; true, else.
      • setHours

        public boolean setHours​(int hours)
        Set the hour fraction of this TimeSpan.
        Parameters:
        hours - The number of hours.
        Returns:
        false, if hours < 0; true, else.
      • setMinutes

        public boolean setMinutes​(int minutes)
        Set the minute fraction of this TimeSpan.
        Parameters:
        minutes - The number of minutes.
        Returns:
        false, if minutes < 0; true, else.
      • setSeconds

        public boolean setSeconds​(int seconds)
        Set the second fraction of this TimeSpan.
        Parameters:
        seconds - The number of seconds.
        Returns:
        false, if seconds < 0; true, else.
      • setMilliseconds

        public boolean setMilliseconds​(int milliseconds)
        Set the millisecond fraction of this TimeSpan.
        Parameters:
        milliseconds - The number of milliseconds.
        Returns:
        false, if milliseconds < 0; true, else.
      • setFullMilliseconds

        public boolean setFullMilliseconds​(long milliseconds)
        Set the full TimeSpan in terms of milliseconds.
        Parameters:
        milliseconds - The number of milliseconds.
        Returns:
        false, if milliseconds < 0; true, else.
      • getFullMilliseconds

        public long getFullMilliseconds()
        Get the length of the TimeSpan as milliseconds.
        Returns:
        The number of milliseconds, if known. -1, else (e.g., when the TimeSpan is not instantiated).
      • getYears

        public int getYears()
        Get the year fraction of this object.
        Returns:
        -1, if this object is not instantiated; the year fraction, else.
      • getDays

        public int getDays()
        Get the day fraction of this object.
        Returns:
        -1, if this object is not instantiated; the day fraction, else.
      • getHours

        public int getHours()
        Get the hour fraction of this object.
        Returns:
        -1, if this object is not instantiated; the hour fraction, else.
      • getMinutes

        public int getMinutes()
        Get the minute fraction of this object.
        Returns:
        -1, if this object is not instantiated; the minute fraction, else.
      • getSeconds

        public int getSeconds()
        Get the second fraction of this object.
        Returns:
        -1, if this object is not instantiated; the second fraction, else.
      • getMilliseconds

        public int getMilliseconds()
        Get the millisecond fraction of this object.
        Returns:
        -1, if this object is not instantiated; the millisecond fraction, else.
      • toString

        public String toString()
        Overrides:
        toString in class Object
        Returns:
        String representation of object. See class comments.