Java 比较两日期相差天数

(版本1)

publicstaticint getIntervalDays(Date fDate, Date oDate) {

       if (null == fDate || null == oDate) {

           return -1;

       }

       long intervalMilli = oDate.getTime() - fDate.getTime();

       return (int) (intervalMilli / (24 * 60 * 60 * 1000));

    }

  

 

    (版本2)

publicstaticint daysOfTwo(Date fDate, Date oDate) {

       Calendar aCalendar = Calendar.getInstance();

       aCalendar.setTime(fDate);

       int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);

       aCalendar.setTime(oDate);

       int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);

       return day2 - day1;

    }

 

posted @ 2015-02-12 16:24  德玛西亚冲锋  阅读(230)  评论(0编辑  收藏  举报