simpledateformat 本地化错误的月份名称列表

simpledateformat localizing wrong months name list

我创建了 Month-year picker.It 工作正常,但几天后它产生了问题。我正在使用 Simpledateformat 本地化获取月份名称列表。 但现在它重复了一些月份名称。我不知道为什么? 请帮我摆脱它。 这是我的代码:

private static String[] monthsList() {
    if (MONTHS_LIST == null) {
        int[] months = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        String[] stringMonths = new String[months.length];
        for (int i = 0; i < months.length; i++) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat monthDate = new SimpleDateFormat(MONTH_FORMAT, Locale.US);
            calendar.set(Calendar.MONTH, months[i]);
            String monthName = monthDate.format(calendar.getTime());
            stringMonths[i] = capitalize(monthName);
        }
        MONTHS_LIST = stringMonths;
    }
    return MONTHS_LIST;
}

输出:

这是月份选择器的屏幕截图,其中显示的是三月而不是二月

I do little bit changes inside your code and now it is giving me the accurate name of months. I also put Log.e for check the name.

private static String[] monthsList() {
    if (MONTHS_LIST == null) {
        int[] months = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        String[] stringMonths = new String[months.length];
        for (int i = 0; i < months.length; i++) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat monthDate = new SimpleDateFormat("MMMM");
            calendar.set(Calendar.MONTH, months[i]);
            String monthName = monthDate.format(calendar.getTime());
            stringMonths[i] = capitalize(monthName);

            Log.e("month name",monthName);
        }
        MONTHS_LIST = stringMonths;
    }
    return MONTHS_LIST;
}

Below I put log screenshot for proof.