获取当前系统时间最近12月的年月(含当月)

获取当前系统时间最近12月的年月(含当月)

public static List getLatest12Month(){
String[] latest12Months = new String[12];
Calendar cal = Calendar.getInstance();
List listX = new ArrayList();
//要先+1,才能把本月的算进去
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1);
for(int i=0; i<12; i++){
//逐次往前推1个月
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
latest12Months[11-i] = cal.get(Calendar.YEAR)+ "-" +fillZero(cal.get(Calendar.MONTH)+1);
listX.add(latest12Months[11-i]);
}
return listX;
}
public static String fillZero(int i){
String month = "";
if(i<10){
month = "0" + i;
}else{
month = String.valueOf(i);
}
return month;
}
posted @ 2021-08-10 13:39  游走人间的蒲公英  阅读(185)  评论(0编辑  收藏  举报