如何使用 Java 的 Joda API 仅计算月份差异

发布于 12-05 04:52 字数 450 浏览 1 评论 0原文

我正在编写一个程序,该程序应该只计算两个给定日期之间的月份并将值返回给程序。例如,如果我必须计算 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();

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

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

水晶透心2024-12-12 04:52:18
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;

您需要 withDaysRemoved() 部分来确保月数加一有效。否则,2011-04-152011-06-14 等两个日期仍会导致答案为 2

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

独守阴晴ぅ圆缺2024-12-12 04:52:18

为什么您预计答案是 3 个月?准确的答案是两个月一点点,这就是您得到的两个月

如果您想要此间隔“触及”的月份数,这是一个完全不同的问题。 只需将差值加 1。

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.

不回头走下去2024-12-12 04:52:18
Months mt = Months.monthsBetween(
    start.monthOfYear().roundFloorCopy(), 
    end.monthOfYear().roundCeilingCopy()
);
Months mt = Months.monthsBetween(
    start.monthOfYear().roundFloorCopy(), 
    end.monthOfYear().roundCeilingCopy()
);
风追烟花雨2024-12-12 04:52:18

y1m1 为开始日期的年份和月份,
y2m2 是结束日期的年份和月份。
然后是之间的月数
开始和结束,包括开始和结束日期的月份

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

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
暖阳2024-12-12 04:52:18

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

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

  • 计算两个日期之间的距离(使用正常的monthsBetween)
  • 如果你有特定情况:一个月的最后一天在一个日期和第二个日期的该月第一天,将最终结果加 +1。

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.
苦笑流年记忆2024-12-12 04:52:18
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文