How can I get Month Name from Calendar?

JavaCalendar

Java Problem Overview


Is there a oneliner to get the name of the month when we know:

int monthNumber  = calendar.get(Calendar.MONTH)

Or what is the easiest way?

Java Solutions


Solution 1 - Java

You can achieve it using SimpleDateFormat, which is meant to format date and times:

Calendar cal = Calendar.getInstance();
System.out.println(new SimpleDateFormat("MMM").format(cal.getTime()));

Solution 2 - Java

String getMonthForInt(int num) {
	String month = "wrong";
	DateFormatSymbols dfs = new DateFormatSymbols();
	String[] months = dfs.getMonths();
	if (num >= 0 && num <= 11) {
		month = months[num];
	}
	return month;
}

Solution 3 - Java

As simple as this

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

Solution 4 - Java

This is the solution I came up with for a class project:

public static String theMonth(int month){
    String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    return monthNames[month];
}

The number you pass in comes from a Calendar.MONTH call.

Solution 5 - Java

If you have multi-language interface, you can use getDisplayName to display the name of month with control of displaying language.

Here is an example of displaying the month name in English, French, Arabic and Arabic in specific country like "Syria":

Calendar c = Calendar.getInstance();
System.out.println(c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH ) );
System.out.println(c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.FRANCE ) );
System.out.println(c.getDisplayName(Calendar.MONTH, Calendar.LONG, new Locale("ar") ) );
System.out.println(c.getDisplayName(Calendar.MONTH, Calendar.LONG, new Locale("ar", "SY") ) );
System.out.println(c.getTime().toString());

The result is:

January
janvier
يناير
كانون الثاني
Sat Jan 17 19:31:30 EET 2015

Solution 6 - Java

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

For some languages (e.g. Russian) this is the only correct way to get the stand-alone month names.

This is what you get, if you use getDisplayName from the Calendar or DateFormatSymbols for January:

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

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

январь

Solution 7 - Java

Joda-Time

How about using Joda-Time. It's a far better date-time API to work with (And January means january here. It's not like Calendar, which uses 0-based index for months).

You can use AbstractDateTime#toString( pattern ) method to format the date in specified format:

DateTime date = DateTime.now();
String month = date.toString("MMM");

Month Name From Number

If you want month name for a particular month number, you can do it like this:

int month = 3;
String monthName = DateTime.now().withMonthOfYear(month).toString("MMM");

Localize

The above approach uses your JVM’s current default Locale for the language of the month name. You want to specify a Locale object instead.

String month = date.toString( "MMM", Locale.CANADA_FRENCH );


Solution 8 - Java

Month::getDisplayName

Since Java 8, use the Month enum. The getDisplayName method automatically localizes the name of the month.

Pass:

  • A TextStyle to determine how long or how abbreviated.
  • A Locale to specify the human language used in translation, and the cultural norms used for abbreviation, punctuation, etc.

Example:

public static String getMonthStandaloneName(Month month) {
    return month.getDisplayName(
        TextStyle.FULL_STANDALONE, 
        Locale.getDefault()
    );
}

Solution 9 - Java

It might be an old question, but as a one liner to get the name of the month when we know the indices, I used

String month = new DateFormatSymbols().getMonths()[monthNumber - 1];

or for short names

String month = new DateFormatSymbols().getShortMonths()[monthNumber - 1];

Please be aware that your monthNumber starts counting from 1 while any of the methods above returns an array so you need to start counting from 0.

Solution 10 - Java

This code has language support. I had used them in Android App.

String[] mons = new DateFormatSymbols().getShortMonths();//Jan,Feb,Mar,... 

String[] months = new DateFormatSymbols().getMonths();//January,Februaty,March,...

Solution 11 - Java

