我有四個日期,我想計算日期之間的日差。我的程序如下。計算差異時的輸出錯誤
Date d1 = new Date("11/28/14 23:59:58");
Date d2 = new Date("11/29/14 00:00:02");
Date d3 = new Date("11/29/14 23:59:58");
Date d4 = new Date("11/30/14 00:00:02");
final long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
int diff1 = (int) ((d2.getTime()- d1.getTime())/DAY_IN_MILLIS);
int diff2 = (int) ((d3.getTime()- d1.getTime())/DAY_IN_MILLIS);
int diff3 = (int) ((d4.getTime()- d1.getTime())/DAY_IN_MILLIS);
System.out.println(diff1);
System.out.println(diff2);
System.out.println(diff3);
欲DIFF1 = 1,DIFF2 = 1 &的diff3 = 2。我的輸出爲diff1 = 0,diff2 = 1,diff2 = 1。
'日期(字符串)'已過時,只是順便說一句。 – ceekay
比較這樣的日期也是一個非常糟糕的主意。 – Kayaman
可能重複的[Java,計算兩個日期之間的天數](http://stackoverflow.com/questions/7103064/java-calculate-the-number-of-days-between-two-dates) –