Java 如何将 Date 对象的时间设置为零 (00/00/00)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2058099/
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-08-13 03:05:30  来源:igfitidea点击:

How to set time on a Date object to be zero (00/00/00)

javadatetime

提问by Milli

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(0);

This sets it to the deafult value. How do I set it to zero?

这将其设置为默认值。我如何将其设置为零?

回答by matt b

You should probably be aware of what setTimeInMillis()expects:

您可能应该知道setTimeInMillis()期望的是什么:

public void setTimeInMillis(long millis)

Sets this Calendar's current time from the given long value.

Parameters:
millis- the new time in UTC milliseconds from the epoch.

public void setTimeInMillis(long millis)

根据给定的 long 值设置此日历的当前时间。

参数:
millis- 从纪元开始的以 UTC 毫秒为单位的新时间。

The epoch is defined as:

纪元定义为:

An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).

时间瞬间可以用毫秒值表示,该值是与 Epoch, 1970 年 1 月 1 日 00:00:00.000 GMT(格里高利)的偏移量。

回答by matt b

You can zero-out what value you want using the set method. For example:

您可以使用 set 方法将所需的值归零。例如:

calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

回答by McDowell

(00/00/00)is not a valid date; the API will not produce this value.

(00/00/00)不是有效日期;API 不会产生这个值。