KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > util > HebrewCalendar


1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7 package com.ibm.icu.util;
8 import com.ibm.icu.util.TimeZone;
9 import com.ibm.icu.impl.CalendarCache;
10 import java.util.Date JavaDoc;
11 import java.util.Locale JavaDoc;
12
13 /**
14  * <code>HebrewCalendar</code> is a subclass of <code>Calendar</code>
15  * that that implements the traditional Hebrew calendar.
16  * This is the civil calendar in Israel and the liturgical calendar
17  * of the Jewish faith worldwide.
18  * <p>
19  * The Hebrew calendar is lunisolar and thus has a number of interesting
20  * properties that distinguish it from the Gregorian. Months start
21  * on the day of (an arithmetic approximation of) each new moon. Since the
22  * solar year (approximately 365.24 days) is not an even multiple of
23  * the lunar month (approximately 29.53 days) an extra "leap month" is
24  * inserted in 7 out of every 19 years. To make matters even more
25  * interesting, the start of a year can be delayed by up to three days
26  * in order to prevent certain holidays from falling on the Sabbath and
27  * to prevent certain illegal year lengths. Finally, the lengths of certain
28  * months can vary depending on the number of days in the year.
29  * <p>
30  * The leap month is known as "Adar 1" and is inserted between the
31  * months of Shevat and Adar in leap years. Since the leap month does
32  * not come at the end of the year, calculations involving
33  * month numbers are particularly complex. Users of this class should
34  * make sure to use the {@link #roll roll} and {@link #add add} methods
35  * rather than attempting to perform date arithmetic by manipulating
36  * the fields directly.
37  * <p>
38  * <b>Note:</b> In the traditional Hebrew calendar, days start at sunset.
39  * However, in order to keep the time fields in this class
40  * synchronized with those of the other calendars and with local clock time,
41  * we treat days and months as beginning at midnight,
42  * roughly 6 hours after the corresponding sunset.
43  * <p>
44  * If you are interested in more information on the rules behind the Hebrew
45  * calendar, see one of the following references:
46  * <ul>
47  * <li>"<a HREF="http://www.amazon.com/exec/obidos/ASIN/0521564743">Calendrical Calculations</a>",
48  * by Nachum Dershowitz & Edward Reingold, Cambridge University Press, 1997, pages 85-91.
49  *
50  * <li>Hebrew Calendar Science and Myths,
51  * <a HREF="http://www.geocities.com/Athens/1584/">
52  * http://www.geocities.com/Athens/1584/</a>
53  *
54  * <li>The Calendar FAQ,
55  * <a HREF="http://www.faqs.org/faqs/calendars/faq/">
56  * http://www.faqs.org/faqs/calendars/faq/</a>
57  * </ul>
58  *
59  * <p>
60  * This class should not be subclassed.</p>
61  * <p>
62  * HebrewCalendar usually should be instantiated using
63  * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code>
64  * with the tag <code>"@calendar=hebrew"</code>.</p>
65  *
66  * @see com.ibm.icu.util.GregorianCalendar
67  * @see com.ibm.icu.util.Calendar
68  *
69  * @author Laura Werner
70  * @author Alan Liu
71  * @stable ICU 2.8
72  */

