Difference between Calendar.HOUR and Calendar.HOUR_OF_DAY?

JavaAndroidCalendar

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

output Date visualization

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVishwesh JainkuniyaView Question on Stackoverflow
Solution 1 - JavaMrugesh ThakerView Answer on Stackoverflow
Solution 2 - Javaabid aliView Answer on Stackoverflow