在 Java 中截断持续时间
Truncate a Duration in Java
将经过的时间捕获为 Duration
时,我只关心整秒分辨率。
如何从 Duration
对象中删除小数秒?
java.time 框架中的其他 类 提供了一种 truncatedTo
方法。但是我在 Duration
.
上没有看到
Java 9 及更高版本
Java 9 为在 Java 8.java.time 类 中首次亮相的
Java 9 带来了一些次要功能和错误修复。 =23=]
其中一个功能是添加 Duration::truncatedTo
method, similar to such methods seen on other classes. Pass a ChronoUnit
(an implementation of TemporalUnit
接口)以指定要截断的粒度。
Duration d = myDuration.truncatedTo( ChronoUnit.SECONDS ) ;
Java 8
如果您正在使用 Java 8 并且还不能移动到 Java 9、10、11 或更高版本,请自行计算截断。
致电minusNanos
method found on the Java 8 version of Duration
。获取 Duration
对象的纳秒数,然后减去该纳秒数。
Duration d = myDuration.minusNanos( myDuration.getNano() ) ;
java.time类使用immutable objects模式。因此,您可以在不改变(“变异”)原始对象的情况下取回一个全新的对象。
我喜欢。我知道这不是你问的,但我想为 Java 8 提供一个或两个选项,用于我们想要截断为秒以外的单位的情况。
如果我们在编写代码时知道单位,我们可以结合toXx
和ofXx
方法来形成截断的持续时间:
Duration d = Duration.ofMillis(myDuration.toMillis());
Duration d = Duration.ofSeconds(myDuration.toSeconds());
Duration d = Duration.ofMinutes(myDuration.toMinutes());
Duration d = Duration.ofHours(myDuration.toHours());
Duration d = Duration.ofDays(myDuration.toDays());
如果单位是可变的,我们可能会根据您提到的 Java 9 方法的实现调整代码,truncatedTo
:
Duration d;
if (unit.equals(ChronoUnit.SECONDS)
&& (myDuration.getSeconds() >= 0 || myDuration.getNano() == 0)) {
d = Duration.ofSeconds(myDuration.getSeconds());
} else if (unit == ChronoUnit.NANOS) {
d = myDuration;
}
Duration unitDur = unit.getDuration();
if (unitDur.getSeconds() > TimeUnit.DAYS.toSeconds(1)) {
throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
}
long dur = unitDur.toNanos();
if ((TimeUnit.DAYS.toNanos(1) % dur) != 0) {
throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
}
long nod = (myDuration.getSeconds() % TimeUnit.DAYS.toSeconds(1)) * TimeUnit.SECONDS.toNanos(1)
+ myDuration.getNano();
long result = (nod / dur) * dur;
d = myDuration.plusNanos(result - nod);
原始方法使用了 Duration
class 中的一些私有内容,因此需要进行一些更改。该代码只接受 ChronoUnit
个单位,不接受其他 TemporalUnit
个单位。我还没有考虑过概括它有多难。
将经过的时间捕获为 Duration
时,我只关心整秒分辨率。
如何从 Duration
对象中删除小数秒?
java.time 框架中的其他 类 提供了一种 truncatedTo
方法。但是我在 Duration
.
Java 9 及更高版本
Java 9 为在 Java 8.java.time 类 中首次亮相的
Java 9 带来了一些次要功能和错误修复。 =23=]
其中一个功能是添加 Duration::truncatedTo
method, similar to such methods seen on other classes. Pass a ChronoUnit
(an implementation of TemporalUnit
接口)以指定要截断的粒度。
Duration d = myDuration.truncatedTo( ChronoUnit.SECONDS ) ;
Java 8
如果您正在使用 Java 8 并且还不能移动到 Java 9、10、11 或更高版本,请自行计算截断。
致电minusNanos
method found on the Java 8 version of Duration
。获取 Duration
对象的纳秒数,然后减去该纳秒数。
Duration d = myDuration.minusNanos( myDuration.getNano() ) ;
java.time类使用immutable objects模式。因此,您可以在不改变(“变异”)原始对象的情况下取回一个全新的对象。
我喜欢
如果我们在编写代码时知道单位,我们可以结合toXx
和ofXx
方法来形成截断的持续时间:
Duration d = Duration.ofMillis(myDuration.toMillis());
Duration d = Duration.ofSeconds(myDuration.toSeconds());
Duration d = Duration.ofMinutes(myDuration.toMinutes());
Duration d = Duration.ofHours(myDuration.toHours());
Duration d = Duration.ofDays(myDuration.toDays());
如果单位是可变的,我们可能会根据您提到的 Java 9 方法的实现调整代码,truncatedTo
:
Duration d;
if (unit.equals(ChronoUnit.SECONDS)
&& (myDuration.getSeconds() >= 0 || myDuration.getNano() == 0)) {
d = Duration.ofSeconds(myDuration.getSeconds());
} else if (unit == ChronoUnit.NANOS) {
d = myDuration;
}
Duration unitDur = unit.getDuration();
if (unitDur.getSeconds() > TimeUnit.DAYS.toSeconds(1)) {
throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
}
long dur = unitDur.toNanos();
if ((TimeUnit.DAYS.toNanos(1) % dur) != 0) {
throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
}
long nod = (myDuration.getSeconds() % TimeUnit.DAYS.toSeconds(1)) * TimeUnit.SECONDS.toNanos(1)
+ myDuration.getNano();
long result = (nod / dur) * dur;
d = myDuration.plusNanos(result - nod);
原始方法使用了 Duration
class 中的一些私有内容,因此需要进行一些更改。该代码只接受 ChronoUnit
个单位,不接受其他 TemporalUnit
个单位。我还没有考虑过概括它有多难。