加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:03:52 所属栏目:Java 来源:网络整理
导读:在 java文档中不清楚 DateUtils.ceiling和 DateUtils.truncate之间的区别是什么. java文档是错误的吗?有人可以澄清一下吗 ceiling public static Date ceiling(Date date, int field) Ceil this date,leaving the field specified as the most significant
在 java文档中不清楚 DateUtils.ceiling和 DateUtils.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());
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读