使用Java 8分钟到天数 [英] Round minutes to ceiling using Java 8

查看:165
本文介绍了使用Java 8分钟到天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我很幸运地使用Java 8和新的时间APi,但是我没有看到任何四舍五入的功能...



基本上如果时间是...

  2014-08-28T10:01.00.000 ----> 2014-08-28T10:02.00.000 
2014-08-28T10:01.10.123 ----> 2014-08-28T10:02.00.000
2014-08-28T10:01.25.123 ----> 2014-08-28T10:02.00.000
2014-08-28T10:01.49.123 ----> 2014-08-28T10:02.00.000
2014-08-28T10:01.59.999 ----> 2014-08-28T10:02.00.000

这似乎没关系, / p>

  LocalDateTime now = LocalDateTime.now(Clock.systemUTC()); 
LocalDateTime newTime = now.plusMinutes(1);

System.out.println(newTime.toString());
System.out.println(newTime.format(DateTimeFormatter.ofPattern(yyyy-dd-MM'T'HH:mm:00.000)));


解决方案

java.time API不支持四舍五入,但它支持四舍五入(截断),可以实现所需的行为(不完全是四舍五入):

  LocalDateTime now = LocalDateTime.now(); 
LocalDateTime roundFloor = now.truncatedTo(ChronoUnit.MINUTES);
LocalDateTime roundCeiling = now.truncatedTo(ChronoUnit.MINUTES).plusMinutes(1);

此外,还有一个工具可以获得一分钟只能打勾一次的时钟,感兴趣的:

  Clock minuteTickingClock = Clock.tickMinutes(ZoneId.systemDefault()); 
LocalDateTime now = LocalDateTime.now(minuteTickingClock);
LocalDateTime roundCeiling = now.plusMinutes(1);

此时钟将自动截断分钟到楼层(尽管是指定,以便可能会返回延迟的缓存值)。请注意,如果需要, Clock 可以存储在静态变量中。



最后,如果这是一个常见的操作您想在多个地方使用,可以编写一个库 TemporalAdjuster 功能执行舍入。 (调整器可以写一次,测试,并作为静态变量或方法使用)。


So I'm lucky enough to use Java 8 and the new time APi but I don't see any rounding functions...

Basically if the time is...

2014-08-28T10:01.00.000 ----> 2014-08-28T10:02.00.000
2014-08-28T10:01.10.123 ----> 2014-08-28T10:02.00.000
2014-08-28T10:01.25.123 ----> 2014-08-28T10:02.00.000
2014-08-28T10:01.49.123 ----> 2014-08-28T10:02.00.000
2014-08-28T10:01.59.999 ----> 2014-08-28T10:02.00.000

This seems to be ok, but is it right?

LocalDateTime now =  LocalDateTime.now(Clock.systemUTC());
LocalDateTime newTime =  now.plusMinutes(1);

System.out.println(newTime.toString());
System.out.println(newTime.format(DateTimeFormatter.ofPattern("yyyy-dd-MM'T'HH:mm:00.000")));

解决方案

The java.time API does not support rounding to ceiling, however it does support rounding to floor (truncation) which enables the desired behaviour (which isn't exactly rounding to ceiling):

LocalDateTime now =  LocalDateTime.now();
LocalDateTime roundFloor =  now.truncatedTo(ChronoUnit.MINUTES);
LocalDateTime roundCeiling =  now.truncatedTo(ChronoUnit.MINUTES).plusMinutes(1);

In addition, there is a facility to obtain a clock that only ticks once a minute, which may be of interest:

Clock minuteTickingClock = Clock.tickMinutes(ZoneId.systemDefault());
LocalDateTime now =  LocalDateTime.now(minuteTickingClock);
LocalDateTime roundCeiling =  now.plusMinutes(1);

This clock will automatically truncate minutes to floor (although it is specified such that it may return a delayed cached value). Note that a Clock may be stored in a static variable if desired.

Finally, if this is a common operation that you want to use in multiple places, it is possible to write a library TemporalAdjuster function to perform the rounding. (Adjusters can be written once, tested, and made available as a static variable or method).

这篇关于使用Java 8分钟到天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