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

后端 未结 13 2041
眼角桃花
眼角桃花 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:50

    Here's how I would do it. I'll leave range checking on the int month up to you.

    import java.text.DateFormatSymbols;
    
    public String formatMonth(int month, Locale locale) {
        DateFormatSymbols symbols = new DateFormatSymbols(locale);
        String[] monthNames = symbols.getMonths();
        return monthNames[month - 1];
    }
    
    0 讨论(0)
提交回复
热议问题