Java中两天之间的错误计数天数

时间:2014-12-02 04:55:34

标签: java

我想计算两个日期之间的天数。我使用此代码

    public Integer getDays() {
       Calendar calStart = new GregorianCalendar();
       Calendar calEnd = new GregorianCalendar();

       String start = String.valueOf(getDataSet().getDate("startrentdate"));
       String startYear = start.substring(0, 4);
       String startMonth = start.substring(5, 7);
       String startDate = start.substring(8);
       int istartYear = Integer.parseInt(startYear);
       int istartMonth = Integer.parseInt(startMonth);
       int istartDate = Integer.parseInt(startDate);

       calStart.set(istartYear, istartMonth, istartDate);

       String end = String.valueOf(getDataSet().getDate("endrentdate"));
       String endYear = end.substring(0, 4);
       String endMonth = end.substring(5, 7);
       String endDate = end.substring(8);
       int iendYear = Integer.parseInt(endYear);
       int iendMonth = Integer.parseInt(endMonth);
       int iendDate = Integer.parseInt(endDate);

       calEnd.set(iendYear, iendMonth, iendDate);

       int day = daysBetween(calStart.getTime(), calEnd.getTime());

       return day;
    }

    public int daysBetween(java.util.Date d1, java.util.Date d2) {
       return (int) ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
    }

但是当我试着计算不同月份的日子时,我得到了错误的结果。 也许有另一种方法可以做到这一点?

0 个答案:

没有答案