Android 中的日历日期差异计算错误

Calender Date Difference Calculation error in Android

我知道有很多格式的示例可以计算两个日期的差异。我尝试了一种方式。如果我输入一个月内的日期,我的答案总是正确的。如果我去寻找差异超过一个月,它就没有给出正确的答案。它总是发现两个日期的差异。如何找到超过 31 天或 2016-01-05 到 2015-12-13 的日期差异。

Date oldDate,newDate;
SimpleDateFormat dateFormat;

//editText1=2015-12-13
//editText2=2016-01-05

oldDate = dateFormat.parse(editText1.getText().toString());
newDate = dateFormat.parse(editText2.getText().toString());
oldDate=dateFormat.parse(dateFormat.format(oldDate));
newDate=dateFormat.parse(dateFormat.format(newDate));
long diff = newDate1.getDate() - oldDate1.getDate();
editText3.setText(""+(diff+1));

将日期转换为以毫秒为单位的时间戳,一个减去另一个,然后将结果除以 86400000(一天中的毫秒数)。

long oldTime, newTime;
oldTime = dateFormat.parse(editText1.getText().toString()).getTime();
newTime = dateFormat.parse(editText2.getText().toString()).getTime();
long daysDiff = Math.abs(newTime - oldTime) / DateUtils.DAY_IN_MILLIS;