public static void main(String[] args) { // 展示过去12个月 Calendar calendar = Calendar.getInstance(); Date now = null; try { now = DateUtils.parseDate( DateFormatUtils.format(new Date(), "yyyy-MM"), "yyyy-MM"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } calendar.setTime(now); for (int i = 0; i < 12; i++) { // 月份递减(不包含本月) calendar.add(Calendar.MONTH, -1); // 包含本月(此句提前) System.out.println(DateFormatUtils.format(calendar, "yyyy-MM")); } }
时间: 2024-10-08 09:04:28