Class Duration
java.lang.Object
java.time.Duration
- All Implemented Interfaces:
Comparable<Duration>, TemporalAmount
An amount of time in seconds and nanoseconds, such as 34.5 seconds.
Arithmetic is exact: a result longer than a Duration holds throws
ArithmeticException rather than wrapping around, as the JDK's does.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionabs()This duration without its sign.A zero duration handstemporalback untouched, null included: JDK 8, the conformance host, does the same and throws nothing.static DurationThe duration from one value to another of the same kind, negative whenendExclusivecomes first.intCompares this object to the specified object to determine their relative order.dividedBy(long divisor) This duration divided bydivisor, the fraction of a nanosecond dropped towards zero.booleanIndicates whether some other object is "equal to" this one.static Durationfrom(TemporalAmount amount) The durationamountadds up to.longget(TemporalUnit unit) The amount ofunitheld: the seconds or the nanoseconds.intgetNano()The nanoseconds within the second, 0 to 999,999,999.longThe whole seconds, rounded towards negative infinity.getUnits()Seconds and nanoseconds, the two units a duration is made of.inthashCode()Returns a hash code value for the object.booleanWhether this is shorter than zero.booleanisZero()Whether this is zero long.minus(long amountToSubtract, TemporalUnit unit) minusDays(long daysToSubtract) minusHours(long hoursToSubtract) minusMillis(long millisToSubtract) minusMinutes(long minutesToSubtract) minusNanos(long nanosToSubtract) minusSeconds(long secondsToSubtract) multipliedBy(long multiplicand) This duration multiplied bymultiplicand, orArithmeticExceptionwhen the product is longer than a duration holds.negated()This duration with its sign reversed.static Durationof(long amount, TemporalUnit unit) A duration ofamountofunit.static DurationofDays(long days) A duration ofdaysstandard 24 hour days.static DurationofHours(long hours) A duration ofhourshours.static DurationofMillis(long millis) A duration ofmillismilliseconds.static DurationofMinutes(long minutes) A duration ofminutesminutes.static DurationofNanos(long nanos) A duration ofnanosnanoseconds.static DurationofSeconds(long seconds) A duration ofsecondsseconds.static DurationofSeconds(long seconds, long nanoAdjustment) A duration ofsecondsseconds adjusted bynanoAdjustmentnanoseconds, which may take it across any number of seconds.static Durationparse(CharSequence text) plus(long amountToAdd, TemporalUnit unit) This duration plusamountToAddofunit, which must have an exact length.plusDays(long daysToAdd) plusHours(long hoursToAdd) plusMillis(long millisToAdd) plusMinutes(long minutesToAdd) plusNanos(long nanosToAdd) plusSeconds(long secondsToAdd) subtractFrom(Temporal temporal) temporalmoved back by this amount.longtoDays()The whole days, of 24 hours.longThe whole days; the same astoDays().longtoHours()The whole hours.intThe hours left over after the whole days.longtoMillis()The whole milliseconds, orArithmeticExceptionpast what a long holds.intThe milliseconds within the second.longThe whole minutes.intThe minutes left over after the whole hours.longtoNanos()The whole nanoseconds, orArithmeticExceptionpast what a long holds (about 292 years).intThe nanoseconds within the second; the same asgetNano().longThe whole seconds; the same asgetSeconds().intThe seconds left over after the whole minutes.toString()Returns a string representation of the object.withNanos(int nanoOfSecond) This duration withnanoOfSecondnanoseconds within the second.withSeconds(long seconds) This duration withsecondswhole seconds.
-
Field Details
-
ZERO
A duration of zero.
-
-
Method Details
-
ofDays
A duration ofdaysstandard 24 hour days. -
ofHours
A duration ofhourshours. -
ofMinutes
A duration ofminutesminutes. -
ofSeconds
A duration ofsecondsseconds. -
ofSeconds
A duration ofsecondsseconds adjusted bynanoAdjustmentnanoseconds, which may take it across any number of seconds. -
ofMillis
A duration ofmillismilliseconds. -
ofNanos
A duration ofnanosnanoseconds. -
of
A duration ofamountofunit. The unit must have an exact length: a day or anything shorter. -
from
The durationamountadds up to. Every unit it is made of must have an exact length. -
between
-
parse
-
get
The amount ofunitheld: the seconds or the nanoseconds.- Specified by:
getin interfaceTemporalAmount
-
getUnits
Seconds and nanoseconds, the two units a duration is made of.- Specified by:
getUnitsin interfaceTemporalAmount
-
addTo
A zero duration handstemporalback untouched, null included: JDK 8, the conformance host, does the same and throws nothing.- Specified by:
addToin interfaceTemporalAmount
-
subtractFrom
Description copied from interface:TemporalAmounttemporalmoved back by this amount.- Specified by:
subtractFromin interfaceTemporalAmount
-
isZero
public boolean isZero()Whether this is zero long. -
isNegative
public boolean isNegative()Whether this is shorter than zero. -
getSeconds
public long getSeconds()The whole seconds, rounded towards negative infinity. -
getNano
public int getNano()The nanoseconds within the second, 0 to 999,999,999. -
withSeconds
This duration withsecondswhole seconds. -
withNanos
This duration withnanoOfSecondnanoseconds within the second. -
plus
-
plus
This duration plusamountToAddofunit, which must have an exact length. -
plusDays
-
plusHours
-
plusMinutes
-
plusSeconds
-
plusMillis
-
plusNanos
-
minus
-
minus
-
minusDays
-
minusHours
-
minusMinutes
-
minusSeconds
-
minusMillis
-
minusNanos
-
multipliedBy
This duration multiplied bymultiplicand, orArithmeticExceptionwhen the product is longer than a duration holds. -
dividedBy
This duration divided bydivisor, the fraction of a nanosecond dropped towards zero. Exact for every duration; only a quotient that is not a duration throwsArithmeticException. -
negated
This duration with its sign reversed. The most negative duration has no positive counterpart and throwsArithmeticException. -
abs
This duration without its sign. -
toDays
public long toDays()The whole days, of 24 hours. -
toHours
public long toHours()The whole hours. -
toMinutes
public long toMinutes()The whole minutes. -
toSeconds
public long toSeconds()The whole seconds; the same asgetSeconds(). -
toMillis
public long toMillis()The whole milliseconds, orArithmeticExceptionpast what a long holds. -
toNanos
public long toNanos()The whole nanoseconds, orArithmeticExceptionpast what a long holds (about 292 years). -
toDaysPart
public long toDaysPart()The whole days; the same astoDays(). -
toHoursPart
public int toHoursPart()The hours left over after the whole days. -
toMinutesPart
public int toMinutesPart()The minutes left over after the whole hours. -
toSecondsPart
public int toSecondsPart()The seconds left over after the whole minutes. -
toMillisPart
public int toMillisPart()The milliseconds within the second. -
toNanosPart
public int toNanosPart()The nanoseconds within the second; the same asgetNano(). -
compareTo
Description copied from interface:ComparableCompares this object to the specified object to determine their relative order.
Parameters
another: the object to compare to this instance.
Returns
- Specified by:
compareToin interfaceComparable<Duration>- Returns:
a negative integer if this instance is less than
another; a positive integer if this instance is greater thananother; 0 if this instance has the same order asanother.Throws
ClassCastException: @throws ClassCastException ifanothercannot be converted into something comparable tothisinstance.
-
equals
Description copied from class:ObjectIndicates whether some other object is "equal to" this one. The equals method implements an equivalence relation: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true). -
hashCode
public int hashCode()Description copied from class:ObjectReturns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.) -
toString
Description copied from class:ObjectReturns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
-