Get the name of the current month

Problem:

You want to retrieve a localized display name of the current month.

Solution:

To get the name of a month you first retrieve an instance of Calendar for the desired locale. You use calendar.get(int) passing in the constant Calendar.MONTH to get the current month of the calendar. You then use that to pull the correct value out of the array of month names returned by getMonths() on an instance of DateFormatSymbols.

To get the display name for the current month:

//Get an instance of Calendar for a Japanese locale

Calendar calendar = Calendar.getInstance(Locale.JAPANESE);

//Get a new DateFormatSymbols object for the Japanese locale

DateFormatSymbols dfs = new DateFormatSymbols(Locale.JAPANESE);

//Get an Array of months

String[] months = dfs.getMonths();

System.out.println(months[calendar.get(Calendar.MONTH)]);


 

To loop through all of the months for a locale:

//Get a new DateFormatSymbols object for the Japanese locale

DateFormatSymbols dfs = new DateFormatSymbols(Locale.FRENCH);

//Get the months String[] months = dfs.getMonths();

//Months are zero based, so loop from 0 to 11

for(int x = 0; x < 12; x++){

  System.out.println(months[x]);

}