Package com.here.time

Class Duration

  • java.lang.Object
    • com.here.time.Duration
  • All Implemented Interfaces:
    java.lang.Comparable<Duration>

    public final class Duration
    extends java.lang.Object
    implements java.lang.Comparable<Duration>
    Represents duration in time (both positive and negative).

    The duration is represented as number of seconds (see getSeconds()) and number of nanonseconds in a second (see getNano()).

    Duration can be created from various units of time by calling on of of* methods. The to* family of methods convert duration to a value expressed in desired unit of time.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(Duration duration)  
      boolean equals​(java.lang.Object o)  
      int getNano()  
      long getSeconds()  
      int hashCode()  
      static Duration ofDays​(long days)
      Creates a duration representing specified number of days.
      static Duration ofHours​(long hours)
      Creates a duration representing specified number of hours.
      static Duration ofMillis​(long milliseconds)
      Creates a duration representing specified number of milliseconds.
      static Duration ofMinutes​(long minutes)
      Creates a duration representing specified number of hours.
      static Duration ofNanos​(long nanoseconds)
      Creates a duration representing specified number of nanoseconds.
      static Duration ofSeconds​(long seconds)
      Creates a duration representing specified number of seconds.
      static Duration ofSeconds​(long seconds, long nanoAdjustment)
      Creates a duration representing specified number of seconds and an adjustment in nanoseconds.
      long toDays()
      Converts this duration to days.
      long toDaysPart()
      Same as toDays().
      long toHours()
      Converts this duration to hours.
      int toHoursPart()
      Gets the hours part of this duration.
      long toMillis()
      Converts this duration to milliseconds.
      int toMillisPart()
      Gets the milliseconds part of this duration.
      long toMinutes()
      Converts this duration to minutes.
      int toMinutesPart()
      Gets the minutes part of this duration.
      long toNanos()
      Converts this duration to nanoseconds.
      int toNanosPart()
      Gets the nanoseconds part of this duration.
      long toSeconds()
      Converts this duration to seconds.
      int toSecondsPart()
      Gets the seconds part of this duration.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getNano

        public int getNano()
        Returns:
        The nanoseconds component of this duration.
      • getSeconds

        public long getSeconds()
        Returns:
        The seconds component of this duration.
      • ofDays

        public static Duration ofDays​(long days)
                               throws java.lang.ArithmeticException
        Creates a duration representing specified number of days. A Day is assumed to always be 24 hours.
        Parameters:
        days - The number of days.
        Returns:
        The Duration representing the specified number of days.
        Throws:
        java.lang.ArithmeticException - if the input is outside the range possible to represent by a Duration
      • ofHours

        public static Duration ofHours​(long hours)
                                throws java.lang.ArithmeticException
        Creates a duration representing specified number of hours. An hour is assumed to always be 60 minutes.
        Parameters:
        hours - The number of hours.
        Returns:
        The Duration representing the specified number of hours.
        Throws:
        java.lang.ArithmeticException - if the input is outside the range possible to represent by a Duration
      • ofMinutes

        public static Duration ofMinutes​(long minutes)
                                  throws java.lang.ArithmeticException
        Creates a duration representing specified number of hours. A minute is assumed to always be 60 seconds.
        Parameters:
        minutes - The number of minutes.
        Returns:
        The Duration representing the specified number of minutes.
        Throws:
        java.lang.ArithmeticException - if the input is outside the range possible to represent by a Duration
      • ofSeconds

        public static Duration ofSeconds​(long seconds)
        Creates a duration representing specified number of seconds.
        Parameters:
        seconds - The number of seconds.
        Returns:
        The Duration representing the specified number of seconds.
      • ofSeconds

        public static Duration ofSeconds​(long seconds,
                                         long nanoAdjustment)
        Creates a duration representing specified number of seconds and an adjustment in nanoseconds.
        Parameters:
        seconds - The number of seconds.
        nanoAdjustment - The nanosecond adjustment to the number of seconds.
        Returns:
        The Duration representing the specified number of seconds, adjusted.
      • ofMillis

        public static Duration ofMillis​(long milliseconds)
        Creates a duration representing specified number of milliseconds.
        Parameters:
        milliseconds - The number of milliseconds.
        Returns:
        The Duration representing the specified number of milliseconds.
      • ofNanos

        public static Duration ofNanos​(long nanoseconds)
        Creates a duration representing specified number of nanoseconds.
        Parameters:
        nanoseconds - The number of nanoseconds.
        Returns:
        The Duration representing the specified number of nanoseconds.
      • toNanos

        public long toNanos()
                     throws java.lang.ArithmeticException
        Converts this duration to nanoseconds.
        Returns:
        Total number of nanoseconds in this duration.
        Throws:
        java.lang.ArithmeticException - if the resulting value cannot be represented by long type.
      • toNanosPart

        public int toNanosPart()
        Gets the nanoseconds part of this duration. Equals to getNano().
        Returns:
        The nanoseconds part of this duration, value from 0 to 999999999.
      • toMillis

        public long toMillis()
                      throws java.lang.ArithmeticException
        Converts this duration to milliseconds. Any data past milliseconds precision is simply discarded. There is no mathematical rounding, so a duration of 999999 nanoseconds will still be converted to 0 milliseconds.
        Returns:
        Total number of milliseconds in this duration.
        Throws:
        java.lang.ArithmeticException - if the resulting value cannot be represented by long type.
      • toMillisPart

        public int toMillisPart()
        Gets the milliseconds part of this duration.
        Returns:
        The milliseconds part of this duration.
      • toSeconds

        public long toSeconds()
        Converts this duration to seconds. Any data past seconds precision is simply discarded. There is no mathematical rounding, so a duration of 999 milliseconds will still be converted to 0 seconds.
        Returns:
        Total number of seconds in this duration.
      • toSecondsPart

        public int toSecondsPart()
        Gets the seconds part of this duration.
        Returns:
        The seconds part of this duration, value from 0 to 59.
      • toMinutes

        public long toMinutes()
        Converts this duration to minutes. Any data past minute precision is simply discarded. There is no mathematical rounding, so a duration of 59 seconds and 999 milliseconds will still be converted to 0 minutes.
        Returns:
        Total number of minutes in this duration.
      • toMinutesPart

        public int toMinutesPart()
        Gets the minutes part of this duration.
        Returns:
        The minutes part of this duration, value from 0 to 59.
      • toHours

        public long toHours()
        Converts this duration to hours. Any data past hour precision is simply discarded. There is no mathematical rounding, so a duration of 59 minutes and 59 seconds will still be converted to 0 hours.
        Returns:
        The number of full hours in this duration.
      • toHoursPart

        public int toHoursPart()
        Gets the hours part of this duration.
        Returns:
        The hours part of this duration, value from 0 to 23.
      • toDays

        public long toDays()
        Converts this duration to days. Any data past day precision is simply discarded. There is no mathematical rounding, so a duration of 23 hours 59 minutes and 59 seconds will still be converted to 0 days. Day is always assumed to be 24 hours.
        Returns:
        The number of full days in this duration.
      • toDaysPart

        public long toDaysPart()
        Same as toDays().
        Returns:
        The number of full days in this duration.
      • compareTo

        public int compareTo​(Duration duration)
        Specified by:
        compareTo in interface java.lang.Comparable<Duration>
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object