public static int getDays(String date1,String date2){
    int days = 0;
    int year1 = Integer.parseInt(date1.substring(0,4));
    int month1 = Integer.parseInt(date1.substring(5,7));
    int day1 = Integer.parseInt(date1.substring(8,10));
    int year2 = Integer.parseInt(date2.substring(0,4));
    int month2 = Integer.parseInt(date2.substring(5,7));
    int day2 = Integer.parseInt(date2.substring(8,10));
    for(int i=year1;i<year2;i++){
      Date tempDate = new Date(i);
      days += tempDate.getYearDays();
    }
    Date dateA = new Date(year1);
    Date dateB = new Date(year2);
    int monthDays1[] = dateA.getMonthDays();
    int monthDays2[] = dateB.getMonthDays();
    for(int i=0;i<month1-1;i++){
      days -= monthDays1[i];
    }
    for(int i=0;i<month2-1;i++){
      days += monthDays2[i];
    }
    System.out.println(day1+","+day2);
    days -= day1;
    days += day2;
    return days;
  }这个是方法,还有一个类
public class Date{
  int year;
  int yearDays;
  int monthDays[] = new int[12];
  public Date(int a){
    year = a;
    run();
  }
  public int getYearDays(){
    return yearDays;
  }
  public void run() {
    monthDays[0] = 31;
    monthDays[1] = 28;
    monthDays[2] = 31;
    monthDays[3] = 30;
    monthDays[4] = 31;
    monthDays[5] = 30;
    monthDays[6] = 31;
    monthDays[7] = 31;
    monthDays[8] = 30;
    monthDays[9] = 31;
    monthDays[10] = 30;
    monthDays[11] = 31;
    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
      monthDays[1] = 29;
      yearDays = 366;
    }
    else{
      yearDays = 365;
    }
  }
  public int[] getMonthDays(){
    return monthDays;
  }
}

解决方案 »

  1.   

    Date类里的这个方法
    Date d=new Date(2006,12,1);
    int i=d.compareTo(Date anotherDate);注释:
    compareTopublic int compareTo(Date anotherDate)
    Compares two Dates for ordering.Parameters:anotherDate - the Date to be compared.Returns:the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.
      

  2.   

    private int getDayDiffProc(Date oSrc, Date oTar) {
        long _lSrc = oSrc.getTime();  // convert to milli-seconds
        long _lTar = oTar.getTime();    _lSrc /= (24 * 60 * 60 * 1000); // 換算成天數
        _lTar /= (24 * 60 * 60 * 1000); // 換算成天數    if (_lSrc < _lTar) {
          long _lTmp = _lSrc;
          _lSrc = _lTar;
          _lTar = _lTmp;
        }
        return (int) (_lSrc - _lTar);
      }
      

  3.   

    public int getDays(String sd1,String sd2){
    int days = 0;
    try {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date d1 = dateFormat.parse(sd1);
    Date d2 = dateFormat.parse(sd2);
    long time = d2.getTime() -d1.getTime();
    days =(int) time/(24*60*60*1000) + 1 ;
    } catch (Exception e) { 
    e.printStackTrace(); 
    }
    return days;
    }
      

  4.   

    改进:
    long time = d2.getTime() - d1.getTime();
    改为:
    long time = Math.abs(d2.getTime() - d1.getTime());