Java 即时舍入到下一秒
Java Instant round up to the next second
使用 Java Instant class,如何四舍五入到最接近的秒数?不管是1毫秒、15毫秒还是999毫秒,都应该四舍五入到下一秒0毫秒。
基本都想要,
Instant myInstant = ...
myInstant.truncatedTo(ChronoUnit.SECONDS);
但方向相反
您可以使用 .getNano
来覆盖极端情况,以确保时间在秒上不完全均匀,然后在有值要截断时使用 .plusSeconds()
添加额外的秒。
Instant myInstant = Instant.now();
if (myInstant.getNano() > 0) //Checks for any nanoseconds for the current second (this will almost always be true)
{
myInstant = myInstant.truncatedTo(ChronoUnit.SECONDS).plusSeconds(1);
}
/* else //Rare case where nanoseconds are exactly 0
{
myInstant = myInstant;
} */
我在 else
语句中留下只是为了演示如果正好是 0 纳秒则不需要执行任何操作,因为没有理由不截断任何内容。
编辑:如果你想检查时间是否在一秒内至少为 1 毫秒以便向上舍入,而不是 1 纳秒,你可以将它与 1000000 进行比较纳秒,但保留 else
语句以截断纳秒:
Instant myInstant = Instant.now();
if (myInstant.getNano() > 1000000) //Nano to milliseconds
{
myInstant = myInstant.truncatedTo(ChronoUnit.SECONDS).plusSeconds(1);
}
else
{
myInstant = myInstant.truncatedTo(ChronoUnit.SECONDS); //Must truncate the nanoseconds off since we are comparing to milliseconds now.
}
您可以使用 lambda functional programming 流方法使其成为一个衬垫。
添加第二个并截断。为了涵盖恰好在一秒的极端情况,检查截断为原始的,如果它们不同则只添加一秒:
Instant myRoundedUpInstant = Optional.of(myInstant.truncatedTo(ChronoUnit.SECONDS))
.filter(myInstant::equals)
.orElse(myInstant.truncatedTo(ChronoUnit.SECONDS).plusSeconds(1));
看到这个code run line at IdeOne.com。
Instant.toString(): 2019-07-30T20:06:33.456424Z
myRoundedUpInstant(): 2019-07-30T20:06:34Z
……和……
myInstant.toString(): 2019-07-30T20:05:20Z
myRoundedUpInstant(): 2019-07-30T20:05:20Z
或者,采用稍微不同的方法:
Instant myRoundedUpInstant = Optional.of(myInstant)
.filter(t -> t.getNano() != 0)
.map(t -> t.truncatedTo(ChronoUnit.SECONDS).plusSeconds(1))
.orElse(myInstant);
看到这个code run live at IdeOne.com。
myInstant.toString(): 2019-07-30T20:09:07.415043Z
myRoundedUpInstant(): 2019-07-30T20:09:08Z
……和……
myInstant.toString(): 2019-07-30T19:44:06Z
myRoundedUpInstant(): 2019-07-30T19:44:06Z
以上当然是在Java8地。如果 Optional
不是你的菜,我会把它作为 reader 的练习,将其拆分为更传统的 if/else :-)
使用 Java Instant class,如何四舍五入到最接近的秒数?不管是1毫秒、15毫秒还是999毫秒,都应该四舍五入到下一秒0毫秒。
基本都想要,
Instant myInstant = ...
myInstant.truncatedTo(ChronoUnit.SECONDS);
但方向相反
您可以使用 .getNano
来覆盖极端情况,以确保时间在秒上不完全均匀,然后在有值要截断时使用 .plusSeconds()
添加额外的秒。
Instant myInstant = Instant.now();
if (myInstant.getNano() > 0) //Checks for any nanoseconds for the current second (this will almost always be true)
{
myInstant = myInstant.truncatedTo(ChronoUnit.SECONDS).plusSeconds(1);
}
/* else //Rare case where nanoseconds are exactly 0
{
myInstant = myInstant;
} */
我在 else
语句中留下只是为了演示如果正好是 0 纳秒则不需要执行任何操作,因为没有理由不截断任何内容。
编辑:如果你想检查时间是否在一秒内至少为 1 毫秒以便向上舍入,而不是 1 纳秒,你可以将它与 1000000 进行比较纳秒,但保留 else
语句以截断纳秒:
Instant myInstant = Instant.now();
if (myInstant.getNano() > 1000000) //Nano to milliseconds
{
myInstant = myInstant.truncatedTo(ChronoUnit.SECONDS).plusSeconds(1);
}
else
{
myInstant = myInstant.truncatedTo(ChronoUnit.SECONDS); //Must truncate the nanoseconds off since we are comparing to milliseconds now.
}
您可以使用 lambda functional programming 流方法使其成为一个衬垫。
添加第二个并截断。为了涵盖恰好在一秒的极端情况,检查截断为原始的,如果它们不同则只添加一秒:
Instant myRoundedUpInstant = Optional.of(myInstant.truncatedTo(ChronoUnit.SECONDS))
.filter(myInstant::equals)
.orElse(myInstant.truncatedTo(ChronoUnit.SECONDS).plusSeconds(1));
看到这个code run line at IdeOne.com。
Instant.toString(): 2019-07-30T20:06:33.456424Z
myRoundedUpInstant(): 2019-07-30T20:06:34Z
……和……
myInstant.toString(): 2019-07-30T20:05:20Z
myRoundedUpInstant(): 2019-07-30T20:05:20Z
或者,采用稍微不同的方法:
Instant myRoundedUpInstant = Optional.of(myInstant)
.filter(t -> t.getNano() != 0)
.map(t -> t.truncatedTo(ChronoUnit.SECONDS).plusSeconds(1))
.orElse(myInstant);
看到这个code run live at IdeOne.com。
myInstant.toString(): 2019-07-30T20:09:07.415043Z
myRoundedUpInstant(): 2019-07-30T20:09:08Z
……和……
myInstant.toString(): 2019-07-30T19:44:06Z
myRoundedUpInstant(): 2019-07-30T19:44:06Z
以上当然是在Java8地。如果 Optional
不是你的菜,我会把它作为 reader 的练习,将其拆分为更传统的 if/else :-)