- Java Internationalization Cookbook
- Locales
- Dates and Times
- Calendars
- Gregorian Calendar
- Hebrew Calendar
- Japanese Calendar
- Chinese Calendar
- Find the Chinese zodiac for a Gregorian year
- Get the name of the current month
- Get the first day of the week
- Add time to Calendar
- Get an array of Holidays
- Find the date for the Chinese New Year
- Get all the Era names for the Japanese Calendar
- Get the Japanese era for a Gregorian date
- Formating dates and times
- Calendars
- Numerical Systems
- Misc
- Resource Bundles
- Unicode, Transliteration, and Charactersets
Java Cookbook
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]);
}
If you are testing any of these recipes in Eclipse and the characters are not displaying correctly in your console visit http://i18ncookbook.com/eclipse_settings.
This site is ad supported. I hope you find something among our sponsors worth clicking. ;)
i18n search
Custom Search