获取最近12个月,经常用于统计图表的X轴

/**
 * 获取最近12个月,经常用于统计图表的X轴
 */
public static String[] getLast12Months() {
    String[] last12Months = new String[12];
    Calendar cal = Calendar.getInstance();
    //要先+1,才能把本月的算进去
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
    // 加此行,否则3月重复
    cal.set(Calendar.DATE, 1);

    for (int i = 0; i < 12; i++) {
        //逐次往前推1个月
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        last12Months[11 - i] = cal.get(Calendar.YEAR) + "-" + fillZero((cal.get(Calendar.MONTH) + 1), 2);
    }

    return last12Months;
}

你可能感兴趣的:(Util)