A simple question, but i couldn't find it:
Is the range of HOUR_OF_DAY between 0 and 23, or 1 and 24?
I want a random HOUR_OF_DAY, do I need:
int randomHour = (int) (Math.random()*24);
or
int randomHour = (int) (Math.random()*24+1);
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 the HOUR_OF_DAY
is 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. :-)
It is 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.
If you run
calendar.set(Calendar.HOUR_OF_DAY, 23);
Output will be 23.
If you run
calendar.set(Calendar.HOUR_OF_DAY, 25);
Output will be 1.
The Range is between 0 to 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
and if the time is 11 PM the output will be 23