java 算倆個日期之前相差多少天,多少分鐘,多少毫秒
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date endTime = sdf.parse("2018-11-08 10:50:49"); Date startTime = sdf.parse("2018-11-07 10:50:50"); long start = startTime.getTime(); long end = endTime.getTime(); long between = end - start; // 相差天數 long day = between/(24*60*60*1000); // 相差小時數 一共相差的毫秒數 - 天所佔的毫秒數 long hour = (between- day*(24*60*60*1000))/(60*60*1000); // 相差分鐘數 一共相差的毫秒數 - 天所佔的毫秒數-小時所佔的毫秒數 long minute = (between- day*(24*60*60*1000) - hour*(60*60*1000) )/(60*1000); // 相差秒數 long second = (between - day*(24*60*60*1000) - hour*(60*60*1000)- minute*(60*1000)) /1000; System.err.println("相差:"+day+"天"+hour+"小時"+minute+"分鐘"+second+"秒");