计算两个日期的时间间隔
public static String getDatePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
long sec = diff % nd % nh % nm / ns;
// 输出 相差n天n小时n分钟n毫秒
return "相差" + day + "天" + hour + "小时" + min + "分钟" + sec + "秒";
}
补充:在uap平台中,采用UFDouble类型的方法,可以直接得到小数(如:相差1.25天)
uap平台计算时间间隔:
long nh = 1000 * 60 * 60;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date signDate = sdf.parse(signTime);
Date endDate = sdf.parse(endTime);
// 先获得两个时间的毫秒时间差异
long diff = endDate.getTime() - signDate.getTime();
calTime = SafeCompute.div(new UFDouble(diff), new UFDouble(nh));
calTime = calTime.setScale(1, UFDouble.ROUND_HALF_UP); //四舍五入,小数点后1位
} catch (ParseException e) {
// TODO 自动生成的 catch 块
MessageDialog.showErrorDlg(null, "错误", "计算时间有误");
e.printStackTrace();
}