How to calculate difference ONLY in months using Java's Joda API

自作多情 提交于 2019-12-18 04:46:05

问题


I am writing a program that is supposed to just calculate the months between 2 given dates and return the value to the program. For instance, if I have to calculate the number of months between 1 April and 30 June (which is a quarter, 3 months), and I use the following code:

    DateTime start = new DateTime().withDate(2011, 4, 1);
    DateTime end = new DateTime().withDate(2011, 6, 30);

    Months mt = Months.monthsBetween(start, end);
    int monthDiff = mt.getMonths();

Using this, I am still getting "2" as the number of months, whereas it is actually "3" months. This is an example of what I want. I am only calculating the number of months (i.e. from 1st of the start month t the last date of the end month) and I dont need additional analysis like days, weeks, hours, etc. How do I achieve this?

Any help would be appreciated.


回答1:


DateTime start = new DateTime().withDate(2011, 4, 1);
DateTime end = new DateTime().withDate(2011, 6, 30);
Period p = new Period(start, end, PeriodType.months().withDaysRemoved());
int months = p.getMonths() + 1;

You need the withDaysRemoved() part to ensure that adding one to the number of months works. Otherwise two dates such as 2011-04-15 and 2011-06-14 would still result in the answer being 2




回答2:


Why do you expect the answer to be 3 months? The precise answer is two months and a little bit, which then results in the two months you are getting.

If you want the number of months that are “touched” by this interval, this is a completely different question. Just add 1 to the result of the difference.




回答3:


Months mt = Months.monthsBetween(
    start.monthOfYear().roundFloorCopy(), 
    end.monthOfYear().roundCeilingCopy()
);



回答4:


Let y1 and m1 be the year and month of the start date, and y2 and m2 be the year and month of the end date. Then the number of months between start and end, including the months of the start and end dates is

(y2 - y1) * 12 + (m2 - m1) + 1



回答5:


Joda algorithm counted correctly difference between this two dates. This pseudo-code will be the easiest way to explain how it works:

// (1)
monthsBetween(2011.6.14, 2011.4.15) = 1
monthsBetween(2011.6.15, 2011.4.15) = 2
// (2)
monthsBetween(2011.6.30, 2011.4.1) = 2
monthsBetween(2011.7.1,  2011.4.1) = 3

To do what you want you need to "improve" joda algorithm:

  • Count distance between two dates (use normal monthsBetween)
  • If you have specific situation: the last day of month in one date and the 1st day of month in second date, add +1 to the final result.



回答6:


DateTime start = new DateTime().withDate(2011, 4, 1);
        DateTime end = new DateTime().withDate(2011, 2, 1);
        Period p = new Period(start, end, PeriodType.months().withDaysRemoved());
        int months = p.getMonths();
        System.out.println(months);


wrong output in this case


来源:https://stackoverflow.com/questions/7460701/how-to-calculate-difference-only-in-months-using-javas-joda-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!