/**
* 获取最近12个月
*/
public static void main(String[] args){
LocalDate today = LocalDate.now();
for(long i = 1L;i <= 12L; i++){
LocalDate localDate = today.minusMonths(i);
String ss = localDate.toString().substring(0,7);
System.out.println(localDate);
System.out.println(ss);
System.out.println("==============");
}
}
/**
* 获取最近12个月
*/
public static void main(String[] args) {
String[] last12Months = new String[12];
Calendar cal = Calendar.getInstance();
//如果当前日期大于二月份的天数28天或者29天会导致计算月份错误,会多出一个三月份,故设置一个靠前日期解决此问题
cal.set(Calendar.DAY_OF_MONTH, 1);
for (int i = 0; i < 12; i++) {
last12Months[11 - i] = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1);
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1); //逐次往前推1个月
}
}
网友评论