Below is the code that I use to transfer the date to 3 hours. It displays the new time as well as the pm value. But when he gets to 12pm or am, he changes the value of am pm am of the new time in the same way as 12 from which he came. Those. if it is set at 12 a.m. and the new value is 9 p.m., it will be released at 9 p.m. Did I miss something? Do I think about it right that 12 a.m. is midnight as such?
calendar.set(Calendar.HOUR, HourValue);
calendar.set(Calendar.MINUTE, MinValue);
calendar.set(Calendar.SECOND, 0);
if(AMPM.equals("AM")){ampmval=0;}
else{ampmval=1;}
Log.e("AMPMVAL Before",Integer.toString(ampmval));
sdf = new SimpleDateFormat("hh");
NewHourValue = sdf.format(calendar.getTime());
Log.e("Before Time",NewHourValue);
calendar.set(Calendar.AM_PM, ampmval);
calendar.add(Calendar.MINUTE, -300);
int AmOrPm = calendar.get(Calendar.AM_PM);
Log.e("AMPMVAL After",Integer.toString(AmOrPm));
sdf = new SimpleDateFormat("hh");
NewHourValue = sdf.format(calendar.getTime());
Log.e("After Time",NewHourValue);
It outputs something like
05-15 23:07:11.233: E/Before Time(457): 09:00:00 PM
05-15 23:07:11.240: E/AMPMVAL After(457): 0
05-15 23:07:11.640: E/After Time(457): 06:00:00 AM
05-15 23:07:23.369: E/AMPMVAL Before(457):0
05-15 23:07:23.742: E/Before Time(457): 10:00:00 PM
05-15 23:07:23.749: E/AMPMVAL After(457): 0
05-15 23:07:24.113: E/After Time(457): 07:00:00 AM
05-15 23:07:28.320: E/AMPMVAL Before(457):0
05-15 23:07:28.712: E/Before Time(457): 11:00:00 PM
05-15 23:07:28.720: E/AMPMVAL After(457): 0
05-15 23:07:29.112: E/After Time(457): 08:00:00 AM
05-15 23:07:34.700: E/AMPMVAL Before(457):1
05-15 23:07:35.300: E/Before Time(457): 12:00:00 AM
05-15 23:07:35.330: E/AMPMVAL After(457): 1
05-15 23:07:35.693: E/After Time(457): 09:00:00 PM
As you can see, it works great when I increase the hours to 12:00 (Midnight), but when it gets to midnight, it says that the new value is also in am
Somk source
share