两个Calendar日期类型变量,如何得出两者之间相差的月数....
如题... --------------------编程问答-------------------- 可以用Calendar循环计算,也可以通过毫秒相减然后转成天数计算for example
System.out.println("----------method 1----------");); --------------------编程问答-------------------- 看错,月数是调用
String s1 = "2012-04-20";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = sdf.parse(s1);
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
Calendar c2 = Calendar.getInstance();
int days = 0;
while (c1.before(c2)) {
days++;
c1.add(Calendar.DATE, 1);
}
System.out.println(days);
System.out.println("----------method 2----------");
days = (int)((c2.getTime().getTime() - d1.getTime()) / (1000*60*60*24)) + 1;
System.out.println(days
c1.add(Calendar.MONTH, 1); --------------------编程问答-------------------- (get(Calendar.YEAR)相减)*12+get(Calendar.MONTH)相减
补充:Java , Java相关