Java – use jodatime to calculate the month between the two dates of the holding year
•
Java
I use this code to calculate the number of months between the input pastdate and CurrentDate It uses jodatime
LocalDate date1 = new LocalDate(installmentStartDate2); LocalDate date2 = new LocalDate(new java.util.Date()); PeriodType monthDay = PeriodType.yearMonthDayTime(); Period difference = new Period(date1,date2,monthDay); int months = difference.getMonths(); return months + 1;
Now, when I enter January 1, 2013, I get 10 as the answer But even if I enter January 1, 2012, the problem is still 10
So this means that it doesn't take the year into account
What can I do to get the right answer, that is, when I enter January 1, 2012
Can jodatime do it? So? how? If not? Is there any other way?
Solution
Your code is almost correct
You can change:
PeriodType monthDay = PeriodType.yearMonthDayTime();
To:
PeriodType monthDay = PeriodType.months();
It also works
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码