73 public class HebrewCalendar extends Calendar {
74     // jdk1.4.2 serialver
75
private static final long serialVersionUID = -1952524560588825816L;
76
77     private static String JavaDoc copyright = "Copyright \u00a9 1997-1998 IBM Corp. All Rights Reserved.";
78
79     //-------------------------------------------------------------------------
80
// Tons o' Constants...
81
//-------------------------------------------------------------------------
82

83
84     /**
85      * Constant for Tishri, the 1st month of the Hebrew year.
86      * @stable ICU 2.8
87      */

88     public static final int TISHRI = 0;
89
90     /**
91      * Constant for Heshvan, the 2nd month of the Hebrew year.
92      * @stable ICU 2.8
93      */

94     public static final int HESHVAN = 1;
95
96     /**
97      * Constant for Kislev, the 3rd month of the Hebrew year.
98      * @stable ICU 2.8
99      */

100     public static final int KISLEV = 2;
101
102     /**
103      * Constant for Tevet, the 4th month of the Hebrew year.
104      * @stable ICU 2.8
105      */

106     public static final int TEVET = 3;
107
108     /**
109      * Constant for Shevat, the 5th month of the Hebrew year.
110      * @stable ICU 2.8
111      */

112     public static final int SHEVAT = 4;
113
114     /**
115      * Constant for Adar I, the 6th month of the Hebrew year
116      * (present in leap years only). In non-leap years, the calendar
117      * jumps from Shevat (5th month) to Adar (7th month).
118      * @stable ICU 2.8
119      */

120     public static final int ADAR_1 = 5;
121
122     /**
123      * Constant for the Adar, the 7th month of the Hebrew year.
124      * @stable ICU 2.8
125      */

126     public static final int ADAR = 6;
127
128     /**
129      * Constant for Nisan, the 8th month of the Hebrew year.
130      * @stable ICU 2.8
131      */

132     public static final int NISAN = 7;
133
134     /**
135      * Constant for Iyar, the 9th month of the Hebrew year.
136      * @stable ICU 2.8
137      */

138     public static final int IYAR = 8;
139
140     /**
141      * Constant for Sivan, the 10th month of the Hebrew year.
142      * @stable ICU 2.8
143      */

144     public static final int SIVAN = 9;
145
146     /**
147      * Constant for Tammuz, the 11th month of the Hebrew year.
148      * @stable ICU 2.8
149      */

150     public static final int TAMUZ = 10;
151
152     /**
153      * Constant for Av, the 12th month of the Hebrew year.
154      * @stable ICU 2.8
155      */

156     public static final int AV = 11;
157
158     /**
159      * Constant for Elul, the 13th month of the Hebrew year.
160      * @stable ICU 2.8
161      */

162     public static final int ELUL = 12;
163
164     /**
165      * The absolute date, in milliseconds since 1/1/1970 AD, Gregorian,
166      * of the start of the Hebrew calendar. In order to keep this calendar's
167      * time of day in sync with that of the Gregorian calendar, we use
168      * midnight, rather than sunset the day before.
169      */

170     //private static final long EPOCH_MILLIS = -180799862400000L; // 1/1/1 HY
171

172     private static final int LIMITS[][] = {
173         // Minimum Greatest Least Maximum
174
// Minimum Maximum
175
{ 0, 0, 0, 0 }, // ERA
176
{ 1, 1, 5000000, 5000000 }, // YEAR
177
{ 0, 0, 12, 12 }, // MONTH
178
{ 1, 1, 51, 56 }, // WEEK_OF_YEAR
179
{ 0, 0, 5, 6 }, // WEEK_OF_MONTH
180
{ 1, 1, 29, 30 }, // DAY_OF_MONTH
181
{ 1, 1, 353, 385 }, // DAY_OF_YEAR
182
{/* */}, // DAY_OF_WEEK
183
{ -1, -1, 4, 6 }, // DAY_OF_WEEK_IN_MONTH
184
{/* */}, // AM_PM
185
{/* */}, // HOUR
186
{/* */}, // HOUR_OF_DAY
187
{/* */}, // MINUTE
188
{/* */}, // SECOND
189
{/* */}, // MILLISECOND
190
{/* */}, // ZONE_OFFSET
191
{/* */}, // DST_OFFSET
192
{ -5000001, -5000001, 5000001, 5000001 }, // YEAR_WOY
193
{/* */}, // DOW_LOCAL
194
{ -5000000, -5000000, 5000000, 5000000 }, // EXTENDED_YEAR
195
{/* */}, // JULIAN_DAY
196
{/* */}, // MILLISECONDS_IN_DAY
197
};
198
199     /**
200      * The lengths of the Hebrew months. This is complicated, because there
201      * are three different types of years, or six if you count leap years.
202      * Due to the rules for postponing the start of the year to avoid having
203      * certain holidays fall on the sabbath, the year can end up being three
204      * different lengths, called "deficient", "normal", and "complete".
205      */

206     private static final int MONTH_LENGTH[][] = {
207         // Deficient Normal Complete
208
{ 30, 30, 30 }, //Tishri
209
{ 29, 29, 30 }, //Heshvan
210
{ 29, 30, 30 }, //Kislev
211
{ 29, 29, 29 }, //Tevet
212
{ 30, 30, 30 }, //Shevat
213
{ 30, 30, 30 }, //Adar I (leap years only)
214
{ 29, 29, 29 }, //Adar
215
{ 30, 30, 30 }, //Nisan
216
{ 29, 29, 29 }, //Iyar
217
{ 30, 30, 30 }, //Sivan
218
{ 29, 29, 29 }, //Tammuz
219
{ 30, 30, 30 }, //Av
220
{ 29, 29, 29 }, //Elul
221
};
222
223     /**
224      * The cumulative # of days to the end of each month in a non-leap year
225      * Although this can be calculated from the MONTH_LENGTH table,
226      * keeping it around separately makes some calculations a lot faster
227      */

228     private static final int MONTH_START[][] = {
229         // Deficient Normal Complete
230
{ 0, 0, 0 }, // (placeholder)
231
{ 30, 30, 30 }, // Tishri
232
{ 59, 59, 60 }, // Heshvan
233
{ 88, 89, 90 }, // Kislev
234
{ 117, 118, 119 }, // Tevet
235
{ 147, 148, 149 }, // Shevat
236
{ 147, 148, 149 }, // (Adar I placeholder)
237
{ 176, 177, 178 }, // Adar
238
{ 206, 207, 208 }, // Nisan
239
{ 235, 236, 237 }, // Iyar
240
{ 265, 266, 267 }, // Sivan
241
{ 294, 295, 296 }, // Tammuz
242
{ 324, 325, 326 }, // Av
243
{ 353, 354, 355 }, // Elul
244
};
245
246     /**
247      * The cumulative # of days to the end of each month in a leap year
248      */

249     private static final int LEAP_MONTH_START[][] = {
250         // Deficient Normal Complete
251
{ 0, 0, 0 }, // (placeholder)
252
{ 30, 30, 30 }, // Tishri
253
{ 59, 59, 60 }, // Heshvan
254
{ 88, 89, 90 }, // Kislev
255
{ 117, 118, 119 }, // Tevet
256
{ 147, 148, 149 }, // Shevat
257
{ 177, 178, 179 }, // Adar I
258
{ 206, 207, 208 }, // Adar II
259
{ 236, 237, 238 }, // Nisan
260
{ 265, 266, 267 }, // Iyar
261
{ 295, 296, 297 }, // Sivan
262
{ 324, 325, 326 }, // Tammuz
263
{ 354, 355, 356 }, // Av
264
{ 383, 384, 385 }, // Elul
265
};
266
267     //-------------------------------------------------------------------------
268
// Data Members...
269
//-------------------------------------------------------------------------
270

271     private static CalendarCache cache = new CalendarCache();
272     
273     //-------------------------------------------------------------------------
274
// Constructors...
275
//-------------------------------------------------------------------------
276

277     /**
278      * Constructs a default <code>HebrewCalendar</code> using the current time
279      * in the default time zone with the default locale.
280      * @stable ICU 2.8
281      */

282     public HebrewCalendar() {
283         this(TimeZone.getDefault(), ULocale.getDefault());
284     }
285
286     /**
287      * Constructs a <code>HebrewCalendar</code> based on the current time
288      * in the given time zone with the default locale.
289      *
290      * @param zone The time zone for the new calendar.
291      * @stable ICU 2.8
292      */

293     public HebrewCalendar(TimeZone zone) {
294         this(zone, ULocale.getDefault());
295     }
296
297     /**
298      * Constructs a <code>HebrewCalendar</code> based on the current time
299      * in the default time zone with the given locale.
300      *
301      * @param aLocale The locale for the new calendar.
302      * @stable ICU 2.8
303      */

304     public HebrewCalendar(Locale JavaDoc aLocale) {
305         this(TimeZone.getDefault(), aLocale);
306     }
307
308     /**
309      * Constructs a <code>HebrewCalendar</code> based on the current time
310      * in the default time zone with the given locale.
311      *
312      * @param locale The locale for the new calendar.
313      * @draft ICU 3.2
314      * @provisional This API might change or be removed in a future release.
315      */

316     public HebrewCalendar(ULocale locale) {
317         this(TimeZone.getDefault(), locale);
318     }
319
320     /**
321      * Constructs a <code>HebrewCalendar</code> based on the current time
322      * in the given time zone with the given locale.
323      *
324      * @param zone The time zone for the new calendar.
325      *
326      * @param aLocale The locale for the new calendar.
327      * @stable ICU 2.8
328      */

329     public HebrewCalendar(TimeZone zone, Locale JavaDoc aLocale) {
330         super(zone, aLocale);
331         setTimeInMillis(System.currentTimeMillis());
332     }
333
334     /**
335      * Constructs a <code>HebrewCalendar</code> based on the current time
336      * in the given time zone with the given locale.
337      *
338      * @param zone The time zone for the new calendar.
339      *
340      * @param locale The locale for the new calendar.
341      * @draft ICU 3.2
342      * @provisional This API might change or be removed in a future release.
343      */

344     public HebrewCalendar(TimeZone zone, ULocale locale) {
345         super(zone, locale);
346         setTimeInMillis(System.currentTimeMillis());
347     }
348
349     /**
350      * Constructs a <code>HebrewCalendar</code> with the given date set
351      * in the default time zone with the default locale.
352      *
353      * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
354      *
355      * @param month The value used to set the calendar's {@link #MONTH MONTH} time field.
356      * The value is 0-based. e.g., 0 for Tishri.
357      *
358      * @param date The value used to set the calendar's {@link #DATE DATE} time field.
359      * @stable ICU 2.8
360      */

361     public HebrewCalendar(int year, int month, int date) {
362         super(TimeZone.getDefault(), ULocale.getDefault());
363         this.set(YEAR, year);
364         this.set(MONTH, month);
365         this.set(DATE, date);
366     }
367
368     /**
369      * Constructs a <code>HebrewCalendar</code> with the given date set
370      * in the default time zone with the default locale.
371      *
372      * @param date The date to which the new calendar is set.
373      * @stable ICU 2.8
374      */

375     public HebrewCalendar(Date JavaDoc date) {
376         super(TimeZone.getDefault(), ULocale.getDefault());
377         this.setTime(date);
378     }
379
380     /**
381      * Constructs a <code>HebrewCalendar</code> with the given date
382      * and time set for the default time zone with the default locale.
383      *
384      * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
385      *
386      * @param month The value used to set the calendar's {@link #MONTH MONTH} time field.
387      * The value is 0-based. e.g., 0 for Tishri.
388      *
389      * @param date The value used to set the calendar's {@link #DATE DATE} time field.
390      *
391      * @param hour The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field.
392      *
393      * @param minute The value used to set the calendar's {@link #MINUTE MINUTE} time field.
394      *
395      * @param second The value used to set the calendar's {@link #SECOND SECOND} time field.
396      * @stable ICU 2.8
397      */

398     public HebrewCalendar(int year, int month, int date, int hour,
399                              int minute, int second)
400     {
401         super(TimeZone.getDefault(), ULocale.getDefault());
402         this.set(YEAR, year);
403         this.set(MONTH, month);
404         this.set(DATE, date);
405         this.set(HOUR_OF_DAY, hour);
406         this.set(MINUTE, minute);
407         this.set(SECOND, second);
408     }
409
410     //-------------------------------------------------------------------------
411
// Rolling and adding functions overridden from Calendar
412
//
413
// These methods call through to the default implementation in IBMCalendar
414
// for most of the fields and only handle the unusual ones themselves.
415
//-------------------------------------------------------------------------
416

417     /**
418      * Add a signed amount to a specified field, using this calendar's rules.
419      * For example, to add three days to the current date, you can call
420      * <code>add(Calendar.DATE, 3)</code>.
421      * <p>
422      * When adding to certain fields, the values of other fields may conflict and
423      * need to be changed. For example, when adding one to the {@link #MONTH MONTH} field
424      * for the date "30 Av 5758", the {@link #DAY_OF_MONTH DAY_OF_MONTH} field
425      * must be adjusted so that the result is "29 Elul 5758" rather than the invalid
426      * "30 Elul 5758".
427      * <p>
428      * This method is able to add to
429      * all fields except for {@link #ERA ERA}, {@link #DST_OFFSET DST_OFFSET},
430      * and {@link #ZONE_OFFSET ZONE_OFFSET}.
431      * <p>
432      * <b>Note:</b> You should always use {@link #roll roll} and add rather
433      * than attempting to perform arithmetic operations directly on the fields
434      * of a <tt>HebrewCalendar</tt>. Since the {@link #MONTH MONTH} field behaves
435      * discontinuously in non-leap years, simple arithmetic can give invalid results.
436      * <p>
437      * @param field the time field.
438      * @param amount the amount to add to the field.
439      *
440      * @exception IllegalArgumentException if the field is invalid or refers
441      * to a field that cannot be handled by this method.
442      * @stable ICU 2.8
443      */

444     public void add(int field, int amount)
445     {
446         switch (field) {
447         case MONTH:
448             {
449                 // We can't just do a set(MONTH, get(MONTH) + amount). The
450
// reason is ADAR_1. Suppose amount is +2 and we land in
451
// ADAR_1 -- then we have to bump to ADAR_2 aka ADAR. But
452
// if amount is -2 and we land in ADAR_1, then we have to
453
// bump the other way -- down to SHEVAT. - Alan 11/00
454
int month = get(MONTH);
455                 int year = get(YEAR);
456                 boolean acrossAdar1;
457                 if (amount > 0) {
458                     acrossAdar1 = (month < ADAR_1); // started before ADAR_1?
459
month += amount;
460                     for (;;) {
461                         if (acrossAdar1 && month>=ADAR_1 && !isLeapYear(year)) {
462                             ++month;
463                         }
464                         if (month <= ELUL) {
465                             break;
466                         }
467                         month -= ELUL+1;
468                         ++year;
469                         acrossAdar1 = true;
470                     }
471                 } else {
472                     acrossAdar1 = (month > ADAR_1); // started after ADAR_1?
473
month += amount;
474                     for (;;) {
475                         if (acrossAdar1 && month<=ADAR_1 && !isLeapYear(year)) {
476                             --month;
477                         }
478                         if (month >= 0) {
479                             break;
480                         }
481                         month += ELUL+1;
482                         --year;
483                         acrossAdar1 = true;
484                     }
485                 }
486                 set(MONTH, month);
487                 set(YEAR, year);
488                 pinField(DAY_OF_MONTH);
489                 break;
490             }
491             
492         default:
493             super.add(field, amount);
494             break;
495         }
496     }
497
498     /**
499      * Rolls (up/down) a specified amount time on the given field. For
500      * example, to roll the current date up by three days, you can call
501      * <code>roll(Calendar.DATE, 3)</code>. If the
502      * field is rolled past its maximum allowable value, it will "wrap" back
503      * to its minimum and continue rolling.
504      * For example, calling <code>roll(Calendar.DATE, 10)</code>
505      * on a Hebrew calendar set to "25 Av 5758" will result in the date "5 Av 5758".
506      * <p>
507      * When rolling certain fields, the values of other fields may conflict and
508      * need to be changed. For example, when rolling the {@link #MONTH MONTH} field
509      * upward by one for the date "30 Av 5758", the {@link #DAY_OF_MONTH DAY_OF_MONTH} field
510      * must be adjusted so that the result is "29 Elul 5758" rather than the invalid
511      * "30 Elul".
512      * <p>
513      * This method is able to roll
514      * all fields except for {@link #ERA ERA}, {@link #DST_OFFSET DST_OFFSET},
515      * and {@link #ZONE_OFFSET ZONE_OFFSET}. Subclasses may, of course, add support for
516      * additional fields in their overrides of <code>roll</code>.
517      * <p>
518      * <b>Note:</b> You should always use roll and {@link #add add} rather
519      * than attempting to perform arithmetic operations directly on the fields
520      * of a <tt>HebrewCalendar</tt>. Since the {@link #MONTH MONTH} field behaves
521      * discontinuously in non-leap years, simple arithmetic can give invalid results.
522      * <p>
523      * @param field the time field.
524      * @param amount the amount by which the field should be rolled.
525      *
526      * @exception IllegalArgumentException if the field is invalid or refers
527      * to a field that cannot be handled by this method.
528      * @stable ICU 2.8
529      */

530     public void roll(int field, int amount)
531     {
532         switch (field) {
533         case MONTH:
534             {
535                 int month = get(MONTH);
536                 int year = get(YEAR);
537                 
538                 boolean leapYear = isLeapYear(year);
539                 int yearLength = monthsInYear(year);
540                 int newMonth = month + (amount % yearLength);
541                 //
542
// If it's not a leap year and we're rolling past the missing month
543
// of ADAR_1, we need to roll an extra month to make up for it.
544
//
545
if (!leapYear) {
546                     if (amount > 0 && month < ADAR_1 && newMonth >= ADAR_1) {
547                         newMonth++;
548                     } else if (amount < 0 && month > ADAR_1 && newMonth <= ADAR_1) {
549                         newMonth--;
550                     }
551                 }
552                 set(MONTH, (newMonth + 13) % 13);
553                 pinField(DAY_OF_MONTH);
554                 return;
555             }
556         default:
557             super.roll(field, amount);
558         }
559     }
560
561     //-------------------------------------------------------------------------
562
// Support methods
563
//-------------------------------------------------------------------------
564

565     // Hebrew date calculations are performed in terms of days, hours, and
566
// "parts" (or halakim), which are 1/1080 of an hour, or 3 1/3 seconds.
567
private static final long HOUR_PARTS = 1080;
568     private static final long DAY_PARTS = 24*HOUR_PARTS;
569     
570     // An approximate value for the length of a lunar month.
571
// It is used to calculate the approximate year and month of a given
572
// absolute date.
573
static private final int MONTH_DAYS = 29;
574     static private final long MONTH_FRACT = 12*HOUR_PARTS + 793;
575     static private final long MONTH_PARTS = MONTH_DAYS*DAY_PARTS + MONTH_FRACT;
576     
577     // The time of the new moon (in parts) on 1 Tishri, year 1 (the epoch)
578
// counting from noon on the day before. BAHARAD is an abbreviation of
579
// Bet (Monday), Hey (5 hours from sunset), Resh-Daled (204).
580
static private final long BAHARAD = 11*HOUR_PARTS + 204;
581
582     /**
583      * Finds the day # of the first day in the given Hebrew year.
584      * To do this, we want to calculate the time of the Tishri 1 new moon
585      * in that year.
586      * <p>
587      * The algorithm here is similar to ones described in a number of
588      * references, including:
589      * <ul>
590      * <li>"Calendrical Calculations", by Nachum Dershowitz & Edward Reingold,
591      * Cambridge University Press, 1997, pages 85-91.
592      *
593      * <li>Hebrew Calendar Science and Myths,
594      * <a HREF="http://www.geocities.com/Athens/1584/">
595      * http://www.geocities.com/Athens/1584/</a>
596      *
597      * <li>The Calendar FAQ,
598      * <a HREF="http://www.faqs.org/faqs/calendars/faq/">
599      * http://www.faqs.org/faqs/calendars/faq/</a>
600      * </ul>
601      */

602     private static long startOfYear(int year)
603     {
604         long day = cache.get(year);
605         
606         if (day == CalendarCache.EMPTY) {
607             int months = (235 * year - 234) / 19; // # of months before year
608

609             long frac = months * MONTH_FRACT + BAHARAD; // Fractional part of day #
610
day = months * 29 + (frac / DAY_PARTS); // Whole # part of calculation
611
frac = frac % DAY_PARTS; // Time of day
612

613             int wd = (int)(day % 7); // Day of week (0 == Monday)
614

615             if (wd == 2 || wd == 4 || wd == 6) {
616                 // If the 1st is on Sun, Wed, or Fri, postpone to the next day
617
day += 1;
618                 wd = (int)(day % 7);
619             }
620             if (wd == 1 && frac > 15*HOUR_PARTS+204 && !isLeapYear(year) ) {
621                 // If the new moon falls after 3:11:20am (15h204p from the previous noon)
622
// on a Tuesday and it is not a leap year, postpone by 2 days.
623
// This prevents 356-day years.
624
day += 2;
625             }
626             else if (wd == 0 && frac > 21*HOUR_PARTS+589 && isLeapYear(year-1) ) {
627                 // If the new moon falls after 9:32:43 1/3am (21h589p from yesterday noon)
628
// on a Monday and *last* year was a leap year, postpone by 1 day.
629
// Prevents 382-day years.
630
day += 1;
631             }
632             cache.put(year, day);
633         }
634         return day;
635     }
636
637     /**
638      * Find the day of the week for a given day
639      *
640      * @param day The # of days since the start of the Hebrew calendar,
641      * 1-based (i.e. 1/1/1 AM is day 1).
642      */

643     ///CLOVER:OFF
644
private static int absoluteDayToDayOfWeek(long day)
645     {
646         // We know that 1/1/1 AM is a Monday, which makes the math easy...
647
return (int)(day % 7) + 1;
648     }
649     ///CLOVER:ON
650

651     /**
652      * Returns the the type of a given year.
653      * 0 "Deficient" year with 353 or 383 days
654      * 1 "Normal" year with 354 or 384 days
655      * 2 "Complete" year with 355 or 385 days
656      */

657     private final int yearType(int year)
658     {
659         int yearLength = handleGetYearLength(year);
660
661         if (yearLength > 380) {
662            yearLength -= 30; // Subtract length of leap month.
663
}
664
665         int type = 0;
666
667         switch (yearLength) {
668             case 353:
669                 type = 0; break;
670             case 354:
671                 type = 1; break;
672             case 355:
673                 type = 2; break;
674             default:
675                 throw new IllegalArgumentException JavaDoc("Illegal year length " + yearLength + " in year " + year);
676
677         }
678         return type;
679     }
680
681     /**
682      * Determine whether a given Hebrew year is a leap year
683      *
684      * The rule here is that if (year % 19) == 0, 3, 6, 8, 11, 14, or 17.
685      * The formula below performs the same test, believe it or not.
686      */

687     private static final boolean isLeapYear(int year) {
688         //return (year * 12 + 17) % 19 >= 12;
689
int x = (year*12 + 17) % 19;
690         return x >= ((x < 0) ? -7 : 12);
691     }
692
693     private static int monthsInYear(int year) {
694         return isLeapYear(year) ? 13 : 12;
695     }
696
697     //-------------------------------------------------------------------------
698
// Calendar framework
699
//-------------------------------------------------------------------------
700

701     /**
702      * @stable ICU 2.8
703      */

704     protected int handleGetLimit(int field, int limitType) {
705         return LIMITS[field][limitType];
706     }
707
708     /**
709      * Returns the length of the given month in the given year
710      * @stable ICU 2.8
711      */

712     protected int handleGetMonthLength(int extendedYear, int month) {
713
714         switch (month) {
715             case HESHVAN:
716             case KISLEV:
717                 // These two month lengths can vary
718
return MONTH_LENGTH[month][yearType(extendedYear)];
719                 
720             default:
721                 // The rest are a fixed length
722
return MONTH_LENGTH[month][0];
723         }
724     }
725
726     /**
727      * Returns the number of days in the given Hebrew year
728      * @stable ICU 2.8
729      */

730     protected int handleGetYearLength(int eyear) {
731         return (int)(startOfYear(eyear+1) - startOfYear(eyear));
732     }
733
734     //-------------------------------------------------------------------------
735
// Functions for converting from milliseconds to field values
736
//-------------------------------------------------------------------------
737

738     /**
739      * Subclasses may override this method to compute several fields
740      * specific to each calendar system. These are:
741      *
742      * <ul><li>ERA
743      * <li>YEAR
744      * <li>MONTH
745      * <li>DAY_OF_MONTH
746      * <li>DAY_OF_YEAR
747      * <li>EXTENDED_YEAR</ul>
748      *
749      * Subclasses can refer to the DAY_OF_WEEK and DOW_LOCAL fields,
750      * which will be set when this method is called. Subclasses can
751      * also call the getGregorianXxx() methods to obtain Gregorian
752      * calendar equivalents for the given Julian day.
753      *
754      * <p>In addition, subclasses should compute any subclass-specific
755      * fields, that is, fields from BASE_FIELD_COUNT to
756      * getFieldCount() - 1.
757      * @stable ICU 2.8
758      */

759     protected void handleComputeFields(int julianDay) {
760         long d = julianDay - 347997;
761         long m = (d * DAY_PARTS) / MONTH_PARTS; // Months (approx)
762
int year = (int)((19 * m + 234) / 235) + 1; // Years (approx)
763
long ys = startOfYear(year); // 1st day of year
764
int dayOfYear = (int)(d - ys);
765
766         // Because of the postponement rules, it's possible to guess wrong. Fix it.
767
while (dayOfYear < 1) {
768             year--;
769             ys = startOfYear(year);
770             dayOfYear = (int)(d - ys);
771         }
772
773         // Now figure out which month we're in, and the date within that month
774
int yearType = yearType(year);
775         int monthStart[][] = isLeapYear(year) ? LEAP_MONTH_START : MONTH_START;
776
777         int month = 0;
778         while (dayOfYear > monthStart[month][yearType]) {
779             month++;
780         }
781         month--;
782         int dayOfMonth = dayOfYear - monthStart[month][yearType];
783
784         internalSet(ERA, 0);
785         internalSet(YEAR, year);
786         internalSet(EXTENDED_YEAR, year);
787         internalSet(MONTH, month);
788         internalSet(DAY_OF_MONTH, dayOfMonth);
789         internalSet(DAY_OF_YEAR, dayOfYear);
790     }
791
792     //-------------------------------------------------------------------------
793
// Functions for converting from field values to milliseconds
794
//-------------------------------------------------------------------------
795

796     /**
797      * @stable ICU 2.8
798      */

799     protected int handleGetExtendedYear() {
800         int year;
801         if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
802             year = internalGet(EXTENDED_YEAR, 1); // Default to year 1
803
} else {
804             year = internalGet(YEAR, 1); // Default to year 1
805
}
806         return year;
807     }
808
809     /**
810      * Return JD of start of given month/year.
811      * @stable ICU 2.8
812      */

