java计算两个日期相差多少天小时分钟等



public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long time = sdf.parse("2022-03-21 23:59:59", new ParsePosition(0)).getTime();
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
long ns = 1000;
// // 获得两个时间的毫秒时间差异
long diff = time - System.currentTimeMillis() + 1000;
// // 计算差多少天
long day = diff / nd;
// // 计算差多少小时
long hour = diff % nd / nh;
// // 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
long sec = diff % nd % nh % nm / ns;
System.out.println(day + "天" + hour + "小时" + min + "分钟" + sec + "秒");
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM