Days between two dates compare & # 8594; easier?

Hi, I ask myself if there is an easier way to get the number of days between two dates.

I want only days without looking at hours or minutes.

Therefore, if today is Monday, and the date I want to compare is on Wednesday, the days between them are 2 (time does not matter)

Therefore, I use this code:

Calendar c = Calendar.getInstance(); // Only the day: c.set(Calendar.HOUR, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); Calendar to = Calendar.getInstance(); to.setTime(date); to.set(Calendar.HOUR, 0); to.set(Calendar.MINUTE, 0); to.set(Calendar.SECOND, 0); to.set(Calendar.MILLISECOND, 0); date = to.getTime(); long millsPerDay = 1000 * 60 * 60 * 24; long dayDiff = ( date.getTime() - dateToday.getTime() ) / millsPerDay; 

after this code, I have days in the long name dayDiff. but do you really need to make a calendar with a date, set the time to 00: 00: 00: 00 and save to.getTime() in date ?

Edit: after using joda-time : Is it also possible to use joda-time to get information about the days, for example: difference == 1 ==> Tomorrow, or difference == -1 ==> yesterday or do I need to do this manually?

+4
source share
5 answers

You can use the JodaTime API as shown here .

+7
source

For this task, I always use this convenient way: (no lib, just Java 5 API)

 import java.util.concurrent.TimeUnit; Date d1 = ... Date d2 = ... long daysBetween = TimeUnit.MILLISECONDS.toDays(d2.getTime() - d1.getTime()); 

Enjoy it!

+7
source
 public long dayDiff(Date d1, Date d2) { final long DAY_MILLIS = 1000 * 60 * 60 * 24; long day1 = d1.getTime() / DAY_MILLIS; long day2 = d2.getTime() / DAY_MILLIS; return (day1 - day2); } 

Sorry for my negligence

+3
source

Instead of setting all unnecessary values ​​to 0, you can use commons lang DateUtils.truncate

In any case, DayDiff (start-end) / milliesPerDay will not work correctly, as Day Light changes are saved.

+1
source

Here's the daydiff analytic method, not based on dangerous millisecond conversions:

 public static int dayDiff(Calendar to, Calendar from){ int result = 0; int years; // global year difference from 1.jan to 1.jan years = to.get(Calendar.YEAR) - from.get(Calendar.YEAR); result = years * 365; // adding days for simple leap years ( divisible by 4 ). This an approximation that will be corrected by the negative leap years formula. result += (to.get(Calendar.YEAR)-1)/4 - (from.get(Calendar.YEAR)-1)/4; // removing days for negative leap years ( divisible by 100 ). This is still an approximation that will be corrected by the big leap years formula. result -= (to.get(Calendar.YEAR)-1)/100 - (from.get(Calendar.YEAR)-1)/100; // adding days for big leap years ( divisible by 400 ). After this formula, the days count from 1.jan.<from> to 1.jan.<to> is correct. result += (to.get(Calendar.YEAR)-1)/400 - (from.get(Calendar.YEAR)-1)/400; // adding month of to-year for(int m=0; m<to.get(Calendar.MONTH ); m++){ result += daysInMonth(m, to.get(Calendar.YEAR)); } // substracting month of from-year for(int m=0; m<from.get(Calendar.MONTH ); m++){ result -= daysInMonth(m, from.get(Calendar.YEAR)); } // adding days of to-year result += to.get(Calendar.DAY_OF_MONTH ); // substracting days of from-year result -= from.get(Calendar.DAY_OF_MONTH ); return result; } private static int daysInMonth(int m, int y){ if(m==3 || m==5 || m==8 || m==10) return 30; if(m==1) if(isLeapYear(y)) return 29; else return 28; return 31; } private static boolean isLeapYear(int y){ return (isSimpleLeapYear(y) && !isNegativeLeapYear(y)) || isBigLeapYear(y); } private static boolean isSimpleLeapYear(int y){ return y%4 == 0; } private static boolean isNegativeLeapYear(int y){ return y%100 == 0; } private static boolean isBigLeapYear(int y){ return y%400 == 0; } 

}

+1
source

All Articles