String t1="2005/01/01";
String t2="2004/12/31";

解决方案 »

  1.   

    先用dateFormat转换成Date
    再转换成Calendar
    设为c1=2004/12/31,c2=2005/01/01
    最后
    int i=0;
    while(c1.before(c2))
    {
    c1.roll(Calendar.MONTH);
    i++;
    }
    System.out.println(i);
      

  2.   

    import java.text.*;
    public class Test
    {
        public int returnDate(String date,String anotherDate)
        {
            int returnValue=0;
            SimpleDateFormat format=new SimpleDateFormat("yyyy/MM/dd");
           
            try
    {
    returnValue=(int)((format.parse(anotherDate).getTime()-format.parse(date).getTime())/(1000*60*60*24));
    }
    catch(ParseException e)
    {
    e.printStackTrace();
    }
            if(returnValue<0)
                returnValue=-returnValue;
            return returnValue;
            
            
        }
        public static void main(String[] args)
        {
           Test t=new Test();
           System.out.println(t.returnDate("2005/01/01","2004/12/31"));
        }
    }
      

  3.   

    把字符串用simpledateformat转成date,用gettime方法得到long类型时间.
    相减再转成date
      

  4.   

    在答案是1的情况下。System.out.println("Date Diff is:" + ((Integer.parseInt(t1.substring(0,4)) - Integer.parseInt(t2.substring(0,4))) * 12 + Integer.parseInt(t1.substring(5,7)) - Integer.parseInt(t2.substring(5,7))));前提,日期必须是正确的。而且是YYYY/MM/DD这样的形式的。