Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the range of HOUR_OF_DAY?

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);
like image 490
Sander Avatar asked Apr 19 '14 10:04

Sander


3 Answers

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. :-)

like image 96
T.J. Crowder Avatar answered Oct 21 '22 22:10

T.J. Crowder


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.

like image 23
Saurabh Gupta Avatar answered Oct 21 '22 23:10

Saurabh Gupta


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

like image 4
Mujahid Avatar answered Oct 21 '22 22:10

Mujahid