需求:设定当前开始递增月份:eg:201802,需要递增12个月的数据,并输出,eg:
201802
201803
201804
201805
201806
201807
201808
201809
201810
201811
201812
201901
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class xxx {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMM");
String startDate="201802";//当前日期+1的月份开始
int addMonths = 12;//需要建几个月的索引
for(int i=0; i<addMonths; i++){
//Thread.sleep(10000);
Date dt=sdf.parse(startDate);
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
if(i != 0){
rightNow.add(Calendar.MONTH,1);//日期加1个月
}
Date dt1=rightNow.getTime();
String reStr = sdf.format(dt1);
System.out.println(reStr);
startDate=reStr;
}
}
}