Java日期舍入 [英] Java Date rounding

查看:136
本文介绍了Java日期舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一种优雅的方法将java日期向上或向下舍入到最接近的分钟(或秒,小时,天)。

I would like an elegant way to round a java Date up or down to the nearest minute (or second, hour, day).

例如日期1月25日星期三格林尼治标准时间10:36:34四舍五入到最接近的分钟将是Wed 1月25日10:37:00 GMT 2012

For example a date of "Wed Jan 25 10:36:34 GMT 2012" rounded up to the nearest minute would be "Wed Jan 25 10:37:00 GMT 2012"

推荐答案

如果你使用Apache commons-lang ,你可以使用DateUtils来整理日期

If you use Apache commons-lang, you can use DateUtils to round your dates:

Date now = new Date();
Date nearestMinute = DateUtils.round(now, Calendar.MINUTE);

这篇关于Java日期舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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