JAVA计算两个Date时间差(精确到秒)
拷贝即可使用
private static final long nd = 1000 * 24 * 60 * 60;
private static final long nh = 1000 * 60 * 60;
private static final long nm = 1000 * 60;
/** * 计算两个时间段时间差,精确到秒 * @param startTime 2019-04-10 17:16:11 * @param endTime 2019-04-10 17:28:17 * @return */
public static String computationTime(Date startTime, Date endTime){
try {
long diff = endTime.getTime() - startTime.getTime();
long day = diff / nd;
long hour = diff % nd / nh;
long min = diff % nd % nh / nm;
long sec = diff % nd % nh % nm / 1000;
return day + " day " + hour + "hour" + min + " min " + sec + "s";
}catch (Exception e) {
e.printStackTrace();
return null;
}
}