如何使用 Java 的 Joda API 计算仅在几个月内的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7460701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:04:04  来源:igfitidea点击:

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

javadatejodatime

提问by Abhay Bhargav

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:

我正在编写一个程序,它应该只计算两个给定日期之间的月份并将值返回给程序。例如,如果我必须计算 4 月 1 日到 6 月 30 日之间的月数(即一个季度,3 个月),我使用以下代码:

    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?

使用这个,我仍然得到“2”作为月数,而实际上是“3”个月。这是我想要的一个例子。我只计算月数(即从开始月份的第一天到结束月份的最后一天),我不需要额外的分析,如天数、周数、小时数等。我该如何实现?

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by Brian Roach

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-15and 2011-06-14would still result in the answer being 2

您需要该withDaysRemoved()部件来确保将月数加一有效。否则两个日期,例如2011-04-152011-06-14仍然会导致答案是2

回答by Roland Illig

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 monthsyou are getting.

你为什么期望答案是3 months?准确的答案是两个月和一点点,这就是你得到的两个月

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.

如果你想要这个区间“触及”的月数,这是一个完全不同的问题。只需将差异的结果加 1。

回答by buritos

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

回答by user2818074

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

回答by lukastymo

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

Joda 算法正确计算了这两个日期之间的差异。此伪代码将是解释其工作原理的最简单方法:

// (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:

要做你想做的事,你需要“改进”joda 算法:

  • 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.
  • 计算两个日期之间的距离(使用正常的月份之间)
  • 如果您有特定情况:一个日期中的最后一天和第二个日期中的第 1 天,则将 +1 添加到最终结果中。

回答by Jiri Kriz

Let y1and m1be the year and month of the start date, and y2and m2be 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

让我们y1m1于开始日期的年份和月份,y2m2于结束日期的年份和月份。那么开始和结束之间的月数,包括开始和结束日期的月份是

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