How to convert integer to localized month name in Java?

I get an integer, and I need to convert to the names of the months in different locales:

Example for the en-us locale:
1 → January
2 → February

Example for locale es-mx:
1 → Enero
2 → Febrero

+94
java date locale
Jun 24 '09 at 2:00
source share
13 answers
import java.text.DateFormatSymbols; public String getMonth(int month) { return new DateFormatSymbols().getMonths()[month-1]; } 
+200
Jun 24 '09 at 14:02
source share

You need to use LLLL for offline month names. this is described in the documentation of SimpleDateFormat , for example:

 SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() ); dateFormat.format( date ); 
+31
Jan 30 '13 at 12:14
source share

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

Starting with Java 1.8 (or 1.7 and 1.6 with ThreeTen-Backport ) you can use this:

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

Note that integerMonth based on 1, i.e. 1 for January. The range is always from 1 to 12 for January-December (i.e., only according to the Gregorian calendar).

+17
Jan 13 '15 at 7:38
source share

I would use SimpleDateFormat. Someone will correct me if there is an easier way to make a mount calendar, but now I do this in code, and I'm not sure.

 import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; public String formatMonth(int month, Locale locale) { DateFormat formatter = new SimpleDateFormat("MMMM", locale); GregorianCalendar calendar = new GregorianCalendar(); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.MONTH, month-1); return formatter.format(calendar.getTime()); } 
+16
Jun 24 '09 at 2:09
source share

This is how I do it. I will leave the range check on int month 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]; } 
+14
Jun 24 '09 at 14:12
source share

Using SimpleDateFormat.

 import java.text.SimpleDateFormat; public String formatMonth(String month) { SimpleDateFormat monthParse = new SimpleDateFormat("MM"); SimpleDateFormat monthDisplay = new SimpleDateFormat("MMMM"); return monthDisplay.format(monthParse.parse(month)); } formatMonth("2"); 

Result: February

+11
Sep 28 '11 at 2:02
source share

There seems to be an error with SimpleDateFormat in Android 2.2.

To use monthly names, you must define them yourself in your resources:

 <string-array name="month_names"> <item>January</item> <item>February</item> <item>March</item> <item>April</item> <item>May</item> <item>June</item> <item>July</item> <item>August</item> <item>September</item> <item>October</item> <item>November</item> <item>December</item> </string-array> 

And then use them in your code as follows:

 /** * Get the month name of a Date. eg January for the Date 2011-01-01 * * @param date * @return eg "January" */ public static String getMonthName(Context context, Date date) { /* * Android 2.2 has a bug in SimpleDateFormat. Can't use "MMMM" for * getting the Month name for the given Locale. Thus relying on own * values from string resources */ String result = ""; Calendar cal = Calendar.getInstance(); cal.setTime(date); int month = cal.get(Calendar.MONTH); try { result = context.getResources().getStringArray(R.array.month_names)[month]; } catch (ArrayIndexOutOfBoundsException e) { result = Integer.toString(month); } return result; } 
+7
09 Oct '11 at 12:15
source share

TL; dr

 Month.of( yourMonthNumber ) // Represent a month by its number, 1-12 for January-December. .getDisplayName( // Generate text of the name of the month automatically localized. TextStyle.SHORT_STANDALONE , // Specify how long or abbreviated the name of month should be. new Locale( "es" , "MX" ) // Locale determines (a) the human language used in translation, and (b) the cultural norms used in deciding issues of abbreviation, capitalization, punctuation, and so on. ) // Returns a String. 

java.time.Month

It is much easier to do this in java.time classes, which supersede these problematic old legacy date and time classes.

The Month enumeration defines a dozen objects, one for each month.

Months from 1 to 12 for January-December.

 Month month = Month.of( 2 ); // 2 → February. 

Ask the object to generate a month name string automatically localized .

Configure TextStyle to indicate how long or short you want the name. Please note that in some languages ​​(not English) the name of the month changes if used separately or as part of the full date. So every style of text has …_STANDALONE option.

