/** * 获取当前月,从上个月开始的最近12个月 * @return */ public static String[] getLast12Months(){ String[] last12Months = new String[12]; Calendar cal = Calendar.getInstance(); for (int i = 0; i < 12; i++) { if (cal.get(Calendar.MONTH) - i < 1) { //last12Months[11 - i] = cal.get(Calendar.YEAR) - 1 + "-" + fillZero((cal.get(Calendar.MONTH) - i + 12 * 1)); last12Months[11 - i] = cal.get(Calendar.YEAR) - 1 + fillZero((cal.get(Calendar.MONTH) - i + 12 * 1)); } else { //last12Months[11 - i] = cal.get(Calendar.YEAR) + "-" + fillZero((cal.get(Calendar.MONTH) - i)); last12Months[11 - i] = cal.get(Calendar.YEAR) + fillZero((cal.get(Calendar.MONTH) - i)); } //System.out.println(last12Months[11 - i]); } return last12Months; }
记录一下平常遇到的问题及新的知识,方便以后查看