Class Time

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<java.util.Date>
    Direct Known Subclasses:
    AbstractISODateTime

    public class Time
    extends java.util.Date
    Immutable class for working with time, which also contains constants and methods for working with time in general, without regard to leap years, time zones, and the like.
    Author:
    Garret Wilson
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Time.Resolution
      The granularity of time.
    • Constructor Summary

      Constructors 
      Constructor Description
      Time()
      Default constructor with the current time.
      Time​(long milliseconds)
      Milliseconds constructor.
      Time​(java.util.Date date)
      Date constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Time add​(Duration duration)
      Adds a duration to this time.
      Time ceil​(Time.Resolution resolution)
      Retrieves the ceiling of this time by the given resolution.
      Time floor​(Time.Resolution resolution)
      Retrieves the floor of this time by the given resolution.
      Time round​(Time.Resolution resolution)
      Rounds this time at the given resolution.
      Time subtract​(Duration duration)
      Subtracts a duration from this time.
      Duration subtract​(Time time)
      Subtracts a time from this time.
      java.lang.String toString()
      This version adds the current time in milliseconds.
      • Methods inherited from class java.util.Date

        after, before, clone, compareTo, equals, from, getDate, getDay, getHours, getMinutes, getMonth, getSeconds, getTime, getTimezoneOffset, getYear, hashCode, parse, setDate, setHours, setMinutes, setMonth, setSeconds, setTime, setYear, toGMTString, toInstant, toLocaleString, UTC
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • MILLISECONDS_PER_SECOND

        public static int MILLISECONDS_PER_SECOND
        The number of milliseconds in a second.
      • SECONDS_PER_MINUTE

        public static int SECONDS_PER_MINUTE
        The number of seconds in a minute.
      • MINUTES_PER_HOUR

        public static int MINUTES_PER_HOUR
        The number of minutes in an hour.
      • HOURS_PER_DAY

        public static int HOURS_PER_DAY
        The number of hours in a day.
    • Constructor Detail

      • Time

        public Time()
        Default constructor with the current time.
        See Also:
        System.currentTimeMillis()
      • Time

        public Time​(java.util.Date date)
        Date constructor.
        Parameters:
        date - The date containing the number of milliseconds past the epoch.
      • Time

        public Time​(long milliseconds)
        Milliseconds constructor.
        Parameters:
        milliseconds - The number of milliseconds past the epoch.
    • Method Detail

      • add

        public Time add​(Duration duration)
        Adds a duration to this time.
        Parameters:
        duration - The duration to add.
        Returns:
        A time object representing this time plus the given duration.
        Throws:
        java.lang.NullPointerException - if the given duration is null.
      • subtract

        public Time subtract​(Duration duration)
        Subtracts a duration from this time.
        Parameters:
        duration - The duration to subtract.
        Returns:
        A time object representing this time minus the given duration.
        Throws:
        java.lang.NullPointerException - if the given duration is null.
      • subtract

        public Duration subtract​(Time time)
        Subtracts a time from this time. This operation may result in a negative duration.
        Parameters:
        time - The time to subtract.
        Returns:
        The duration between the two times, that is, this time minus the given time.
        Throws:
        java.lang.NullPointerException - if the given time is null.
      • ceil

        public Time ceil​(Time.Resolution resolution)
        Retrieves the ceiling of this time by the given resolution. A resolution of Time.Resolution.MILLISECONDS will simply return a time object equal to this time object. A resolution of Time.Resolution.SECONDS, for example, will effectively drop the milliseconds, rounding down to seconds.
        Parameters:
        resolution - The resolution at which rounding should occur.
        Returns:
        A time instance indicating the ceiling time at the specified resolution.
        See Also:
        Math.ceil(double)
      • floor

        public Time floor​(Time.Resolution resolution)
        Retrieves the floor of this time by the given resolution. A resolution of Time.Resolution.MILLISECONDS will simply return a time object equal to this time object. A resolution of Time.Resolution.SECONDS, for example, will effectively drop the milliseconds, rounding down to seconds.
        Parameters:
        resolution - The resolution at which rounding should occur.
        Returns:
        A time instance indicating the floor time at the specified resolution.
        See Also:
        Math.floor(double)
      • round

        public Time round​(Time.Resolution resolution)
        Rounds this time at the given resolution. A resolution of Time.Resolution.MILLISECONDS will simply return a time object equal to this time object. A resolution of Time.Resolution.SECONDS, for example, will effectively drop the milliseconds, rounding down to seconds.
        Parameters:
        resolution - The resolution at which rounding should occur.
        Returns:
        A time instance indicating the rounded time at the specified resolution.
        See Also:
        Math.round(double)
      • toString

        public java.lang.String toString()
        This version adds the current time in milliseconds.
        Overrides:
        toString in class java.util.Date