Java HOUR_OF_DAY 的范围是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23168805/
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
what is the range of HOUR_OF_DAY?
提问by Sander
A simple question, but i couldn't find it:
一个简单的问题,但我找不到它:
Is the range of HOUR_OF_DAY between 0 and 23, or 1 and 24?
HOUR_OF_DAY 的范围是在 0 到 23 之间,还是在 1 到 24 之间?
I want a random HOUR_OF_DAY, do I need:
我想要一个随机的 HOUR_OF_DAY,我需要:
int randomHour = (int) (Math.random()*24);
or
或者
int randomHour = (int) (Math.random()*24+1);
采纳答案by T.J. Crowder
From the documentation:
从文档:
Field number for get and set indicating the hour of the day.
HOUR_OF_DAY
is used for the 24-hour clock. E.g., at 10:04:15.250 PM theHOUR_OF_DAY
is 22.
get 和 set 的字段编号指示一天中的小时。
HOUR_OF_DAY
用于 24 小时制。例如,在 10:04:15.250 PMHOUR_OF_DAY
是 22。
If 10:04:15.250 PM is HOUR_OF_DAY
22, That would make the range 0
- 23
. If it were 1
to 24
, 10 p.m. would be 23. And that would be wrong on so many levels. :-)
如果 10:04:15.250 PM 是HOUR_OF_DAY
22,那将使范围0
- 23
。如果是1
这样24
,晚上 10 点就是 23 点。这在很多层面上都是错误的。:-)
回答by Saurabh Gupta
It is 0-23.
它是 0-23。
I have tested it by running below code
我已经通过运行以下代码对其进行了测试
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 24);
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
Output is 0.
输出为 0。
If you run
如果你跑
calendar.set(Calendar.HOUR_OF_DAY, 23);
Output will be 23.
输出将为 23。
If you run
如果你跑
calendar.set(Calendar.HOUR_OF_DAY, 25);
Output will be 1.
输出将为 1。
回答by Mujahid
The Range is between 0 to 23
范围在 0 到 23 之间
Run this
运行这个
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY);
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
If the time is 12 AM the output will be 0
如果时间是 12 AM,则输出将为 0
and if the time is 11 PM the output will be 23
如果时间是晚上 11 点,输出将为 23