本文整理匯總了Java中java.time.temporal.Temporal.until方法的典型用法代碼示例。如果您正苦於以下問題:Java Temporal.until方法的具體用法?Java Temporal.until怎麽用?Java Temporal.until使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.time.temporal.Temporal
的用法示例。
在下文中一共展示了Temporal.until方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: between
import java.time.temporal.Temporal; //導入方法依賴的package包/類
/**
* Obtains a {@code Duration} representing the duration between two temporal objects.
* <p>
* This calculates the duration between two temporal objects. If the objects
* are of different types, then the duration is calculated based on the type
* of the first object. For example, if the first argument is a {@code LocalTime}
* then the second argument is converted to a {@code LocalTime}.
* <p>
* The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
* For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
* {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
* <p>
* The result of this method can be a negative period if the end is before the start.
* To guarantee to obtain a positive duration call {@link #abs()} on the result.
*
* @param startInclusive the start instant, inclusive, not null
* @param endExclusive the end instant, exclusive, not null
* @return a {@code Duration}, not null
* @throws DateTimeException if the seconds between the temporals cannot be obtained
* @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
*/
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
try {
return ofNanos(startInclusive.until(endExclusive, NANOS));
} catch (DateTimeException | ArithmeticException ex) {
long secs = startInclusive.until(endExclusive, SECONDS);
long nanos;
try {
nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
if (secs > 0 && nanos < 0) {
secs++;
} else if (secs < 0 && nanos > 0) {
secs--;
}
} catch (DateTimeException ex2) {
nanos = 0;
}
return ofSeconds(secs, nanos);
}
}