Get the Names of the Months in Java

This Java tutorial shows how to get the month names for the current locale or for a specific locale. Java provides an easy mechanism for getting localized month names. To get the month names, you will use the DateFormatSymbols in the java.text package. By default, the constructor will create a DateFormatSymbols object based on the current locale.

DateFormatSymbols symbols = new DateFormatSymbols();

If the you are writting a web application, the locale on the web server is probably not the same as the user's locale. In this case you will need to specify the locale when creating the DateFormatSymbols object. Below is an example of specifying a German locale:
DateFormatSymbols symbols = new DateFormatSymbols(Locale.GERMAN);

Now that you have created a DateFormatSymbols object, you can get the month names by using the getMonths() method. This will return a String array of the month names in the specified locale.
String[] monthNames = symbols.getMonths();


1 comments:

man.without.face said...

thx!

Post a Comment

Latest Jobs