1

For calculating days between two dates(Date or Calendar object) consider the end date also for calculation.

I used below method:

    public static int daysBetween(Date d1, Date d2) { 
return Days.daysBetween( new LocalDate(d1.getTime()), new LocalDate(d2.getTime())).getDays(); 
}

For example

    Date d1 = new Date(); // 30-08-2016
Calendar c= Calendar.getInstance();
c.add(Calendar.YEAR, 2016);
c.add(Calendar.MONTH, Calendar.AUGUST);
c.add(Calendar.DAY_OF_MONTH, 31)// 31-08-2016
Date d2 = c.getTime();

daysBetween(d2, d1);

The method returns '1' but I want as '2' because I need to consider end date also it means 30 and 31.

For that requirement i can add the +1 whatever that method returns or before returning the value also. This is not a correct way to solve the problem and need to consider leap year dates also.

Please help me resolve the problem.

pandiaraj
  • 580
  • 1
  • 6
  • 18
  • The technical term for what you are *not* asking is ‘Half-Open’, where the span of time is defined as the beginning being *inclusive* while the ending is *exclusive*. What you *are* asking for is “Closed”, both beginning & ending being inclusive. The Half-Open approach is commonly used, and more sensible & practical. When used consistently, Half-Open makes date-time handling logic cleaner, clearer, and simpler. Where your requirements demand a Closed number, simply adding one is the correct solution, except perhaps a zero count (which you will need to ponder and work out in your requirements). – Basil Bourque Aug 30 '16 at 21:02
  • @BasilBourque In general, half-open is good because it helps to handle intervals where there are potentially more precise units then actually used, but for date intervals the situation is different because the day as unit is already the smallest possible unit. As usual in the area of banks and assurances, people working in this area (like me) use closed date intervals (for example one full month is commonly modelled as closed range [2016-08-01/2016-08-31] with the advantage not to show another month instead of as something like [2016-08-01/2016-09-01) – Meno Hochschild Aug 31 '16 at 06:01

1 Answers1

2

I would not mix up two different libraries, here Joda-Time and java.util.Calendar/Date. Instead the duration in days between today and the end of August can be evaluated in Joda-Time this way:

Days.daysBetween(LocalDate.now(), new LocalDate(2016, 8, 31)).getDays(); // 30

This automatically takes into account leap days and is correct if you want to add the result to the original date to achieve the end date. However:

The length of a closed date interval is different from the count of elapsed days necessary in addition. For this case, you simply add one day to determine the length between start and end inclusive. For example, the August has [2016-08-31] - [2016-08-01] + 1 = 31 - 1 + 1 = 31 days, but you add only 30 days to the first of August to get the end of August. The difference of one day is quite natural and is due to different concepts about length (of a closed interval) versus the duration necessary in date addition.

Nothing is wrong about adding one day to duration to achieve the length.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126