我试图找到两个日期之间的月数。任何帮助表示赞赏。我在下面的示例中使用Joda-Time。
DateMidnight start = new DateMidnight(new Date());
DateMidnight dtEndDate = start.plusDays(11); //adding Days
int months = Months.monthsBetween(start, dtEndDate).getMonths();
System.out.println("Months between " +
start.toString("yyyy-MM-dd") + " and " +
dtEndDate.toString("yyyy-MM-dd") + " = " +
months + " month(s)");
以上代码在 2013 年 2 月 18 日至 2013 年 3 月 1 日返回“0”
我无法将 +1 添加到月份,因为我需要找到同一个月的差异,我还需要找到两个经过期间之间的差异,即“2012 年 12 月 4 日”和“2013 年 1 月 6 日”之间应该返回 -1 ;
scenario1:
date1: 2013-02-18
date1.plusDays(11);
date2: 2013-03-01
Output : 0 month(s) //but I need as 1 Month
scenario2:
date1: 2013-02-18
date1.plusDays(1);
date2: 2013-02-19
Output : 0 month(s) //returns correctly exactly what I need
scenario3:
date1: 2013-03-18
date2: 2013-02-19
Output should be : -1 month(s)