复制直接可用
/**
* 计算两个时间段时间差,精确到秒
* @param startTime 开始时间
* @param endTime 结束时间
*/
public static String computationTime(Date startTime, Date endTime){
try {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = startTime.getTime() - endTime.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟" + sec + "秒";
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
执行结果: