The Calendar
class is not the best class to use when it comes obtaining the localized month name in one statement.
The following is an example of obtaining the month name of a desired month specified by a int
value (where January is 1), using only the Calendar
class:
// Month as a number.
int month = 1;
// Sets the Calendar instance to the desired month.
// The "-1" takes into account that Calendar counts months
// beginning from 0.
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, month - 1);
// Returns a String of the month name in the current locale.
c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
The above code will return the month name in the system locale.
If another locale is required, one can specify another Locale
by replacing the Locale.getDefault()
with a specific locale such as Locale.US
.