Month name as a string

I'm trying to return the name of the month as a String, for instance "May", "September", "November".

I tried:

int month = c.get(Calendar.MONTH);

However, this returns integers (5, 9, 11, respectively). How can I get the month name?


Solution 1:

Use this :

Calendar cal=Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
String month_name = month_date.format(cal.getTime());

Month name will contain the full month name,,if you want short month name use this

 SimpleDateFormat month_date = new SimpleDateFormat("MMM");
 String month_name = month_date.format(cal.getTime());

Solution 2:

For getting month in string variable use the code below

For example the month of September:

M -> 9

MM -> 09

MMM -> Sep

MMMM -> September

String monthname=(String)android.text.format.DateFormat.format("MMMM", new Date())

Solution 3:

Use getDisplayName.

For earlier API's use String.format(Locale.US,"%tB",c);

Solution 4:

"MMMM" is definitely NOT the right solution (even if it works for many languages), use "LLLL" pattern with SimpleDateFormat

The support for 'L' as ICU-compatible extension for stand-alone month names was added to Android platform on Jun. 2010.

Even if in English there is no difference between the encoding by 'MMMM' and 'LLLL', your should think about other languages, too.

E.g. this is what you get, if you use Calendar.getDisplayName or the "MMMM" pattern for January with the Russian Locale:

января (which is correct for a complete date string: "10 января, 2014")

but in case of a stand-alone month name you would expect:

январь

The right solution is:

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

If you are interested in where all the translations come from - here is the reference to gregorian calendar translations (other calendars linked on top of the page).

Solution 5:

As simple as this

mCalendar = Calendar.getInstance();    
String month = mCalendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());

Calendar.LONG is to get the full name of the month and Calendar.SHORT gives the name in short. For eg: Calendar.LONG will return January Calendar.SHORT will return Jan