Package com.here.time

Class Duration

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

public final class Duration extends Object implements 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

    Modifier and Type
    Method
    Description
    int
    compareTo(Duration duration)
     
    boolean
     
    int
     
    long
     
    int
     
    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
    Converts this duration to days.
    long
    Same as toDays().
    long
    Converts this duration to hours.
    int
    Gets the hours part of this duration.
    long
    Converts this duration to milliseconds.
    int
    Gets the milliseconds part of this duration.
    long
    Converts this duration to minutes.
    int
    Gets the minutes part of this duration.
    long
    Converts this duration to nanoseconds.
    int
    Gets the nanoseconds part of this duration.
    long
    Converts this duration to seconds.
    int
    Gets the seconds part of this duration.

    Methods inherited from class java.lang.Object

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

    • 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 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:
      ArithmeticException - if the input is outside the range possible to represent by a Duration
    • ofHours

      public static Duration ofHours(long hours) throws 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:
      ArithmeticException - if the input is outside the range possible to represent by a Duration
    • ofMinutes

      public static Duration ofMinutes(long minutes) throws 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:
      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 ArithmeticException
      Converts this duration to nanoseconds.
      Returns:
      Total number of nanoseconds in this duration.
      Throws:
      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 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:
      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 Comparable<Duration>
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object