Java DateUtils.ceiling和DateUtils.truncate之间的区别

前端之家收集整理的这篇文章主要介绍了Java DateUtils.ceiling和DateUtils.truncate之间的区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
java文档中不清楚 DateUtils.ceilingDateUtils.truncate之间的区别是什么. java文档是错误的吗?有人可以澄清一下吗

ceiling

public static Date ceiling(Date date,
int field)

Ceil this date,leaving the field specified as the most significant field.

For example,if you had the datetime of 28 Mar 2002 13:45:01.231,if you
passed with HOUR,it would return 28 Mar 2002 13:00:00.000. If this was
passed with MONTH,it would return 1 Mar 2002 0:00:00.000.

VS

truncate

public static Date truncate(Date date,
int field)

Truncate this date,leaving the field specified as the most
significant field.

For example,
if you passed with HOUR,it would return 28 Mar 2002 13:00:00.000.
If this was passed with MONTH,it would return 1 Mar 2002 0:00:00.000.

解决方法

为了补充Jim的答案,我怀疑天花板方法有一个Javadoc错误.对于上限(Date,int)的描述更新为 3.0 javadoc(与 2.5 javadoc相同的方法相比)…虽然其他更新没有更新,但该方法使用日历版本通用的代码…或使用一个简单的测试用例,您可以看到它们的行为相同(对于我,至少3.1))
@Test
public void testCeil() {
    final Calendar date = new GregorianCalendar();
    date.clear();
    date.set(2002,3,28,13,45,01);

    System.out.println(date.getTime());
    System.out.println(DateUtils.ceiling(date,Calendar.HOUR).getTime());
    System.out.println(DateUtils.ceiling(date.getTime(),Calendar.HOUR));
    System.out.println(DateUtils.truncate(date,Calendar.HOUR).getTime());
    System.out.println(DateUtils.truncate(date.getTime(),Calendar.HOUR));
    System.out.println(date.getTime());
}
原文链接:https://www.f2er.com/java/123589.html

猜你在找的Java相关文章