使用Java获取两个月之间的差异[重复]

时间:2022-01-19 16:54:33

This question already has an answer here:

这个问题在这里已有答案:

I need to get difference between two dates using Java. I need my result to be in months.

我需要使用Java来区分两个日期。我需要几个月的结果。

Example:

Startdate = 2013-04-03 enddate = 2013-05-03 Result should be 1

Startdate = 2013-04-03 enddate = 2013-05-03结果应为1

if the interval is

如果间隔是

Startdate = 2013-04-03 enddate = 2014-04-03 Result should be 12

Startdate = 2013-04-03 enddate = 2014-04-03结果应为12

Using the following code I can get the results in days. How can I get in months?

使用以下代码,我可以在几天内得到结果。我怎么能在几个月内进入?

Date startDate = new Date(2013,2,2);
Date endDate = new Date(2013,3,2);
int difInDays = (int) ((endDate.getTime() - startDate.getTime())/(1000*60*60*24));

3 个解决方案

#1


96  

If you can't use JodaTime, you can do the following:

如果您不能使用JodaTime,您可以执行以下操作:

Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startDate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);

int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);

Note that if your dates are 2013-01-31 and 2013-02-01, you get a distance of 1 month this way, which may or may not be what you want.

请注意,如果您的日期是2013-01-31和2013-02-01,那么您可以通过这种方式获得1个月的距离,这可能是您想要的,也可能不是。

#2


4  

You can use Joda time library for Java. It would be much easier to calculate time-diff between dates with it.

您可以将Joda时间库用于Java。使用它计算日期之间的时差可能会容易得多。

Sample snippet for time-diff:

time-diff的示例代码段:

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

#3


-5  

You can try this:

你可以试试这个:

Calendar sDate = Calendar.getInstance();
Calendar eDate = Calendar.getInstance();
sDate.setTime(startDate.getTime());
eDate.setTime(endDate.getTime());
int difInMonths = sDate.get(Calendar.MONTH) - eDate.get(Calendar.MONTH);

I think this should work. I used something similar for my project and it worked for what I needed (year diff). You get a Calendar from a Date and just get the month's diff.

我认为这应该有效。我为我的项目使用了类似的东西,它适用于我需要的东西(年差异)。你从日期得到一个日历,然后得到月份的差异。

#1


96  

If you can't use JodaTime, you can do the following:

如果您不能使用JodaTime,您可以执行以下操作:

Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startDate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);

int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);

Note that if your dates are 2013-01-31 and 2013-02-01, you get a distance of 1 month this way, which may or may not be what you want.

请注意,如果您的日期是2013-01-31和2013-02-01,那么您可以通过这种方式获得1个月的距离,这可能是您想要的,也可能不是。

#2


4  

You can use Joda time library for Java. It would be much easier to calculate time-diff between dates with it.

您可以将Joda时间库用于Java。使用它计算日期之间的时差可能会容易得多。

Sample snippet for time-diff:

time-diff的示例代码段:

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

#3


-5  

You can try this:

你可以试试这个:

Calendar sDate = Calendar.getInstance();
Calendar eDate = Calendar.getInstance();
sDate.setTime(startDate.getTime());
eDate.setTime(endDate.getTime());
int difInMonths = sDate.get(Calendar.MONTH) - eDate.get(Calendar.MONTH);

I think this should work. I used something similar for my project and it worked for what I needed (year diff). You get a Calendar from a Date and just get the month's diff.

我认为这应该有效。我为我的项目使用了类似的东西,它适用于我需要的东西(年差异)。你从日期得到一个日历,然后得到月份的差异。