Java 中,如何計算兩個日期之間的差距?

ITWUYI發表於2020-12-21
public static String getDateDiff(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 + "分鐘";
    }

相關文章