开发者

finding the difference in days from two given dates

 /** Determines the difference in days between d and this Date.For example, 
  *  i开发者_Go百科f 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?


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);
}


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/


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