Java計算2個時間單位計算相差多少天
阿新 • • 發佈:2021-02-04
Java關於時間操作
計算2個時間單位計算相差多少天?
計算的是2021-02-03到2021-02-04相差多少天?
下面展示程式碼。
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Long days = null;
try {
//現在系統當前時間(2021-02-03)
Date currentTime = dateFormat.parse(dateFormat.format(new Date()));
//預期完成時間
Date finishTime = dateFormat.parse("2021-02-04");
long diff = finishTime.getTime() - currentTime.getTime();
days = diff / (1000 * 60 * 60 * 24);
} catch (ParseException e) {
e. printStackTrace();
}
System.out.println(days + "天");
}
輸出結果:
1天
Process finished with exit code 0