Specify Locale to determine:

  • What human language should be used in translation.
  • What cultural norms should address issues such as abbreviation, punctuation and capitalization.

Example:

 Locale l = new Locale( "es" , "MX" ); String output = Month.FEBRUARY.getDisplayName( TextStyle.SHORT_STANDALONE , l ); // Or Locale.US, Locale.CANADA_FRENCH. 



Name → Month Object

For your information, moving in the other direction (parsing the month name string to get the Month enumeration object) is not built-in. You can write your own class for this. Here is my quick attempt in this class. Use at your own risk. I have not thought about this code about any serious tests.

Using.

 Month m = MonthDelocalizer.of( Locale.CANADA_FRENCH ).parse( "janvier" ) ; // Month.JANUARY 

The code.

 package com.basilbourque.example; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.time.Month; import java.time.format.TextStyle; import java.util.ArrayList; import java.util.List; import java.util.Locale; // For a given name of month in some language, determine the matching 'java.time.Month' enum object. // This class is the opposite of 'Month.getDisplayName' which generates a localized string for a given 'Month' object. // Usage… MonthDelocalizer.of( Locale.CANADA_FRENCH ).parse( "janvier" ) → Month.JANUARY // Assumes 'FormatStyle.FULL', for names without abbreviation. // About 'java.time.Month' enum: https://docs.oracle.com/javase/9/docs/api/java/time/Month.html // USE AT YOUR OWN RISK. Provided without guarantee or warranty. No serious testing or code review was performed. public class MonthDelocalizer { @NotNull private Locale locale; @NotNull private List < String > monthNames, monthNamesStandalone; // Some languages use an alternate spelling for a "standalone" month name used without the context of a date. // Constructor. Private, for static factory method. private MonthDelocalizer ( @NotNull Locale locale ) { this.locale = locale; // Populate the pair of arrays, each having the translated month names. int countMonthsInYear = 12; // Twelve months in the year. this.monthNames = new ArrayList <>( countMonthsInYear ); this.monthNamesStandalone = new ArrayList <>( countMonthsInYear ); for ( int i = 1 ; i <= countMonthsInYear ; i++ ) { this.monthNames.add( Month.of( i ).getDisplayName( TextStyle.FULL , this.locale ) ); this.monthNamesStandalone.add( Month.of( i ).getDisplayName( TextStyle.FULL_STANDALONE , this.locale ) ); } // System.out.println( this.monthNames ); // System.out.println( this.monthNamesStandalone ); } // Constructor. Private, for static factory method. // Personally, I think it unwise to default implicitly to a 'Locale'. But I included this in case you disagree with me, and to follow the lead of the *java.time* classes. --Basil Bourque private MonthDelocalizer ( ) { this( Locale.getDefault() ); } // static factory method, instead of constructors. // See article by Dr. Joshua Bloch. http://www.informit.com/articles/article.aspx?p=1216151 // The 'Locale' argument determines the human language and cultural norms used in de-localizing input strings. synchronized static public MonthDelocalizer of ( @NotNull Locale localeArg ) { MonthDelocalizer x = new MonthDelocalizer( localeArg ); // This class could be optimized by caching this object. return x; } // Attempt to translate the name of a month to look-up a matching 'Month' enum object. // Returns NULL if the passed String value is not found to be a valid name of month for the human language and cultural norms of the 'Locale' specified when constructing this parent object, 'MonthDelocalizer'. @Nullable public Month parse ( @NotNull String input ) { int index = this.monthNames.indexOf( input ); if ( - 1 == index ) { // If no hit in the contextual names, try the standalone names. index = this.monthNamesStandalone.indexOf( input ); } int ordinal = ( index + 1 ); Month m = ( ordinal > 0 ) ? Month.of( ordinal ) : null; // If we have a hit, determine the 'Month' enum object. Else return null. if ( null == m ) { throw new java.lang.IllegalArgumentException( "The passed month name: '" + input + " is not valid for locale: " + this.locale.toString() ); } return m; } // 'Object' class overrides. @Override public boolean equals ( Object o ) { if ( this == o ) return true; if ( o == null || getClass() != o.getClass() ) return false; MonthDelocalizer that = ( MonthDelocalizer ) o; return locale.equals( that.locale ); } @Override public int hashCode ( ) { return locale.hashCode(); } public static void main ( String[] args ) { // Usage example: MonthDelocalizer monthDelocJapan = MonthDelocalizer.of( Locale.JAPAN ); try { Month m = monthDelocJapan.parse( "pink elephant" ); // Invalid input. } catch ( IllegalArgumentException e ) { // … handle error System.out.println( "ERROR: " + e.getLocalizedMessage() ); } // Ignore exception. (not recommended) if ( MonthDelocalizer.of( Locale.CANADA_FRENCH ).parse( "janvier" ).equals( Month.JANUARY ) ) { System.out.println( "GOOD - In locale "+Locale.CANADA_FRENCH+", the input 'janvier parses to Month.JANUARY." ); } } } 



