What is the difference between Calendar.HOUR
and Calendar.HOUR_OF_DAY
?
When to use Calendar.HOUR
and Calendar.HOUR_OF_DAY
?
I am confused sometime Calendar.HOUR
this works fine and othertime Calendar.HOUR_OF_DAY
this works fine. What they return in the form of int?
I have read this documentation but not understood the difference.
Any suggestions
Thanks.
Asked
Active
Viewed 5.5k times
68

Vishwesh Jainkuniya
- 2,821
- 3
- 18
- 35
-
6You have a perfect definition in the docs. See [HOUR](https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#HOUR) and [HOUR_OF_DAY](https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#HOUR_OF_DAY). In short, HOUR uses a 12-hour clock (am, pm) and HOUR_OF_DAY a 24-hour clock. – dabadaba May 15 '16 at 09:10
-
https://github.com/dlew/joda-time-android Joda time if you want to save your time! – Kingfisher Phuoc May 15 '16 at 09:15
2 Answers
121
From http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#HOUR:
Calendar.HOUR = Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock. E.g., at 10:04:15.250 PM the HOUR is 10.
Calendar.HOUR_OF_DAY = 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.
-
14Side note that the range for HOUR_OF_DAY is from 0 to 23 (inclusive). – Advait Saravade Mar 10 '19 at 21:56
1
This code will help you understand better
import java.util.Calendar; import java.util.GregorianCalendar;
public class test{ public static void main(String[] args) {
GregorianCalendar gc = new GregorianCalendar(2013, 8, 15, 21, 69,55);
//minutes = 69 is equal to 1 hour and 09 minutes. This hour will add to Hour place (21+1 = 22)//Sun Sep 15 22:09:55 IST 2013
p(gc, Calendar.YEAR); //gives year
p(gc, Calendar.MONTH); // gives month staring at 0 for January
p(gc, Calendar.DATE); // date
p(gc, Calendar.DAY_OF_WEEK);// Sunday=1, Monday=2, .....Saturday -7
p(gc, Calendar.WEEK_OF_MONTH);//what week its running in week ,whether its first or second;
p(gc, Calendar.DAY_OF_WEEK_IN_MONTH);//In this case, How may times does Sunday is repeating in the month = 3;
p(gc, Calendar.DAY_OF_YEAR);//count of the day in the year
p(gc, Calendar.HOUR);//12 hour format. if the time is 22:09:55, answer would be (22-12)=10
p(gc, Calendar.HOUR_OF_DAY);// hour of day that is 22 (24h format)
p(gc, Calendar.MINUTE);// 09
p(gc, Calendar.SECOND);// 55
System.out.println();
System.out.println(gc.getTime());
}
static void p(Calendar c, int type) {
System.out.print(c.get(type) + "-");
} }
*output :
2013-8-15-1-3-3-258-10-22-9-55-
Sun Sep 15 22:09:55 IST 2013
*

abid ali
- 71
- 4