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 =