我試圖在兩個日期之間找到沒有月份。任何幫助讚賞。以下示例使用Joda-Time。如何在添加天數後得到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)");
上面的代碼返回「0」 2013 - 2月18至2013-MAR-01
我不能添加+1個月我需要找到在同一個月差,也我需要找出兩個過去的時間段之間的差異,即在'2012年12月至04年'和'2013年1月至06年'之間應該返回-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)
到底什麼是「月」,由你定義是什麼?是否「這段時間跨越了多少個月的邊界」? 2013-02-28(2月底)至2013-03-01(3月初)算作1個月? – 2014-01-24 04:33:46