- 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
Hebrew Calendar
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.
Thankfully icu4j provides an excellent Hebrew calendar. It is a simple manner to create a calendar, and the usage is similar to a standard Gregorian calendar.
To get an instance of a Hebrew calendar simply call the getInstance method passing in a ULocale that has a calendar type specified.
//Specify the calendar type on the ULocale
ULocale he = new ULocale("he_IL@calendar=hebrew");
//Call getInstance passing the ULocale
Calendar cal = Calendar.getInstance(he);
To get the months of the Jewish Calendar get an instance of DateFormatSymbols with a ULocale specifying the Hebrew Calendar.
//Get a Hebrew calendar for a Hebrew locale
ULocale he = new ULocale("he_IL@calendar=hebrew");
//Get a Hebrew calendar on an English locale
ULocale en = new ULocale("en@calendar=hebrew");
//Get DateFormatSymols for the locales
DateFormatSymbols dfsHe = DateFormatSymbols.getInstance(he);
DateFormatSymbols dfsEn = DateFormatSymbols.getInstance(en);
//Get arrays of months
String[] hebrewMonths = dfsHe.getMonths();
String[] englishMonths = dfsEn.getMonths();
//loop through the arrays and print out the English and Hebrew months
for(int x = 0; x < hebrewMonths.length; x++){
System.out.println(englishMonths[x] + " = " + hebrewMonths[x]);
}
The output is:
Tishri = תשרי
Heshvan = חשון
Kislev = כסלו
Tevet = טבת
Shevat = שבט
Adar I = אדר ראשון
Adar = אדר שני
Nisan = ניסן
Iyar = אייר
Sivan = סיון
Tamuz = תמוז
Av = אב
Elul =
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