There is a method to get the required through the java.util.Calendar
class and its instances (see Calendar.getInstance()
). Next, set the month for the getDisplayName()
and request getDisplayName()
with the correct locale (see Calendar.getAvailableLocales()
).
In the java.util.Date
class, the corresponding methods are declared deprecated, i.e. they will no longer be supported in the future, so it’s right to use Calendar right away.
Total the following will turn out:
String getNameOfMonth(int month, Locale locale) throws IllegalArgumentException { Calendar c; String s; try { c=Calendar.getInstance(); c.set(Calendar.MONTH,month); s=c.getDisplayName(Calendar.MONTH,Calendar.LONG,locale); } catch (java.lang.NullPointerException ex) { s=null; } finally { // TODO finalize c } return s; }
It should be noted that it is desirable to transfer months converted to Calendar.JANUARY
, etc., since in Java this constant is zero, i.e. the naive code System.out.println(getNameOfMonth(1,locale));
will return February.