Java Cookbook
calendar
Problem:
You want to know the Japanese era for a Gregorian date.
Solution:
Use a Japanese Calendar and DateFormatSymbols from icu4j to retrieve the Japanese year and era.
Problem:
You want to find the date of the Chinese new year for a given year.
Solution:
Use icu4j's Chinese calendar to perform the calculations. You will specify a year in a Gregorian calendar and use that to set the time of the Chinese calendar. Then set the calendar tot he first month and first day.
Problem:
You want to know the Chinese zodiac sign for a Gregorian year.
Solution:
Use the icu4j Chinese calendar to get the appropriate branch. The following code illustrates and example:
Problem:
You want to create a Chinese calendar.
Solution:
Use icu4j to create a calendar instance.
Problem:
You want to know what holidays are common for a locale.
Solution:
Icu4j contains a system to help the Java developer handle international holidays. The Holiday class is simple to use, and has failry complete data.
To get an Array of all Holidays for Mexico:
//Get a ULocale
ULocale locale = new ULocale("es_MX");
//Get an array of Holidays
Holiday[] holidays = Holiday.getHolidays(locale);
Problem:
You want to use a Hebrew Calendar in your application.
Solution:
The Hebrew calendar is a very interesting lunar-solar calendar that poses some interesting challenges for the pogrammer. It uses a leap month to synch the lunar and solar components. The first day of the year can vary depending upon how it will affect jewish holidays, and the day ends at sundown.
Problem:
You want a calendar that is most familiar to your Japanese users.
Solution:
The Japanese calendar is identical to the Gregorian calendar, except the year is represented by the year of the emperor's reign. It is currently the 20th year of the Heisei emperor's reign, so the year is Heisei 20.
You can create a Japanese calendar by using icu4j .
To create a calendar object:
Problem:
You need to add or subtract time from a calendar.
Solution:
There are two ways to perform date math with a Calendar, use add(int,int) or roll(int,int). Roll will increment or decrement the specified field until the maximum or minimum value is reached, and then it will begin again. Adding 13 months to the calendar with roll will not increment the year.
Add will bubble up such that adding 13 months to a Calendar will also increment the year. To get the current date and add 3 months:
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:
Problem:
You want to create a standard calendar as used in the west.
Solution:
The Gregorian Calendar is the predominant calendar of western countries today. To create a new Gregorian Calendar call the Singleton getInstance method on the Calendar class. if you specify a Locale as an argument the Calendar returned will reflect the first day of the week etc. of the Locale.
Locale locale = new Locale("en","US");
Calendar calendar = Calendar.getInstance(locale);
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