Ask Gregorian Calendar for hour of day in java

Leanne C picture Leanne C · Apr 24, 2010 · Viewed 10.6k times · Source

I'm using a Gregorian Calendar to set a specific date and time to an application using the set function of the Gregorian Calendar. When i use the getTime() method, it gives me the right output however when i try to access the Hour_Of_Day and Minute it gives a wrong number.

    Calendar time = new GregorianCalendar();
    time.set(2010, Calendar.JANUARY, 1, 7, 20,0);       
    hour = time.HOUR_OF_DAY;
    minute = time.MINUTE; 

The hour gives an output of 11 and the minute gives an a value of 12.
Any suggestions on how to fix this? Thanks

Answer

Brett Kail picture Brett Kail · Apr 24, 2010

Your code is just assigning hour/minute to constants. You need to call Calendar.get(int):

hour = time.get(Calendar.HOUR_OF_DAY);
minute = time.get(Calendar.MINUTE);