找出两个给定日期的天数差异

时间:2011-02-13 14:29:21

标签: java

 /** 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;                          
 }

我已经制定了这个逻辑......而且它没有用。它回答了错误的答案。任何人都可以帮助逻辑吗?

4 个答案:

答案 0 :(得分:3)

使用Joda日期时间: -

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

答案 1 :(得分:1)

如果您有两个日期对象,则减去毫秒时间要简单得多:

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

然后将时差转换为日差:

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

注意:这仅适用于1970年1月1日以来的日期

如果您尝试自己复制所有日历逻辑(例如闰年),那么您很可能会错误。有一些令人惊讶的微妙角落案例会咬你,而其他人已经把它全部搞清楚了。

如果您需要严肃的多日历Java日期处理,请参阅JODA:http://joda-time.sourceforge.net/

答案 2 :(得分:0)

确定闰年数的逻辑是不正确的。在这个问题上看到Alok的答案:

How to find leap year programatically in C

答案 3 :(得分:0)

如果你只是要处理1900年到2100年之间的日期,那么有一个简单的计算方法可以给你自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);
}

因此,为了获得两个日期之间的天数差异,您需要计算自1900年以来的天数并计算差异。我们的daysBetween方法如下所示:

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

不要问我这个计算的来源,因为我们从90年代初就开始使用它。