I found this much easier(https://docs.oracle.com/javase/tutorial/datetime/iso/enum.html)

private void getCalendarMonth(Date date) {		
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	Month month = Month.of(calendar.get(Calendar.MONTH));		
	Locale locale = Locale.getDefault();
	System.out.println(month.getDisplayName(TextStyle.FULL, locale));
    System.out.println(month.getDisplayName(TextStyle.NARROW, locale));
    System.out.println(month.getDisplayName(TextStyle.SHORT, locale));
}

Solution 12 - Java

You can get it one line like this:

String monthName = new DataFormatSymbols.getMonths()[cal.get(Calendar.MONTH)]

Solution 13 - Java

This works for me:

String getMonthName(int monthNumber) {
    String[] months = new DateFormatSymbols().getMonths();
    int n = monthNumber-1;
    return (n >= 0 && n <= 11) ? months[n] : "wrong number";
}

To returns "September" with one line:

String month = getMonthName(9);

Solution 14 - Java

One way:

We have Month API in Java (java.time.Month). We can get by using Month.of(month);

Here, the Month are indexed as numbers so either you can provide by Month.JANUARY or provide an index in the above API such as 1, 2, 3, 4.

Second way:

ZonedDateTime.now().getMonth();

This is available in java.time.ZonedDateTime.

Solution 15 - Java

Calender cal = Calendar.getInstance(Locale.ENGLISH)
String[] mons = new DateFormatSymbols(Locale.ENGLISH).getShortMonths();
int m = cal.get(Calendar.MONTH);
String mName = mons[m];

Solution 16 - Java

Easiest Way

import java.text.DateFormatSymbols;

int month = 3; // March
System.out.println(new DateFormatSymbols().getMonths()[month-1]);

Solution 17 - Java

It returns English name of the month. 04 returns APRIL and so on.

String englishMonth (int month){
        return Month.of(month);
    }

Solution 18 - Java

I created a Kotlin extension based on responses in this topic and using the DateFormatSymbols answers you get a localized response.

fun Date.toCalendar(): Calendar {
    val calendar = Calendar.getInstance()
    calendar.time = this
    return calendar
}


fun Date.getMonthName(): String {
    val month = toCalendar()[Calendar.MONTH]
    val dfs = DateFormatSymbols()
    val months = dfs.months
    return months[month]
}

Solution 19 - Java

import java.text.SimpleDateFormat;
import java.util.Calendar;


Calendar cal = Calendar.getInstance();
String currentdate=new SimpleDateFormat("dd-MMM").format(cal.getTime());

Solution 20 - Java

DateFormat date =  new SimpleDateFormat("dd/MMM/yyyy");
Date date1 = new Date();
System.out.println(date.format(date1));

Solution 21 - Java

For full name of month:

val calendar = Calendar.getInstance()
calendar.timeInMillis = date
return calendar.getDisplayName(Calendar.MONTH, Calendar.Long, Locale.ENGLISH)!!.toString()

And for short name of month:

val calendar = Calendar.getInstance()
calendar.timeInMillis = date
return calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.ENGLISH)!!.toString()

Solution 22 - Java

from the SimpleDateFormat java doc:

*         <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code>
 *         <td><code>02001.July.04 AD 12:08 PM</code>
 *         <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
 *         <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionogzdView Question on Stackoverflow
Solution 1 - JavaPermGenErrorView Answer on Stackoverflow
Solution 2 - JavasubodhView Answer on Stackoverflow
Solution 3 - JavasandaloneView Answer on Stackoverflow
Solution 4 - JavaTerry ChernView Answer on Stackoverflow
Solution 5 - JavaEng. Samer TView Answer on Stackoverflow
Solution 6 - JavaartkoenigView Answer on Stackoverflow
Solution 7 - JavaRohit JainView Answer on Stackoverflow
Solution 8 - JavaNikolay FedorovskikhView Answer on Stackoverflow
Solution 9 - JavageorgerView Answer on Stackoverflow
Solution 10 - JavaSamet ÖZTOPRAKView Answer on Stackoverflow
Solution 11 - JavaJibinNajeebView Answer on Stackoverflow
Solution 12 - JavaWhirvisView Answer on Stackoverflow
Solution 13 - JavajhpgView Answer on Stackoverflow
Solution 14 - Javakushal BaldevView Answer on Stackoverflow
Solution 15 - JavaMilad jalaliView Answer on Stackoverflow
Solution 16 - JavaNaveen RoyView Answer on Stackoverflow
Solution 17 - JavamxhcView Answer on Stackoverflow
Solution 18 - JavaFelipe CastilhosView Answer on Stackoverflow
Solution 19 - Javamahadev goudaView Answer on Stackoverflow
Solution 20 - JavaAmit MishraView Answer on Stackoverflow
Solution 21 - JavaJunaid BashirView Answer on Stackoverflow
Solution 22 - JavaRafael SanchesView Answer on Stackoverflow