Java DateUtils.ceiling 和 DateUtils.truncate 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10591821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:43:35  来源:igfitidea点击:

Difference between Java DateUtils.ceiling and DateUtils.truncate

javadatetime

提问by cmcginty

It's not clear in the java doc what the difference between DateUtils.ceilingand DateUtils.truncateis. Is the java doc wrong? Can someone clarify this?

java doc 中不清楚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.

天花板

公共静态日期上限(日期日期,整数字段)

将此日期设为上限,将指定的字段保留为最重要的字段。

例如,如果您的日期时间为 2002 年 3 月 28 日 13:45:01.231,如果您通过 HOUR 传递,它将返回 2002 年 3 月 28 日 13:00:00.000。如果这与 MONTH 一起传递,它将返回 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 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.

截短

公共静态日期截断(日期日期,整数字段)

截断此日期,将指定的字段保留为最重要的字段。

例如,如果您的日期时间为 2002 年 3 月 28 日 13:45:01.231,如果您通过 HOUR 传递,它将返回 2002 年 3 月 28 日 13:00:00.000。如果这与 MONTH 一起传递,它将返回 1 Mar 2002 0:00:00.000。

采纳答案by Charlie

To add on to Jim's answer, I suspect there is a Javadoc error on the ceiling method. The description for ceiling(Date,int) was updated with the 3.0 javadoc(compare to the 2.5 javadocfor the same method)... and although the others weren't updated, that method uses code common to the Calendar version... Or using a simple test case you could see that they both behave the same (for me with 3.1 at least :) )

为了补充吉姆的回答,我怀疑天花板方法存在 Javadoc 错误。天花板(日期,整数)的描述已更新为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());
}

回答by Jim Barrows

The answer is in the documentation:

答案在文档中:

The truncate, ceiling and round methods could be considered the Math.floor(), Math.ceil() or Math.round versions for dates This way date-fields will be ignored in bottom-up order.

截断、天花板和舍入方法可以被视为日期的 Math.floor()、Math.ceil() 或 Math.round 版本。这样日期字段将按自下而上的顺序被忽略。

Which I would interpret as "You're correct, but there is a reason"

我会解释为“你是对的,但这是有原因的”

回答by Matt

The documentation on some of the older versions for the ceil() method is WRONG. It has been corrected at some point, and here's the docs from 3.1:

ceil() 方法的一些旧版本的文档是错误的。它已在某个时候得到纠正,这是 3.1 中的文档:

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 14:00:00.000. If this was passed with MONTH, it would return 1 Apr 2002 0:00:00.000.

So, while ceil() and trunc() both minimize all the remaining fields (in some cases sets to 0, but for MONTH it will set the day to 1), ceil() will actually increment the field you pass in by 1, whereas trunc will not.

因此,虽然 ceil() 和 trunc() 都最小化所有剩余的字段(在某些情况下设置为 0,但对于 MONTH 它将设置日期为 1), ceil() 实际上会将您传入的字段增加 1,而 trunc 不会。