不可能Date类是取当前系统的时间!这2个DateDate型变量的差一定是0,如果忽略时分秒的话。

解决方案 »

  1.   

    在java.util.Date类中有一个方法public int compareTo(Date anotherDate)
    应该可以解决你的问题。
    试一下吧。
      

  2.   

    Date date1 = new Date(1061521133802L);
    System.out.println(date1);
    Date date2 = new Date(1001521133802L);
    System.out.println(date2);
    Calendar cal = Calendar.getInstance();
    Date sub = new Date(date1.getTime() - date2.getTime());
    cal.setTime(sub);
    System.out.print("相差" + (cal.get(Calendar.YEAR) - 1970) + "年");
    System.out.print((cal.get(Calendar.MONTH) + 1) + "月");
    System.out.println(cal.get(Calendar.DAY_OF_MONTH) + "天");
      

  3.   

    //计算两个日期之间相隔的天数
     public int getDayInRange(Date lowerLimitDate,Date upperLimitDate)
       throws InsufficientDateException{
       
       long upperTime,lowerTime;
       upperTime=upperLimitDate.getTime();
       lowerTime=lowerLimitDate.getTime();
       if(upperTime<lowerTime)
         throw new InsufficientDateException();
       Long result=new Long((upperTime-lowerTime)/(1000*60*60*24));
       return result.intValue();   
     }
      

  4.   

    推荐使用java.util.Calendar来计算相差年月日等等。有些东西还必须事先定义或做声明好,比如说,2003/8/21 23:00:00 和 2003/8/22 1:00:00 之间差几天?0天还是1天?如果说是一天,我们知道两者间差2个小时,那么难道它们之间相差一天又两个小时?