813     protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) {
814
815         // Resolve out-of-range months. This is necessary in order to
816
// obtain the correct year. We correct to
817
// a 12- or 13-month year (add/subtract 12 or 13, depending
818
// on the year) but since we _always_ number from 0..12, and
819
// the leap year determines whether or not month 5 (Adar 1)
820
// is present, we allow 0..12 in any given year.
821
while (month < 0) {
822             month += monthsInYear(--eyear);
823         }
824         // Careful: allow 0..12 in all years
825
while (month > 12) {
826             month -= monthsInYear(eyear++);
827         }
828
829         long day = startOfYear(eyear);
830
831         if (month != 0) {
832             if (isLeapYear(eyear)) {
833                 day += LEAP_MONTH_START[month][yearType(eyear)];
834             } else {
835                 day += MONTH_START[month][yearType(eyear)];
836             }
837         }
838
839         return (int) (day + 347997);
840     }
841
842     /**
843      * Return the current Calendar type.
844      * @return type of calendar (gregorian, etc.)
845      * @internal ICU 3.0
846      * @deprecated This API is ICU internal only.
847      */

848     public String JavaDoc getType() {
849         return "hebrew";
850     }
851
852     /*
853     private static CalendarFactory factory;
854     public static CalendarFactory factory() {
855         if (factory == null) {
856             factory = new CalendarFactory() {
857                 public Calendar create(TimeZone tz, ULocale loc) {
858                     return new HebrewCalendar(tz, loc);
859                 }
860
861                 public String factoryName() {
862                     return "Hebrew";
863                 }
864             };
865         }
866         return factory;
867     }
868     */

869 }
870
Popular Tags