2011-02-13 58 views
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;       
} 

我已经完成了这个逻辑......并且它不工作。它返回了错误的答案。任何人都可以帮助逻辑吗?发现两个给定日期的差异

+1

什么不起作用?你能举一些例子输入,输出和期望的输出吗? – 2011-02-13 14:32:16

回答

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/

+0

如果两次来自不同的时间,这可能不会产生预期的结果。 – 2011-02-13 14:41:05

+0

是的。通过将getTime()值四舍五入到最近的一天,可以轻松解决这个问题。 – payne 2011-02-13 16:19:28

3

使用乔达日期时间: -

@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); 
} 
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年代初用它来了。