Search code examples
javaandroidcalendar

What is the range of HOUR_OF_DAY?


A simple question, but I couldn't find an answer to it:

Is the range of HOUR_OF_DAY between 0 and 23, or 1 and 24?

If I want a random HOUR_OF_DAY, do I need:

int randomHour = (int) (Math.random() * 24);

or

int randomHour = (int) (Math.random() * 24 + 1);

Solution

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