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