求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这样的形式的。 jtree怎么让节点是一个panel控件,并且,panel控件被激活 卡牌游戏。消息机制的问题 计算机进制转换 关于正则预搜索的疑惑 关于多线程中为减少争用采用的锁崩溃方法 给打算用MySQL的人,泼一点冷水! 第一次接触jni的一个初级问题 Enumeration 问题 一个类的finalize()不是只能执行一次吗?怎么…… 谁知道用友NC是怎么做的吗 什么“一次编译到处执行”,应该是“一次编译到处调试”? 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