大约在 2 月份,您可以在 Joda-Time 的错误结果中看到一个奇怪的行为,您可以往下看:
DateTime date1 = new DateTime(2013, 4, 28, 0, 0, 0, 0);
DateTime date1PlusNineMonths = new DateTime(2014, 1, 28, 0, 0, 0, 0);
Period period1 = new Period(date1, date1PlusNineMonths);
//Correct result
System.out.print(date1.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date1.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period1.getYears() + "Years/" + period1.getMonths() + "Months/" + period1.getWeeks() + "Weeks/" + period1.getDays() + "Days");
DateTime date2 = new DateTime(2013, 4, 29, 0, 0, 0, 0);
DateTime date2PlusNineMonths = new DateTime(2014, 1, 28, 0, 0, 0, 0);
Period period2 = new Period(date2, date2PlusNineMonths);
//Correct result
System.out.print(date2.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date2.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period2.getYears() + "Years/" + period2.getMonths() + "Months/" + period2.getWeeks() + "Weeks/" + period2.getDays() + "Days");
DateTime date3 = new DateTime(2013, 5, 28, 0, 0, 0, 0);
DateTime date3PlusNineMonths = new DateTime(2014, 2, 28, 0, 0, 0, 0);
Period period3 = new Period(date3, date3PlusNineMonths);
//Correct result
System.out.print(date3.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date3.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period3.getYears() + "Years/" + period3.getMonths() + "Months/" + period3.getWeeks() + "Weeks/" + period3.getDays() + "Days");
DateTime date4 = new DateTime(2013, 5, 29, 0, 0, 0, 0);
DateTime date4PlusNineMonths = new DateTime(2014, 2, 28, 0, 0, 0, 0);
Period period4 = new Period(date4, date4PlusNineMonths);
//Incorrect result
System.out.print(date4.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date4.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period4.getYears() + "Years/" + period4.getMonths() + "Months/" + period4.getWeeks() + "Weeks/" + period4.getDays() + "Days");
DateTime date5 = new DateTime(2013, 5, 30, 0, 0, 0, 0);
DateTime date5PlusNineMonths = new DateTime(2014, 2, 28, 0, 0, 0, 0);
Period period5 = new Period(date5, date5PlusNineMonths);
//Incorrect result
System.out.print(date5.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date5.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period5.getYears() + "Years/" + period5.getMonths() + "Months/" + period5.getWeeks() + "Weeks/" + period5.getDays() + "Days");
DateTime date6 = new DateTime(2013, 5, 31, 0, 0, 0, 0);
DateTime date6PlusNineMonths = new DateTime(2014, 2, 28, 0, 0, 0, 0);
Period period6 = new Period(date6, date6PlusNineMonths);
//Incorrect result
System.out.print(date6.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date6.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period6.getYears() + "Years/" + period6.getMonths() + "Months/" + period6.getWeeks() + "Weeks/" + period6.getDays() + "Days");
DateTime date7 = new DateTime(2013, 6, 1, 0, 0, 0, 0);
DateTime date7PlusNineMonths = new DateTime(2014, 3, 1, 0, 0, 0, 0);
//Correct result
Period period7 = new Period(date7, date7PlusNineMonths);
System.out.print(date7.toString("dd/MM/YYYY") + ".plusMonths(9) = " + date7.plusMonths(9).toString("dd/MM/YYYY"));
System.out.print(period7.getYears() + "Years/" + period7.getMonths() + "Months/" + period7.getWeeks() + "Weeks/" + period7.getDays() + "Days");
通常在测试中(周期4、周期5、周期6)我应该有天变化而不是每次都显示9个月和0天,这是否正常,也许我在使用joda框架时出错了,当我做一个时输出变得正确.addMonth(9)
上01/06/2013
。
输出:
28/04/2013.plusMonths(9) = 28/01/2014 //correct result
0Years/9Months/0Weeks/0Days //correct calculation
29/04/2013.plusMonths(9) = 29/01/2014 //correct result
0Years/8Months/4Weeks/2Days //correct calculation
28/05/2013.plusMonths(9) = 28/02/2014 //correct result
0Years/9Months/0Weeks/0Days //correct calculation
29/05/2013.plusMonths(9) = 28/02/2014 //incorrect result should be 29/02/2014
0Years/9Months/0Weeks/0Days //correct calculation
30/05/2013.plusMonths(9) = 28/02/2014 //incorrect result should be 01/03/2014
0Years/9Months/0Weeks/0Days //incorrect calculation, should be 0Years/8Months/4Weeks/2Days
31/05/2013.plusMonths(9) = 28/02/2014 //incorrect result should be 01/03/2014
0Years/9Months/0Weeks/0Days //incorrect calculation, should be 0Years/8Months/4Weeks/1Days
01/06/2013.plusMonths(9) = 01/03/2014 //correct result
0Years/9Months/0Weeks/0Days //correct calculation