How can I convert an Integer to localized month name in Java?

后端 未结 13 2144
眼角桃花
眼角桃花 2020-11-27 03:20

I get an integer and I need to convert to a month names in various locales:

Example for locale en-us:
1 -> January
2 -> February

Example for locale e

13条回答
  •  庸人自扰
    2020-11-27 03:49

    There is an issue when you use DateFormatSymbols class for its getMonthName method to get Month by Name it show Month by Number in some Android devices. I have resolved this issue by doing this way:

    In String_array.xml

    
        January
        February
        March
        April
        May
        June
        July
        August
        September
        October
        November
        December
        
    

    In Java class just call this array like this way:

    public String[] getYearMonthName() {
            return getResources().getStringArray(R.array.year_month_names);
            //or like 
           //return cntx.getResources().getStringArray(R.array.month_names);
        } 
    
          String[] months = getYearMonthName(); 
               if (i < months.length) {
                monthShow.setMonthName(months[i] + " " + year);
    
                }
    

    Happy Coding :)

提交回复
热议问题