public static String dateDiff(String startTime, String endTime) throws Exception { SimpleDateFormat sd = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS); long nd = 1000 * 24 * 60 * 60; //一天的毫秒数 long nh = 1000 * 60 * 60;//一小时的毫秒数据 long nm = 1000 * 60; //一分钟毫秒数 long ns = 1000; long diff; //获得两个时间的毫秒时间差异 diff = sd.parse(endTime).getTime() - sd.parse(startTime).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 + "分" + sec + "秒"; }
JAVA计算两个日期相差天、时、分、秒方法
最新推荐文章于 2024-07-18 17:24:45 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)