JodaTime向下舍入到最近的四分之一小时 [英] JodaTime rounding down to nearest quarter of hour
本文介绍了JodaTime向下舍入到最近的四分之一小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果时间是10:36,我想将时间四舍五入到10:30.如果时间是1050,我想将时间四舍五入到10:45.等等...我不确定该怎么做.有什么想法吗?
If the time is 10:36 I would like to round the time down to 10:30. If the time is 1050 I would like to round the time down to 10:45. etc... I am not sure how to do this. Any ideas?
推荐答案
如何?
public static LocalTime roundToQuarterHour(LocalTime time) {
int oldMinute = time.getMinuteOfHour();
int newMinute = 15 * (int) Math.round(oldMinute / 15.0);
return time.plusMinutes(newMinute - oldMinute);
}
(由于存在withMinuteOfHour
方法,因此似乎有些复杂,但是请记住,我们可能会四舍五入到60,并且withMinuteOfHour(60)
是无效的.)
(It may seem slightly overcomplicated since there's a withMinuteOfHour
method, but keep in mind that we might round to 60, and withMinuteOfHour(60)
is invalid.)
这篇关于JodaTime向下舍入到最近的四分之一小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文