public String getGapTime(Date date1,Date date2){ // 相差的毫秒值 Long milliseconds = date1.getTime() - date2.getTime(); long nd = 1000 * 24 * 60 * 60;// 一天的毫秒數 long nh = 1000 * 60 * 60;// 一小時的毫秒數 long nm = 1000 * 60;// 一分鍾的毫秒數 long ns = 1000;// 一秒鍾的毫秒數 long day = milliseconds / nd; // 計算相差多少天 long hour = milliseconds % nd / nh; // 計算相差剩余多少小時 long min = milliseconds % nd % nh / nm; // 計算相差剩余多少分鍾 long sec = milliseconds % nd % nh % nm / ns; // 計算相差剩余多少秒 System.out.println("時間相差:" + day + "天" + hour + "小時" + min + "分鍾" + sec + "秒"); long hourAll = milliseconds / nh; // 計算相差多少小時 System.out.println("時間相差:" + hourAll + "小時" + min + "分鍾" + sec + "秒"); long min2 = milliseconds / nm; // 計算相差多少分鍾 System.out.println("時間相差:" + min2 + "分鍾" + sec + "秒"); String overTime = hourAll+"小時"+min+"分"; return overTime; }
參考鏈接:https://blog.csdn.net/github_39325328/article/details/115012534
