Difference between 2 dates in months and days in Java 7

对着背影说爱祢 提交于 2019-12-13 07:11:19

问题


I want to calculate the difference between 2 dates in months and days, I don't want to consider a month is 30 days and get the time in milliseconds and convert them, and I don't want to use a library such as Joda, and I also found a solution that uses LocalDate, however I am using Java 7. I looked for answers to my question but could not find any, and I tried many approaches, I tried creating a calendar and passed the time in milliseconds the difference between my 2 dates, but if I tried for example getting the difference between 15/04 and 15/5 I would get 30 days and not 1 month, below is the code

Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2013);
c.set(Calendar.MONTH, Calendar.APRIL);
c.set(Calendar.DAY_OF_MONTH, 15);

Date startDate = c.getTime();

c.set(Calendar.YEAR, 2013);
c.set(Calendar.MONTH, Calendar.MAY);
c.set(Calendar.DAY_OF_MONTH, 15);

Date lastDate = c.getTime();

Calendar diffCal = Calendar.getInstance();
diffCal.setTimeInMillis(lastDate.getTime() - startDate.getTime());

int months = ((diffCal.get(Calendar.YEAR) - 1970) * 12) + diffCal.get(Calendar.MONTH);
int days   = diffCal.get(Calendar.DAY_OF_MONTH) - 1;

System.out.println("month(s) = " + months); // month(s) = 0
System.out.println("day(s)   = " + days);   // day(s)   = 30

What can I use to get the difference in months first then in days between 2 dates?

EDIT: I wrote the below code and seems to give me what I want, could this be used

Date startDate = sdf.parse("31/05/2016");
Date endDate   = sdf.parse("1/06/2016");

Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar   = Calendar.getInstance();

startCalendar.setTime(startDate);
endCalendar.setTime(endDate);

int months = 0, days = 0;

months += ((endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR)) * 12);

int startDays = startCalendar.get(Calendar.DAY_OF_MONTH);
int endDays   = endCalendar.get(Calendar.DAY_OF_MONTH);

if(endDays >= startDays) {
    months += (endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH));
    days   += (endDays - startDays);
} else {
    months += (endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH) - 1);
    days   += ((startCalendar.getActualMaximum(Calendar.DAY_OF_MONTH) - startDays) + endDays);
}

System.out.println("Difference");
System.out.println("Month(s): " + months);
System.out.println("Day(s):   " + days);

回答1:


You answered your own question in your research. JDK7 does not support what you want. You have three options:

  1. Write your own (do not do this because you will make many mistakes that have been made before)
  2. Use a library like Joda-Time which has been recommended many times
  3. Upgrade to JDK8 and use java.time.Period


来源:https://stackoverflow.com/questions/40613603/difference-between-2-dates-in-months-and-days-in-java-7

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