Calculates the period between this temporal and another temporal in
terms of the specified unit.
This calculates the period between two temporals in terms of a single unit.
The start and end points are
this and the specified temporal.
The result will be negative if the end is before the start.
For example, the period in hours between two temporal objects can be
calculated using
startTime.until(endTime, HOURS).
The calculation returns a whole number, representing the number of
complete units between the two temporals.
For example, the period in hours between the times 11:30 and 13:29
will only be one hour as it is one minute short of two hours.
There are two equivalent ways of using this method.
The first is to invoke this method directly.
The second is to use
TemporalUnit#between(Temporal,Temporal):
// these two lines are equivalent
between = thisUnit.between(start, end);
between = start.until(end, thisUnit);
The choice should be made based on which makes the code more readable.
For example, this method allows the number of days between two dates to be calculated:
long daysBetween = DAYS.between(start, end);
// or alternatively
long daysBetween = start.until(end, DAYS);
Specification for implementors
Implementations must begin by checking to ensure that the input temporal
object is of the same observable type as the implementation.
They must then perform the calculation for all instances of
ChronoUnit.
A
DateTimeException must be thrown for
ChronoUnitinstances that are unsupported.
If the unit is not a
ChronoUnit, then the result of this method
is obtained by invoking
TemporalUnit.between(Temporal, Temporal)passing
this as the first argument and the input temporal as
the second argument.
In summary, implementations must behave in a manner equivalent to this code:
// check input temporal is the same type as this class
if (unit instanceof ChronoUnit) {
// if unit is supported, then calculate and return result
// else throw DateTimeException for unsupported units
}
return unit.between(this, endTemporal);
The target object must not be altered by this method.