import java.text.DateFormat;   
import java.text.SimpleDateFormat;   
import java.util.Calendar;   
import java.util.Date;   /**   
 * @description 日期比较天 月 年    
 */  public class DateTest {       public static void main(String[] args) {   
        String date = "1990-07-31";     
        DateTest.compareDate(date, "2009-08-01", 0);   
        DateTest.compareDate(date, "2009-08-01", 1);   
        DateTest.compareDate(date, "2009-08-01", 2);  
    }   
       
    /**   
     * @param stype 返回值类型   0为多少天,1为多少个月,2为多少年   
     * @return   
     */  
    public static int compareDate(String date1,String date2,int stype){   
        int n = 0;   
           
        String[] u = {"天","月","年"};   
        String formatStyle ="yyyy-MM-dd";   
        DateFormat df = new SimpleDateFormat(formatStyle);   
        Calendar c1 = Calendar.getInstance();   
        Calendar c2 = Calendar.getInstance();   
        try {   
            c1.setTime(df.parse(date1));   
            c2.setTime(df.parse(date2));   
        } catch (Exception e3) {   
            System.out.println("wrong occured");   
        }   
        while (!c1.after(c2)) {       
            n++;   
            if(stype==1){   
                c1.add(Calendar.MONTH, 1);// 比较月份,月份+1   
            }   
            else{   
                c1.add(Calendar.DATE, 1); // 比较天数,日期+1   
            }   
        }   
           
        n = n-1;   
           
        if(stype==2){  
            int yushu=(int)n%365;  
            n =  yushu==0?(n/365):((n/365)-1);
        }      
           
        System.out.println(date1+" -- "+date2+" 相差多少"+u[stype]+":"+n);         
        return n;   
    }   
       
    /**   
     * 得到当前日期   
     * @return   
     */  
    public static String getCurrentDate() {   
        Calendar c = Calendar.getInstance();   
        Date date = c.getTime();   
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");   
        return simple.format(date);       }   

运行结果: 
1990-07-31 -- 2009-08-01 相差多少天:6941 
1990-07-31 -- 2009-08-01 相差多少月:228 
1990-07-31 -- 2009-08-01 相差多少年:18  -------------------------应该是19年还有
1990-07-31---2009-07-30  相差应该是18年

解决方案 »

  1.   

     
    if(stype==2){  
        int yushu=(int)n%365;  
        n =  yushu==0?(n/365):((n/365)-1); 
    }      请问楼主,算年的时候为什么要判断yushu?直接n=n/365,得不到正确的答案吗?
      

  2.   

    错误发生的都是在换算年的问题上,用days自己来换算年需要考虑是否闰年的问题,很容易出错。不建议这么用。就用calendar的api不就得了,况且你的天,月的计算也都是应用的api。问题就出在
    if(stype==2){  
        int yushu=(int)n%365;  
        n =  yushu==0?(n/365):((n/365)-1); 
    }所以改成
    if(stype==1){  
                    c1.add(Calendar.MONTH, 1);// 比较月份,月份+1  
                }  
                else if (stype==0){  
                    c1.add(Calendar.DATE, 1); // 比较天数,日期+1  
                }  
                else if(stype==2){
                 c1.add(Calendar.YEAR, 1); // 比较年数,年+1
                }                          下面的代码也不需要了
    if(stype==2){  
        int yushu=(int)n%365;  
        n =  yushu==0?(n/365):((n/365)-1); 
    }
    就可以了。或者
    你不用calendar 自己写方法的话,
    year = c2的年 - c1的年
    if(c2的月>c1的月) ;
    else if(c2的月<c1的月) year--;
    else{
       if(c2的天<=c1的天) year--;
    }
    返回 year