日期:2014-05-20  浏览次数:20901 次

求助,如何的出两个时间之间相差多少个月????
传入两个时间如何得出   两个时间相差多少月呢   如String   bejintime= "2006-02 ";
String   endtime= "2007-09 ";


------解决方案--------------------
String bejintime= "2006-02 ";
String endtime= "2007-09 ";
int y1 = Integer.paseInt(endtime.substring(0,4));//不记得是不是这样写了,反正得到2007
int y2 = Integer.paseInt(bejintime.substring(0,4));
int m1 = Integer.paseInt(endtime.substring(5,7));//看看能不能得到09
int m2 = Integer.paseInt(bejintime.substring(5,7));//看看能不能得到09
int result = 12*(y1-y2)+(m1-m2)
------解决方案--------------------
package util;

import java.util.*;
import java.text.*;
public class Time {

/**
* @param args
*/
public static void main(String[] args) throws ParseException{
// TODO Auto-generated method stub
String time = "2006-02 ";
String time2 = "2007-09 ";
DateFormat df = new SimpleDateFormat( "yyyy-MM ");
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(df.parse(time));
c2.setTime(df.parse(time2));
ArrayList <String> list = new ArrayList <String> ();
while(!c1.after(c2)){
list.add(df.format(c1.getTime()));
c1.add(Calendar.MONTH, 1);
}
System.out.println(list);
}

}