在Java中减去两个日期 [英] Subtract two dates in Java

查看:114
本文介绍了在Java中减去两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

计算两个Java日期实例之间的差异




$ b $我知道这可能是一个重复的线程。但是我试图找出一种计算两个日期之间的差异的方法。从jquery,日期字符串格式为'yyyy-mm-dd'。我读这个字符串并转换为java日期这样

I know this might be a duplicate thread. But I am trying to figure out a way to compute the difference between two dates. From jquery the date string is in the format 'yyyy-mm-dd'. I read this as a String and converted to java Date like this

Date d1 = new SimpleDateFormat("yyyy-M-dd").parse((String) request.getParameter(date1));
Date d2 = new SimpleDateFormat("yyyy-M-dd").parse((String) request.getParameter(date2));

我想计算这两个日期之间的差距。

I want to compute the difference in the number of days between these two dates.

注意:我不能使用第三方API作为需要审核的。

Note: I cannot use third party API's as those need to reviewed.

推荐答案

long diff = Math.abs(d1.getTime() - d2.getTime());
long diffDays = diff / (24 * 60 * 60 * 1000);

这篇关于在Java中减去两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