I'm creating a Calendar
instance (current time) then setting hour minute and am/pm
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR,12);
now.set(Calendar.MINUTE,0);
now.set(Calendar.AM_PM,1);
Then if I try to grab the am/pm from the now Calendar instance it incorrectly always set to am and 1 day ahead of now. This only seems to happen with hour 12 and no other hour. What is the issue here? Does the order I set them matter or in the case of hour 12, should I use the 24 hour format to set the 'now' instance? Yes I should have mentioned, this is on Android.
It's because each half of the day goes from hour zero to the end of hour 11. Mathematically speaking, there is no 12th hour (even though we use it all the time in real life). So what you mean to set is hour zero in the PM period, so:
now.set(Calendar.HOUR, 0);
now.set(Calendar.MINUTE,0);
now.set(Calendar.AM_PM, Calendar.PM);
By setting the hour to 12, you tell the calendar (right or wrong, you'll have to ask the Oracle engineers) to set it to the 12th hour of the PM period, which winds up being the zero hour of the AM period of the next day.
There are only 12 hours in a half-day period and that count is zero based, so mathematically speaking, there is no 12th hour. It's zero. It is a little counter intuitive because we are used to saying it's 12:30, not 0:30.
Much better answer for me was by AndroidDev
How to set time to 24 hour format in Calendar S/he says use Calendar.HOUR_OF_DAY so it should be now.set(Calendar.HOUR_OF_DAY, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With