求2005/01/01与2004/12/31相差几个月? String t1="2005/01/01";String t2="2004/12/31"; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先用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); 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")); }} 把字符串用simpledateformat转成date,用gettime方法得到long类型时间.相减再转成date 在答案是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这样的形式的。 escapeHTML 再问public protected 和 private 发现一个有趣的问题,请高手解答 怎么自动生成1到10的char数组? 接口问题,你一定知道的(小弟刚刚学,对于你们来说简单了,送分)!! 书上的一个例子,可就是不知道是什么错误 初学者问题 大家来看看哦 能不能直接打开网络上的文件 请教字典类的写法 为什么没人用Visual j++ 呀?都用Jbuilder??两个有什么不同 什么“一次编译到处执行”,应该是“一次编译到处调试”? Sun 中国技术社区发布JavaChina 2005大会演讲资料
再转换成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);
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"));
}
}
相减再转成date