3

我想找到两个日期之间的月份和日期的日期差异。一个是我的系统当前日期,另一个是将来的日期。

并且未来日期采用以下格式:

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 to find the difference,但它也对我没有帮助。

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()));

两者都不起作用请帮助我,我该如何找到它?谢谢

4

1 回答 1

4

我已经使用了下面的代码,它工作正常。

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, PeriodType.yearMonthDayTime());
tvMonth.setText(String.valueOf(period.getMonths()));
tvDays.setText(String.valueOf(period.getDays()));
于 2013-09-07T06:07:48.607 回答