本文整理汇总了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);
}
}