java 计算两个日期之间的差

简介:需求是根据登录时间和当前时间是否超过24小时,来进行业务操作, 因此需要计算时间之差来进行判断。

代码如下:

public static String getDateDiscrepency(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;
    return day + "天" + hour + "小时" + min + "分钟";
}
原文地址:https://www.cnblogs.com/xuchao0506/p/15551774.html