下面是我用来将日期向后移动 3 小时的代码。它显示新时间和下午值。但是当它到达 12pm 或 am 时,它会将新时间的 am pm 值更改为与它来自的 12 点相同。即,如果它设置在上午 12 点并且新值是晚上 9 点,它会输出晚上 9 点。我错过了一些简单的东西吗?我是否正确地认为上午 12 点是午夜这样的?
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);
这输出类似
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
正如您所看到的,它工作正常,因为我在上午 12 点(午夜)之前的几个小时内递增,但是当它到达午夜时,它会说新值也在 am