2
 /** Determines the difference in days between d and this Date.For example, 
  *  if this Date is 12/15/1997 and d is 12/14/1997, the difference is 1.
  *  If this Date occurs before d, the result is negative.
  *  @return the difference in days between d and this date.
  */

public int difference(Date d) {
int NoOfLeapYr_d = d.year/4;    
int NoOfLeapYr_this  = this.year/4;
int daysofthis = NoOfLeapYr_this + (this.year-1 * 365) + this.dayInYear();
int daysofd = NoOfLeapYr_d + (d.year-1 * 365) + d.dayInYear();
   return daysofd - daysofthis;                          
 }

I have made this logic ...and it's not working. It's returning the wrong answer. Can anybody help in the logic?

1
  • 1
    What isn't working? Can you give some example input, along with its output, and the expected output? Commented Feb 13, 2011 at 14:32

4 Answers 4

3

Using Joda Datetime:-

@Test
public void testOneDayEarlier() {
    DateTime fromDate = new DateTime(2011, 2, 12, 0, 0, 0, 0);
    DateTime toDate = new DateTime(2011, 2, 13, 0, 0, 0, 0);

    int days = Days.daysBetween(fromDate, toDate).getDays();
    assertEquals("fromDate is one day earlier than toDate", 1, days);
}

@Test
public void testOneDayLater() {
    DateTime fromDate = new DateTime(2011, 2, 13, 0, 0, 0, 0);
    DateTime toDate = new DateTime(2011, 2, 12, 0, 0, 0, 0);

    int days = Days.daysBetween(fromDate, toDate).getDays();
    assertEquals("fromDate is one day later than toDate", -1, days);
}

@Test
public void testSameDay() {
    DateTime fromDate = new DateTime(2011, 2, 13, 0, 0, 0, 0);
    DateTime toDate = new DateTime(2011, 2, 13, 0, 0, 0, 0);

    int days = Days.daysBetween(fromDate, toDate).getDays();
    assertEquals("fromDate is the same as toDate", 0, days);
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you have two date objects, it's much simpler to subtract the millisecond times:

long diff = today.getTime() - d1.getTime();

And then convert the time difference to a day difference:

long days_diff = diff / (1000*60*60*24);

Note: this only works for dates since Jan 1, 1970

If you try to replicate all the calendar logic yourself (e.g. leap years), there's a good chance you'll get it wrong. There are a surprising number of subtle corner cases to bite you, and others have already figured it all out.

And if you need serious multi-calendar Java date handling, see JODA: http://joda-time.sourceforge.net/

2 Comments

This may not give the expected results if the two times are from different times of day.
True. That can be easily addressed by rounding the getTime() values to the nearest day before subtracting.
0

Your logic to determine the number of leap years is incorrect. See the answer from Alok on this question:

How to find leap year programatically in C

Comments

0

If you are only going to be dealing with dates between the years 1900 and 2100, there is a simple calculation which will give you the number of days since 1900:

public static int daysSince1900(Date date) {
    Calendar c = new GregorianCalendar();
    c.setTime(date);

    int year = c.get(Calendar.YEAR);
    if (year < 1900 || year > 2099) {
        throw new IllegalArgumentException("daysSince1900 - Date must be between 1900 and 2099");
    }
    year -= 1900;
    int month = c.get(Calendar.MONTH) + 1;
    int days = c.get(Calendar.DAY_OF_MONTH);

    if (month < 3) {
        month += 12;
        year--;
    }
    int yearDays = (int) (year * 365.25);
    int monthDays = (int) ((month + 1) * 30.61);

    return (yearDays + monthDays + days - 63);
}

Thus, to get the difference in days between two dates, you calculate their days since 1900 and calc the difference. Our daysBetween method looks like this:

public static Integer getDaysBetween(Date date1, Date date2) {
    if (date1 == null || date2 == null) {
        return null;
    }

    int days1 = daysSince1900(date1);
    int days2 = daysSince1900(date2);

    if (days1 < days2) {
        return days2 - days1;
    } else {
        return days1 - days2;
    }
}

And don't ask me where this calculation came from because we've used it since the early '90s.

1 Comment

So where this calculation came from?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.