Calendar.HOUR vs Calendar.HOUR_OF_DAY
Beware when u are using calendar to set the time...
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 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:
very helpful, thank you!
By Anonymous, at 8:36 AM
Thanks dear
By Shrikant Kesarkar, at 7:40 PM
Thanks very usefull and simple!
By Bartran, at 10:55 PM
Thank you that's useful :)
By Arun, at 7:09 PM
Cleared-up my problem. Thanks!
By Anonymous, at 2:33 AM
Man, the example you gave is from Java Doc. Pls provide a better and clear example.
By Anonymous, at 3:43 AM
Post a Comment
<< Home