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

时间:2019-06-01 06:26:54

标签: android android-datepicker

我创建了月度选择器,虽然工作正常,但几天后就出现了问题。我当时正在使用 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;
}

输出:

这是“月”选择器的屏幕截图,其中显示了“三月”代替“三月”

it's the screenshot of Month picker, in which it showing march in place of Feb

1 个答案:

答案 0 :(得分:0)

  

我在您的代码中做了一些改动,现在它给了我几个月的准确名称。我还输入了 Log.e 来检查名称。

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;
}
  

下面,我将日志屏幕截图用作证明。   enter image description here