Java 使用 JodaTime 计算两个日期之间的月份,记住年份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19677739/
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-08-12 19:19:39  来源:igfitidea点击:

Calculate months between two dates keeping year in mind using JodaTime

javajodatime

提问by Abhishek Singh

I am using this code to calculate no of months between input pastdate and currentdate. It uses JodaTime

我正在使用此代码来计算输入过去日期和当前日期之间的月数。它使用 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 1 jan 2013 i get 10 as answer. But the problem is get 10 even when i enter 1 jan 2012.

现在,当我输入 2013 年 1 月 1 日时,我得到 10 作为答案。但问题是即使我进入 2012 年 1 月 1 日也得到 10。

So that means while calculation it doesn't considers year.

所以这意味着在计算时不考虑年份。

What can i do to get correct answer i.e., 22 when i enter 1 jan 2012.

当我进入 2012 年 1 月 1 日时,我该怎么做才能得到正确的答案,即 22。

Can JodaTime do that ? If yes ? How ? If not ? Any other approach ?

JodaTime 能做到吗?如果是 ?如何 ?如果不 ?还有其他方法吗?

采纳答案by chillworld

Your code is almost correct.

你的代码几乎是正确的。

You can change :

你可以改变 :

PeriodType monthDay = PeriodType.yearMonthDayTime();

to :

到 :

PeriodType monthDay = PeriodType.months();

and it will work also.

它也会起作用。

回答by Jon Skeet

You're asking for the difference in years, months and days - so you're getting back 1 year, 10 months and 29 days.

您要求的是年、月和日的差异 - 所以您要返回 1 年、10 个月和 29 天。

Just use:

只需使用:

int months = Months.monthsBetween(date1, date2).getMonths();

Or if you really want to use new Periodand perhaps get days as well you can use:

或者,如果您真的想使用new Period并且可能还需要几天,您可以使用:

PeriodType monthDay = PeriodType.yearMonthDayTime().withYearsRemoved();
Period difference = new Period(date1, date2, monthDay);

You could just use PeriodType.months(), but if you're genuinely only interested in the months, I'd use the first snippet above to do it all in a single short-ish statement.

您可以只使用PeriodType.months(),但如果您真的只对月份感兴趣,我会使用上面的第一个片段在一个简短的语句中完成所有操作。

回答by Gabber

Joda time is good way to dealing with this problem

Joda 时间是处理这个问题的好方法

Create a joda time date object

创建一个 joda 时间日期对象

DateTime startDate = DateTime.parse("1970-01-01", DateTimeFormat.forPattern("yyyy-MM-dd"))
DateTime endDate = DateTime.parse("2015-02-25", DateTimeFormat.forPattern("yyyy-MM-dd"))

Difference between two dates in days

两个日期之间的天数差异

int days = Days.daysBetween(startDate, endDate).getDays()

Difference between two dates in months

两个日期之间的月差

int months = Months.monthsBetween(startDate.withDayOfMonth(1), endDate.withDayOfMonth(1)).getMonths()

Difference between two dates in years

两个日期之间的年差

int years = Years.yearsBetween(startDate, endDate).getYears();

回答by android developer

I suggest using this:

我建议使用这个:

ChronoUnit.MONTHS.between(earlierDate,date)