我在使用 Java 日历时遇到了一个奇怪的问题。
以下代码从午夜开始的日期连续添加 3 小时。
public static void main(String[] args) {
Calendar now = Calendar.getInstance(TimeZone.getTimeZone("GMT+0"));
// Setting to January 29th, 1920 at 00:00:00
// now.setTimeZone(TimeZone.getTimeZone("GMT+0"));
now.set(Calendar.YEAR, 1920);
now.set(Calendar.MONTH, 0);
now.set(Calendar.DAY_OF_MONTH, 29);
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
now.setLenient(false);
int threeHours = 1000 * 60 * 60 * 3;
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
for (int i=0;i<25;i++) {
System.out.println(sdf.format(now.getTime()));
now.add(Calendar.MILLISECOND, threeHours);
}
}
然而,显示的结果是:
1920-01-29 01:00:00 000
1920-01-29 04:00:00 000
1920-01-29 07:00:00 000
1920-01-29 10:00:00 000
1920-01-29 13:00:00 000
1920-01-29 16:00:00 000
1920-01-29 19:00:00 000
1920-01-29 22:00:00 000
1920-01-30 01:00:00 000
1920-01-30 04:00:00 000
1920-01-30 07:00:00 000
1920-01-30 10:00:00 000
1920-01-30 13:00:00 000
1920-01-30 16:00:00 000
1920-01-30 19:00:00 000
1920-01-30 22:00:00 000
1920-01-31 01:00:00 000
1920-01-31 04:00:00 000
1920-01-31 07:00:00 000
1920-01-31 10:00:00 000
1920-01-31 13:00:00 000
1920-01-31 16:00:00 000
1920-01-31 19:00:00 000
1920-01-31 22:00:00 000
1920-02-01 01:00:00 000
为什么第一个小时是 1 而不是 0?我位于 GMT+1,这可能是相关的吗?