Class Duration
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 (seegetNano()).Duration can be created from various units of time by calling on of
of*methods. Theto*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 intcompareTo(Duration duration)booleanequals(java.lang.Object o)intgetNano()longgetSeconds()inthashCode()static DurationofDays(long days)Creates a duration representing specified number of days.static DurationofHours(long hours)Creates a duration representing specified number of hours.static DurationofMillis(long milliseconds)Creates a duration representing specified number of milliseconds.static DurationofMinutes(long minutes)Creates a duration representing specified number of hours.static DurationofNanos(long nanoseconds)Creates a duration representing specified number of nanoseconds.static DurationofSeconds(long seconds)Creates a duration representing specified number of seconds.static DurationofSeconds(long seconds, long nanoAdjustment)Creates a duration representing specified number of seconds and an adjustment in nanoseconds.longtoDays()Converts this duration to days.longtoDaysPart()Same astoDays().longtoHours()Converts this duration to hours.inttoHoursPart()Gets the hours part of this duration.longtoMillis()Converts this duration to milliseconds.inttoMillisPart()Gets the milliseconds part of this duration.longtoMinutes()Converts this duration to minutes.inttoMinutesPart()Gets the minutes part of this duration.longtoNanos()Converts this duration to nanoseconds.inttoNanosPart()Gets the nanoseconds part of this duration.longtoSeconds()Converts this duration to seconds.inttoSecondsPart()Gets the seconds part of this duration.
-
-
-
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.ArithmeticExceptionConverts this duration to nanoseconds.- Returns:
- Total number of nanoseconds in this duration.
- Throws:
java.lang.ArithmeticException- if the resulting value cannot be represented bylongtype.
-
toNanosPart
public int toNanosPart()
Gets the nanoseconds part of this duration. Equals togetNano().- Returns:
- The nanoseconds part of this duration, value from 0 to 999999999.
-
toMillis
public long toMillis() throws java.lang.ArithmeticExceptionConverts 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 bylongtype.
-
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 astoDays().- Returns:
- The number of full days in this duration.
-
compareTo
public int compareTo(Duration duration)
- Specified by:
compareToin interfacejava.lang.Comparable<Duration>
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-