我希望找到两个日期之间以月和天为单位的日期差。一个是当前系统日期,另一个是未来日期。
未来日期的格式如下:
2014-02-06 21:26
我已经使用以下代码来计算日期差:
1.
currentCal = Calendar.getInstance();
weddingCal = Calendar.getInstance();
weddingCal.clear();
DateFormat formatterResponseDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
formatterResponseDate.setTimeZone(TimeZone.getTimeZone("GMT"));
weddingCal.setTime(formatterResponseDate.parse(responseThis.trim()));
long dateDiff = weddingCal.getTimeInMillis() -
currentCal.getTimeInMillis();
long months = dateDiff / (30 * 24 * 60 * 60 * 1000);
long days = (dateDiff % (30 * 24 * 60 * 60 * 1000)) / (24 * 60 * 60 * 1000);
但是它给了我错误的结果,而且我认为它并不方便,因为有时一个月有31天。我还使用了JODA - Time来查找差异,但它也没有帮助我。
2.
currentCal = Calendar.getInstance();
startDate = new DateTime(currentCal.get(Calendar.YEAR),
(currentCal.get(Calendar.MONTH)+1),
currentCal.get(Calendar.DAY_OF_MONTH), currentCal.get(Calendar.HOUR),
currentCal.get(Calendar.MINUTE));
weddingCal = Calendar.getInstance();
weddingCal.clear();
weddingCal.setTime(formatterResponseDate.parse(responseThis.trim()));
endDate = new DateTime(weddingCal.get(Calendar.YEAR), (weddingCal.get(Calendar.MONTH)+1),
weddingCal.get(Calendar.DAY_OF_MONTH), weddingCal.get(Calendar.HOUR),
weddingCal.get(Calendar.MINUTE));
Period period = new Period(startDate, endDate);
tvMonth.setText(String.valueOf(period.getMonths()));
tvDays.setText(String.valueOf(period.getDays()));
两者都不起作用,请帮助我,我该如何找到它?谢谢。