我遇到了Java日曆的一個奇怪問題。日曆小時數設置爲0,但顯示1,爲什麼?
以下代碼從午夜開始的日期連續添加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,這可能是相關的嗎?
夏令時也許? – BigMike
你有沒有考慮過使用jodatime? – fmsf
@BigMike我無法在日曆中找到任何有關日光的方法... – JVerstry