Date d1 = xxx
Date d2 = xxx
d1_D2_jueduizhi = (d1.getTime() - d2.getTime())/(24*3600*1000);
d1_D2_tianshucha = d1.getTime()/(24*3600*1000) - d2.getTime()/(24*3600*1000);that's ok
先除后减和先减后除是有区别的,看你的需要了。

解决方案 »

  1.   


    import java.util.*;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;public class DateDiff{

    public static void main(String[] args){
    String s1= "20040301";
    String s2="20040330";
      DateFormat df1 = new SimpleDateFormat("yyyyMMDD");
             Date date1=new Date();
             Date date2=new Date();
            String strResult="";
    Calendar d1=Calendar.getInstance();
            Calendar d2=Calendar.getInstance();        try{
         date1= df1.parse(s1) ;
         date2= df1.parse(s2) ;
         d1.setTime(date1);    
         d2.setTime(date2);
          
    long result;
            result=dateDiff("DAY",d1,d2);
            strResult=String.valueOf(result);
             }catch(ParseException e){     }
      System.out.println(strResult);

       }
        public static long dateDiff(String part, Calendar startCal, Calendar endCal) {
        long deltaMs = endCal.getTimeInMillis() - startCal.getTimeInMillis();    if ("MILLISECOND".equalsIgnoreCase(part)) {
          return deltaMs;
        }    int reverse = 1;
        //if start time is after the end time
        if(startCal.after(endCal)){ 
          reverse = -1;
          deltaMs = -deltaMs;
          Calendar tmp = startCal;
          startCal = endCal;
          endCal = tmp;
        }    long res = 0;    if ("YEAR".equalsIgnoreCase(part)) {
          res = endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR);
        } else if ("MONTH".equalsIgnoreCase(part)) {
          int year = endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR);
          res += year * 12;
          res += endCal.get(Calendar.MONTH) - startCal.get(Calendar.MONTH);
        } else if ("WEEK".equalsIgnoreCase(part)) {
          res += deltaMs / (7 * 24 * 3600 * 1000);
          int w = startCal.get(Calendar.DAY_OF_WEEK);
          int tmp = (int)(deltaMs % (7*24*3600*1000));
          startCal.add(Calendar.MILLISECOND,tmp);
          int w2 = startCal.get(Calendar.DAY_OF_WEEK);
          if(w2<w || (w2 == w && tmp>(24*3600*1000))){
            res++;
          }
        } else{
          long base = 0;
          int type = 0;
          if ("DAY".equalsIgnoreCase(part)) {
            type = Calendar.DATE;
            base = 24 * 3600 * 1000;
          } else if ("HOUR".equalsIgnoreCase(part)) {
            type = Calendar.HOUR;
            base = 3600 * 1000;
          } else if ("MINUTE".equalsIgnoreCase(part)) {
            type = Calendar.MINUTE;
            base = 60 * 1000;
          } else if ("SECOND".equalsIgnoreCase(part)) {
            type = Calendar.SECOND;
            base = 1000;
          }else{
            return Long.MIN_VALUE;
          }
          int cur = startCal.get(type);
          res = deltaMs / base;
          int tmp = (int)(deltaMs % base);
          startCal.add(Calendar.MILLISECOND,tmp);
          if(startCal.get(type)!=cur){
            res++;
          }
        }    return res*reverse;
      }}