My Personal Online Recycle Bin
You may find something useful here...........

Sunday, May 21, 2006

Calendar.HOUR vs Calendar.HOUR_OF_DAY

Beware when u are using calendar to set the time...

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
System.out.println("calendar is set to " + calendar.getTime);

You may expect it to always print "calendar is set to yyyy/mm/dd 00:00:00"

But this is not the case.

it will print "calendar is set to yyyy/mm/dd 00:00:00" if the execution time is AM
else it will print "calendar is set to yyyy/mm/dd 12:00:00" if the execution time is PM.

used Calendar.HOUR_OF_DAY to make the output always print "calendar is set to yyyy/mm/dd 00:00:00".

Below is the description from java sun api for Calendar.HOUR and Calendar.HOUR_OF_DAY

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.

6 Comments:

Post a Comment

<< Home