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

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


import java.text.DateFormatSymbols;public String getMonth(int month) {    return new DateFormatSymbols().getMonths()[month-1];}


You need to use LLLL for stand-alone month names. this is documented in the SimpleDateFormat documentation, such as:

SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() );dateFormat.format( date );


tl;dr

Month                             // Enum class, predefining and naming a dozen objects, one for each month of the year. .of( 12 )                         // Retrieving one of the enum objects by number, 1-12. .getDisplayName(    TextStyle.FULL_STANDALONE ,     Locale.CANADA_FRENCH          // Locale determines the human language and cultural norms used in localizing. )

java.time

Since Java 1.8 (or 1.7 & 1.6 with the ThreeTen-Backport) you can use this:

Month.of(integerMonth).getDisplayName(TextStyle.FULL_STANDALONE, locale);

Note that integerMonth is 1-based, i.e. 1 is for January. Range is always from 1 to 12 for January-December (i.e. Gregorian calendar only).