gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:11:16 29 4
gpt4 key购买 nike

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.

对比

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.

最佳答案

为了补充 Jim 的回答,我怀疑 ceiling 方法存在 Javadoc 错误。 ceiling(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());
}

关于java - Java DateUtils.ceiling 和 DateUtils.truncate 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10591821/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com