About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old obsolete date and time classes, such as java.util.Date , Calendar , and SimpleDateFormat .

The Joda-Time project, currently in maintenance mode , recommends switching to the java.time classes.

To learn more, see the Oracle Tutorial . And a search for many examples and explanations. JSR 310 specification .

You can exchange java.time objects directly with your database. Use a JDBC driver that conforms to JDBC 4.2 or later. No strings needed, no java.sql.* Needed.

Where to get java.time classes?

  • Java SE 8 , Java SE 9 and later
    • Built in.
    • Part of the standard Java API with an embedded implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the functionality of java.time has been ported to Java 6 and 7 in ThreeTen-Backport .
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier versions of Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP ....

The ThreeTen-Extra project extends java.time with additional classes. This project is a testing ground for possible future additions to java.time. Here you can find some useful classes such as Interval , YearWeek , YearQuarter and others .

+6
Aug 30 '16 at 6:58
source share

There is a problem when you use the DateFormatSymbols class for the getMonthName method to get Month by Name, which shows Month by Number on some Android devices. I solved this problem as follows:

In String_array.xml

 <string-array name="year_month_name"> <item>January</item> <item>February</item> <item>March</item> <item>April</item> <item>May</item> <item>June</item> <item>July</item> <item>August</item> <item>September</item> <item>October</item> <item>November</item> <item>December</item> </string-array> 

In a Java class, just call this array as follows:

 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 :)

+1
Mar 09 '15 at 8:57
source share
  public static void main(String[] args) { SimpleDateFormat format = new SimpleDateFormat("MMMMM", new Locale("en", "US")); System.out.println(format.format(new Date())); } 
0
Jun 07 '16 at 20:52
source share

Just insert a line

 DateFormatSymbols.getInstance().getMonths()[view.getMonth()] 

will do his job.

0
Feb 22 '19 at 19:25
source share

Try using this in a very simple way and name it as your own.

 public static String convertnumtocharmonths(int m){ String charname=null; if(m==1){ charname="Jan"; } if(m==2){ charname="Fev"; } if(m==3){ charname="Mar"; } if(m==4){ charname="Avr"; } if(m==5){ charname="Mai"; } if(m==6){ charname="Jun"; } if(m==7){ charname="Jul"; } if(m==8){ charname="Aou"; } if(m==9){ charname="Sep"; } if(m==10){ charname="Oct"; } if(m==11){ charname="Nov"; } if(m==12){ charname="Dec"; } return charname; } 
0
May 17 '19 at 14:34
source share

Kotlin expansion

 fun Int.toMonthName(): String { return DateFormatSymbols().months[this] } 

Usage

Usage
 calendar.get(Calendar.MONTH).toMonthName() 
0
Aug 26 '19 at 10:27
source share



All Articles