1/* GENERATED SOURCE. DO NOT MODIFY. */ 2/* 3 ******************************************************************************* 4 * Copyright (C) 1996-2016, International Business Machines Corporation and 5 * others. All Rights Reserved. 6 ******************************************************************************* 7 */ 8package android.icu.util; 9import java.util.Date; 10import java.util.Locale; 11 12import android.icu.impl.CalendarCache; 13import android.icu.util.ULocale.Category; 14 15/** 16 * <code>HebrewCalendar</code> is a subclass of <code>Calendar</code> 17 * that that implements the traditional Hebrew calendar. 18 * This is the civil calendar in Israel and the liturgical calendar 19 * of the Jewish faith worldwide. 20 * <p> 21 * The Hebrew calendar is lunisolar and thus has a number of interesting 22 * properties that distinguish it from the Gregorian. Months start 23 * on the day of (an arithmetic approximation of) each new moon. Since the 24 * solar year (approximately 365.24 days) is not an even multiple of 25 * the lunar month (approximately 29.53 days) an extra "leap month" is 26 * inserted in 7 out of every 19 years. To make matters even more 27 * interesting, the start of a year can be delayed by up to three days 28 * in order to prevent certain holidays from falling on the Sabbath and 29 * to prevent certain illegal year lengths. Finally, the lengths of certain 30 * months can vary depending on the number of days in the year. 31 * <p> 32 * The leap month is known as "Adar 1" and is inserted between the 33 * months of Shevat and Adar in leap years. Since the leap month does 34 * not come at the end of the year, calculations involving 35 * month numbers are particularly complex. Users of this class should 36 * make sure to use the {@link #roll roll} and {@link #add add} methods 37 * rather than attempting to perform date arithmetic by manipulating 38 * the fields directly. 39 * <p> 40 * <b>Note:</b> In the traditional Hebrew calendar, days start at sunset. 41 * However, in order to keep the time fields in this class 42 * synchronized with those of the other calendars and with local clock time, 43 * we treat days and months as beginning at midnight, 44 * roughly 6 hours after the corresponding sunset. 45 * <p> 46 * If you are interested in more information on the rules behind the Hebrew 47 * calendar, see one of the following references: 48 * <ul> 49 * <li>"<a href="http://www.amazon.com/exec/obidos/ASIN/0521564743">Calendrical Calculations</a>", 50 * by Nachum Dershowitz & Edward Reingold, Cambridge University Press, 1997, pages 85-91. 51 * 52 * <li>Hebrew Calendar Science and Myths, 53 * <a href="http://web.archive.org/web/20090423084613/http://www.geocities.com/Athens/1584/"> 54 * http://web.archive.org/web/20090423084613/http://www.geocities.com/Athens/1584/</a> 55 * 56 * <li>The Calendar FAQ, 57 * <a href="http://www.faqs.org/faqs/calendars/faq/"> 58 * http://www.faqs.org/faqs/calendars/faq/</a> 59 * </ul> 60 * 61 * <p> 62 * This class should not be subclassed.</p> 63 * <p> 64 * HebrewCalendar usually should be instantiated using 65 * {@link android.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code> 66 * with the tag <code>"@calendar=hebrew"</code>.</p> 67 * 68 * @see android.icu.util.GregorianCalendar 69 * @see android.icu.util.Calendar 70 * 71 * @author Laura Werner 72 * @author Alan Liu 73 */ 74public class HebrewCalendar extends Calendar { 75 // jdk1.4.2 serialver 76 private static final long serialVersionUID = -1952524560588825816L; 77 78 //------------------------------------------------------------------------- 79 // Tons o' Constants... 80 //------------------------------------------------------------------------- 81 82 83 /** 84 * Constant for Tishri, the 1st month of the Hebrew year. 85 */ 86 public static final int TISHRI = 0; 87 88 /** 89 * Constant for Heshvan, the 2nd month of the Hebrew year. 90 */ 91 public static final int HESHVAN = 1; 92 93 /** 94 * Constant for Kislev, the 3rd month of the Hebrew year. 95 */ 96 public static final int KISLEV = 2; 97 98 /** 99 * Constant for Tevet, the 4th month of the Hebrew year. 100 */ 101 public static final int TEVET = 3; 102 103 /** 104 * Constant for Shevat, the 5th month of the Hebrew year. 105 */ 106 public static final int SHEVAT = 4; 107 108 /** 109 * Constant for Adar I, the 6th month of the Hebrew year 110 * (present in leap years only). In non-leap years, the calendar 111 * jumps from Shevat (5th month) to Adar (7th month). 112 */ 113 public static final int ADAR_1 = 5; 114 115 /** 116 * Constant for the Adar, the 7th month of the Hebrew year. 117 */ 118 public static final int ADAR = 6; 119 120 /** 121 * Constant for Nisan, the 8th month of the Hebrew year. 122 */ 123 public static final int NISAN = 7; 124 125 /** 126 * Constant for Iyar, the 9th month of the Hebrew year. 127 */ 128 public static final int IYAR = 8; 129 130 /** 131 * Constant for Sivan, the 10th month of the Hebrew year. 132 */ 133 public static final int SIVAN = 9; 134 135 /** 136 * Constant for Tammuz, the 11th month of the Hebrew year. 137 */ 138 public static final int TAMUZ = 10; 139 140 /** 141 * Constant for Av, the 12th month of the Hebrew year. 142 */ 143 public static final int AV = 11; 144 145 /** 146 * Constant for Elul, the 13th month of the Hebrew year. 147 */ 148 public static final int ELUL = 12; 149 150 /** 151 * The absolute date, in milliseconds since 1/1/1970 AD, Gregorian, 152 * of the start of the Hebrew calendar. In order to keep this calendar's 153 * time of day in sync with that of the Gregorian calendar, we use 154 * midnight, rather than sunset the day before. 155 */ 156 //private static final long EPOCH_MILLIS = -180799862400000L; // 1/1/1 HY 157 158 private static final int LIMITS[][] = { 159 // Minimum Greatest Least Maximum 160 // Minimum Maximum 161 { 0, 0, 0, 0 }, // ERA 162 { -5000000, -5000000, 5000000, 5000000 }, // YEAR 163 { 0, 0, 12, 12 }, // MONTH 164 { 1, 1, 51, 56 }, // WEEK_OF_YEAR 165 {/* */}, // WEEK_OF_MONTH 166 { 1, 1, 29, 30 }, // DAY_OF_MONTH 167 { 1, 1, 353, 385 }, // DAY_OF_YEAR 168 {/* */}, // DAY_OF_WEEK 169 { -1, -1, 5, 5 }, // DAY_OF_WEEK_IN_MONTH 170 {/* */}, // AM_PM 171 {/* */}, // HOUR 172 {/* */}, // HOUR_OF_DAY 173 {/* */}, // MINUTE 174 {/* */}, // SECOND 175 {/* */}, // MILLISECOND 176 {/* */}, // ZONE_OFFSET 177 {/* */}, // DST_OFFSET 178 { -5000000, -5000000, 5000000, 5000000 }, // YEAR_WOY 179 {/* */}, // DOW_LOCAL 180 { -5000000, -5000000, 5000000, 5000000 }, // EXTENDED_YEAR 181 {/* */}, // JULIAN_DAY 182 {/* */}, // MILLISECONDS_IN_DAY 183 }; 184 185 /** 186 * The lengths of the Hebrew months. This is complicated, because there 187 * are three different types of years, or six if you count leap years. 188 * Due to the rules for postponing the start of the year to avoid having 189 * certain holidays fall on the sabbath, the year can end up being three 190 * different lengths, called "deficient", "normal", and "complete". 191 */ 192 private static final int MONTH_LENGTH[][] = { 193 // Deficient Normal Complete 194 { 30, 30, 30 }, //Tishri 195 { 29, 29, 30 }, //Heshvan 196 { 29, 30, 30 }, //Kislev 197 { 29, 29, 29 }, //Tevet 198 { 30, 30, 30 }, //Shevat 199 { 30, 30, 30 }, //Adar I (leap years only) 200 { 29, 29, 29 }, //Adar 201 { 30, 30, 30 }, //Nisan 202 { 29, 29, 29 }, //Iyar 203 { 30, 30, 30 }, //Sivan 204 { 29, 29, 29 }, //Tammuz 205 { 30, 30, 30 }, //Av 206 { 29, 29, 29 }, //Elul 207 }; 208 209 /** 210 * The cumulative # of days to the end of each month in a non-leap year 211 * Although this can be calculated from the MONTH_LENGTH table, 212 * keeping it around separately makes some calculations a lot faster 213 */ 214 private static final int MONTH_START[][] = { 215 // Deficient Normal Complete 216 { 0, 0, 0 }, // (placeholder) 217 { 30, 30, 30 }, // Tishri 218 { 59, 59, 60 }, // Heshvan 219 { 88, 89, 90 }, // Kislev 220 { 117, 118, 119 }, // Tevet 221 { 147, 148, 149 }, // Shevat 222 { 147, 148, 149 }, // (Adar I placeholder) 223 { 176, 177, 178 }, // Adar 224 { 206, 207, 208 }, // Nisan 225 { 235, 236, 237 }, // Iyar 226 { 265, 266, 267 }, // Sivan 227 { 294, 295, 296 }, // Tammuz 228 { 324, 325, 326 }, // Av 229 { 353, 354, 355 }, // Elul 230 }; 231 232 /** 233 * The cumulative # of days to the end of each month in a leap year 234 */ 235 private static final int LEAP_MONTH_START[][] = { 236 // Deficient Normal Complete 237 { 0, 0, 0 }, // (placeholder) 238 { 30, 30, 30 }, // Tishri 239 { 59, 59, 60 }, // Heshvan 240 { 88, 89, 90 }, // Kislev 241 { 117, 118, 119 }, // Tevet 242 { 147, 148, 149 }, // Shevat 243 { 177, 178, 179 }, // Adar I 244 { 206, 207, 208 }, // Adar II 245 { 236, 237, 238 }, // Nisan 246 { 265, 266, 267 }, // Iyar 247 { 295, 296, 297 }, // Sivan 248 { 324, 325, 326 }, // Tammuz 249 { 354, 355, 356 }, // Av 250 { 383, 384, 385 }, // Elul 251 }; 252 253 //------------------------------------------------------------------------- 254 // Data Members... 255 //------------------------------------------------------------------------- 256 257 private static CalendarCache cache = new CalendarCache(); 258 259 //------------------------------------------------------------------------- 260 // Constructors... 261 //------------------------------------------------------------------------- 262 263 /** 264 * Constructs a default <code>HebrewCalendar</code> using the current time 265 * in the default time zone with the default <code>FORMAT</code> locale. 266 * @see Category#FORMAT 267 */ 268 public HebrewCalendar() { 269 this(TimeZone.getDefault(), ULocale.getDefault(Category.FORMAT)); 270 } 271 272 /** 273 * Constructs a <code>HebrewCalendar</code> based on the current time 274 * in the given time zone with the default <code>FORMAT</code> locale. 275 * 276 * @param zone The time zone for the new calendar. 277 * @see Category#FORMAT 278 */ 279 public HebrewCalendar(TimeZone zone) { 280 this(zone, ULocale.getDefault(Category.FORMAT)); 281 } 282 283 /** 284 * Constructs a <code>HebrewCalendar</code> based on the current time 285 * in the default time zone with the given locale. 286 * 287 * @param aLocale The locale for the new calendar. 288 */ 289 public HebrewCalendar(Locale aLocale) { 290 this(TimeZone.getDefault(), aLocale); 291 } 292 293 /** 294 * Constructs a <code>HebrewCalendar</code> based on the current time 295 * in the default time zone with the given locale. 296 * 297 * @param locale The locale for the new calendar. 298 */ 299 public HebrewCalendar(ULocale locale) { 300 this(TimeZone.getDefault(), locale); 301 } 302 303 /** 304 * Constructs a <code>HebrewCalendar</code> based on the current time 305 * in the given time zone with the given locale. 306 * 307 * @param zone The time zone for the new calendar. 308 * 309 * @param aLocale The locale for the new calendar. 310 */ 311 public HebrewCalendar(TimeZone zone, Locale aLocale) { 312 super(zone, aLocale); 313 setTimeInMillis(System.currentTimeMillis()); 314 } 315 316 /** 317 * Constructs a <code>HebrewCalendar</code> based on the current time 318 * in the given time zone with the given locale. 319 * 320 * @param zone The time zone for the new calendar. 321 * 322 * @param locale The locale for the new calendar. 323 */ 324 public HebrewCalendar(TimeZone zone, ULocale locale) { 325 super(zone, locale); 326 setTimeInMillis(System.currentTimeMillis()); 327 } 328 329 /** 330 * Constructs a <code>HebrewCalendar</code> with the given date set 331 * in the default time zone with the default <code>FORMAT</code> locale. 332 * 333 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field. 334 * 335 * @param month The value used to set the calendar's {@link #MONTH MONTH} time field. 336 * The value is 0-based. e.g., 0 for Tishri. 337 * 338 * @param date The value used to set the calendar's {@link #DATE DATE} time field. 339 * @see Category#FORMAT 340 */ 341 public HebrewCalendar(int year, int month, int date) { 342 super(TimeZone.getDefault(), ULocale.getDefault(Category.FORMAT)); 343 this.set(YEAR, year); 344 this.set(MONTH, month); 345 this.set(DATE, date); 346 } 347 348 /** 349 * Constructs a <code>HebrewCalendar</code> with the given date set 350 * in the default time zone with the default <code>FORMAT</code> locale. 351 * 352 * @param date The date to which the new calendar is set. 353 * @see Category#FORMAT 354 */ 355 public HebrewCalendar(Date date) { 356 super(TimeZone.getDefault(), ULocale.getDefault(Category.FORMAT)); 357 this.setTime(date); 358 } 359 360 /** 361 * Constructs a <code>HebrewCalendar</code> with the given date 362 * and time set for the default time zone with the default <code>FORMAT</code> locale. 363 * 364 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field. 365 * 366 * @param month The value used to set the calendar's {@link #MONTH MONTH} time field. 367 * The value is 0-based. e.g., 0 for Tishri. 368 * 369 * @param date The value used to set the calendar's {@link #DATE DATE} time field. 370 * 371 * @param hour The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field. 372 * 373 * @param minute The value used to set the calendar's {@link #MINUTE MINUTE} time field. 374 * 375 * @param second The value used to set the calendar's {@link #SECOND SECOND} time field. 376 * @see Category#FORMAT 377 */ 378 public HebrewCalendar(int year, int month, int date, int hour, 379 int minute, int second) 380 { 381 super(TimeZone.getDefault(), ULocale.getDefault(Category.FORMAT)); 382 this.set(YEAR, year); 383 this.set(MONTH, month); 384 this.set(DATE, date); 385 this.set(HOUR_OF_DAY, hour); 386 this.set(MINUTE, minute); 387 this.set(SECOND, second); 388 } 389 390 //------------------------------------------------------------------------- 391 // Rolling and adding functions overridden from Calendar 392 // 393 // These methods call through to the default implementation in IBMCalendar 394 // for most of the fields and only handle the unusual ones themselves. 395 //------------------------------------------------------------------------- 396 397 /** 398 * Add a signed amount to a specified field, using this calendar's rules. 399 * For example, to add three days to the current date, you can call 400 * <code>add(Calendar.DATE, 3)</code>. 401 * <p> 402 * When adding to certain fields, the values of other fields may conflict and 403 * need to be changed. For example, when adding one to the {@link #MONTH MONTH} field 404 * for the date "30 Av 5758", the {@link #DAY_OF_MONTH DAY_OF_MONTH} field 405 * must be adjusted so that the result is "29 Elul 5758" rather than the invalid 406 * "30 Elul 5758". 407 * <p> 408 * This method is able to add to 409 * all fields except for {@link #ERA ERA}, {@link #DST_OFFSET DST_OFFSET}, 410 * and {@link #ZONE_OFFSET ZONE_OFFSET}. 411 * <p> 412 * <b>Note:</b> You should always use {@link #roll roll} and add rather 413 * than attempting to perform arithmetic operations directly on the fields 414 * of a <tt>HebrewCalendar</tt>. Since the {@link #MONTH MONTH} field behaves 415 * discontinuously in non-leap years, simple arithmetic can give invalid results. 416 * <p> 417 * @param field the time field. 418 * @param amount the amount to add to the field. 419 * 420 * @exception IllegalArgumentException if the field is invalid or refers 421 * to a field that cannot be handled by this method. 422 */ 423 public void add(int field, int amount) 424 { 425 switch (field) { 426 case MONTH: 427 { 428 // We can't just do a set(MONTH, get(MONTH) + amount). The 429 // reason is ADAR_1. Suppose amount is +2 and we land in 430 // ADAR_1 -- then we have to bump to ADAR_2 aka ADAR. But 431 // if amount is -2 and we land in ADAR_1, then we have to 432 // bump the other way -- down to SHEVAT. - Alan 11/00 433 int month = get(MONTH); 434 int year = get(YEAR); 435 boolean acrossAdar1; 436 if (amount > 0) { 437 acrossAdar1 = (month < ADAR_1); // started before ADAR_1? 438 month += amount; 439 for (;;) { 440 if (acrossAdar1 && month>=ADAR_1 && !isLeapYear(year)) { 441 ++month; 442 } 443 if (month <= ELUL) { 444 break; 445 } 446 month -= ELUL+1; 447 ++year; 448 acrossAdar1 = true; 449 } 450 } else { 451 acrossAdar1 = (month > ADAR_1); // started after ADAR_1? 452 month += amount; 453 for (;;) { 454 if (acrossAdar1 && month<=ADAR_1 && !isLeapYear(year)) { 455 --month; 456 } 457 if (month >= 0) { 458 break; 459 } 460 month += ELUL+1; 461 --year; 462 acrossAdar1 = true; 463 } 464 } 465 set(MONTH, month); 466 set(YEAR, year); 467 pinField(DAY_OF_MONTH); 468 break; 469 } 470 471 default: 472 super.add(field, amount); 473 break; 474 } 475 } 476 477 /** 478 * Rolls (up/down) a specified amount time on the given field. For 479 * example, to roll the current date up by three days, you can call 480 * <code>roll(Calendar.DATE, 3)</code>. If the 481 * field is rolled past its maximum allowable value, it will "wrap" back 482 * to its minimum and continue rolling. 483 * For example, calling <code>roll(Calendar.DATE, 10)</code> 484 * on a Hebrew calendar set to "25 Av 5758" will result in the date "5 Av 5758". 485 * <p> 486 * When rolling certain fields, the values of other fields may conflict and 487 * need to be changed. For example, when rolling the {@link #MONTH MONTH} field 488 * upward by one for the date "30 Av 5758", the {@link #DAY_OF_MONTH DAY_OF_MONTH} field 489 * must be adjusted so that the result is "29 Elul 5758" rather than the invalid 490 * "30 Elul". 491 * <p> 492 * This method is able to roll 493 * all fields except for {@link #ERA ERA}, {@link #DST_OFFSET DST_OFFSET}, 494 * and {@link #ZONE_OFFSET ZONE_OFFSET}. Subclasses may, of course, add support for 495 * additional fields in their overrides of <code>roll</code>. 496 * <p> 497 * <b>Note:</b> You should always use roll and {@link #add add} rather 498 * than attempting to perform arithmetic operations directly on the fields 499 * of a <tt>HebrewCalendar</tt>. Since the {@link #MONTH MONTH} field behaves 500 * discontinuously in non-leap years, simple arithmetic can give invalid results. 501 * <p> 502 * @param field the time field. 503 * @param amount the amount by which the field should be rolled. 504 * 505 * @exception IllegalArgumentException if the field is invalid or refers 506 * to a field that cannot be handled by this method. 507 */ 508 public void roll(int field, int amount) 509 { 510 switch (field) { 511 case MONTH: 512 { 513 int month = get(MONTH); 514 int year = get(YEAR); 515 516 boolean leapYear = isLeapYear(year); 517 int yearLength = monthsInYear(year); 518 int newMonth = month + (amount % yearLength); 519 // 520 // If it's not a leap year and we're rolling past the missing month 521 // of ADAR_1, we need to roll an extra month to make up for it. 522 // 523 if (!leapYear) { 524 if (amount > 0 && month < ADAR_1 && newMonth >= ADAR_1) { 525 newMonth++; 526 } else if (amount < 0 && month > ADAR_1 && newMonth <= ADAR_1) { 527 newMonth--; 528 } 529 } 530 set(MONTH, (newMonth + 13) % 13); 531 pinField(DAY_OF_MONTH); 532 return; 533 } 534 default: 535 super.roll(field, amount); 536 } 537 } 538 539 //------------------------------------------------------------------------- 540 // Support methods 541 //------------------------------------------------------------------------- 542 543 // Hebrew date calculations are performed in terms of days, hours, and 544 // "parts" (or halakim), which are 1/1080 of an hour, or 3 1/3 seconds. 545 private static final long HOUR_PARTS = 1080; 546 private static final long DAY_PARTS = 24*HOUR_PARTS; 547 548 // An approximate value for the length of a lunar month. 549 // It is used to calculate the approximate year and month of a given 550 // absolute date. 551 static private final int MONTH_DAYS = 29; 552 static private final long MONTH_FRACT = 12*HOUR_PARTS + 793; 553 static private final long MONTH_PARTS = MONTH_DAYS*DAY_PARTS + MONTH_FRACT; 554 555 // The time of the new moon (in parts) on 1 Tishri, year 1 (the epoch) 556 // counting from noon on the day before. BAHARAD is an abbreviation of 557 // Bet (Monday), Hey (5 hours from sunset), Resh-Daled (204). 558 static private final long BAHARAD = 11*HOUR_PARTS + 204; 559 560 /** 561 * Finds the day # of the first day in the given Hebrew year. 562 * To do this, we want to calculate the time of the Tishri 1 new moon 563 * in that year. 564 * <p> 565 * The algorithm here is similar to ones described in a number of 566 * references, including: 567 * <ul> 568 * <li>"Calendrical Calculations", by Nachum Dershowitz & Edward Reingold, 569 * Cambridge University Press, 1997, pages 85-91. 570 * 571 * <li>Hebrew Calendar Science and Myths, 572 * <a href="http://www.geocities.com/Athens/1584/"> 573 * http://www.geocities.com/Athens/1584/</a> 574 * 575 * <li>The Calendar FAQ, 576 * <a href="http://www.faqs.org/faqs/calendars/faq/"> 577 * http://www.faqs.org/faqs/calendars/faq/</a> 578 * </ul> 579 */ 580 private static long startOfYear(int year) 581 { 582 long day = cache.get(year); 583 584 if (day == CalendarCache.EMPTY) { 585 int months = (235 * year - 234) / 19; // # of months before year 586 587 long frac = months * MONTH_FRACT + BAHARAD; // Fractional part of day # 588 day = months * 29 + (frac / DAY_PARTS); // Whole # part of calculation 589 frac = frac % DAY_PARTS; // Time of day 590 591 int wd = (int)(day % 7); // Day of week (0 == Monday) 592 593 if (wd == 2 || wd == 4 || wd == 6) { 594 // If the 1st is on Sun, Wed, or Fri, postpone to the next day 595 day += 1; 596 wd = (int)(day % 7); 597 } 598 if (wd == 1 && frac > 15*HOUR_PARTS+204 && !isLeapYear(year) ) { 599 // If the new moon falls after 3:11:20am (15h204p from the previous noon) 600 // on a Tuesday and it is not a leap year, postpone by 2 days. 601 // This prevents 356-day years. 602 day += 2; 603 } 604 else if (wd == 0 && frac > 21*HOUR_PARTS+589 && isLeapYear(year-1) ) { 605 // If the new moon falls after 9:32:43 1/3am (21h589p from yesterday noon) 606 // on a Monday and *last* year was a leap year, postpone by 1 day. 607 // Prevents 382-day years. 608 day += 1; 609 } 610 cache.put(year, day); 611 } 612 return day; 613 } 614 615 /* 616 * Find the day of the week for a given day 617 * 618 * @param day The # of days since the start of the Hebrew calendar, 619 * 1-based (i.e. 1/1/1 AM is day 1). 620 */ 621 /*private static int absoluteDayToDayOfWeek(long day) 622 { 623 // We know that 1/1/1 AM is a Monday, which makes the math easy... 624 return (int)(day % 7) + 1; 625 }*/ 626 627 /** 628 * Returns the the type of a given year. 629 * 0 "Deficient" year with 353 or 383 days 630 * 1 "Normal" year with 354 or 384 days 631 * 2 "Complete" year with 355 or 385 days 632 */ 633 private final int yearType(int year) 634 { 635 int yearLength = handleGetYearLength(year); 636 637 if (yearLength > 380) { 638 yearLength -= 30; // Subtract length of leap month. 639 } 640 641 int type = 0; 642 643 switch (yearLength) { 644 case 353: 645 type = 0; break; 646 case 354: 647 type = 1; break; 648 case 355: 649 type = 2; break; 650 default: 651 throw new IllegalArgumentException("Illegal year length " + yearLength + " in year " + year); 652 653 } 654 return type; 655 } 656 657 /** 658 * Determine whether a given Hebrew year is a leap year 659 * 660 * The rule here is that if (year % 19) == 0, 3, 6, 8, 11, 14, or 17. 661 * The formula below performs the same test, believe it or not. 662 * @deprecated This API is ICU internal only. 663 * @hide original deprecated declaration 664 * @hide draft / provisional / internal are hidden on Android 665 */ 666 @Deprecated 667 public static boolean isLeapYear(int year) { 668 //return (year * 12 + 17) % 19 >= 12; 669 int x = (year*12 + 17) % 19; 670 return x >= ((x < 0) ? -7 : 12); 671 } 672 673 private static int monthsInYear(int year) { 674 return isLeapYear(year) ? 13 : 12; 675 } 676 677 //------------------------------------------------------------------------- 678 // Calendar framework 679 //------------------------------------------------------------------------- 680 681 /** 682 */ 683 protected int handleGetLimit(int field, int limitType) { 684 return LIMITS[field][limitType]; 685 } 686 687 /** 688 * Returns the length of the given month in the given year 689 */ 690 protected int handleGetMonthLength(int extendedYear, int month) { 691 // Resolve out-of-range months. This is necessary in order to 692 // obtain the correct year. We correct to 693 // a 12- or 13-month year (add/subtract 12 or 13, depending 694 // on the year) but since we _always_ number from 0..12, and 695 // the leap year determines whether or not month 5 (Adar 1) 696 // is present, we allow 0..12 in any given year. 697 while (month < 0) { 698 month += monthsInYear(--extendedYear); 699 } 700 // Careful: allow 0..12 in all years 701 while (month > 12) { 702 month -= monthsInYear(extendedYear++); 703 } 704 705 switch (month) { 706 case HESHVAN: 707 case KISLEV: 708 // These two month lengths can vary 709 return MONTH_LENGTH[month][yearType(extendedYear)]; 710 711 default: 712 // The rest are a fixed length 713 return MONTH_LENGTH[month][0]; 714 } 715 } 716 717 /** 718 * Returns the number of days in the given Hebrew year 719 */ 720 protected int handleGetYearLength(int eyear) { 721 return (int)(startOfYear(eyear+1) - startOfYear(eyear)); 722 } 723 724 /** 725 * {@inheritDoc} 726 * <p> 727 * Overrides {@link Calendar#validateField(int)} to provide 728 * special handling for month validation for Hebrew calendar. 729 * @deprecated This API is ICU internal only. 730 * @hide original deprecated declaration 731 * @hide draft / provisional / internal are hidden on Android 732 */ 733 @Deprecated 734 protected void validateField(int field) { 735 if (field == MONTH && !isLeapYear(handleGetExtendedYear()) && internalGet(MONTH) == ADAR_1) { 736 throw new IllegalArgumentException("MONTH cannot be ADAR_1(5) except leap years"); 737 } 738 739 super.validateField(field); 740 } 741 742 //------------------------------------------------------------------------- 743 // Functions for converting from milliseconds to field values 744 //------------------------------------------------------------------------- 745 746 /** 747 * Subclasses may override this method to compute several fields 748 * specific to each calendar system. These are: 749 * 750 * <ul><li>ERA 751 * <li>YEAR 752 * <li>MONTH 753 * <li>DAY_OF_MONTH 754 * <li>DAY_OF_YEAR 755 * <li>EXTENDED_YEAR</ul> 756 * 757 * Subclasses can refer to the DAY_OF_WEEK and DOW_LOCAL fields, 758 * which will be set when this method is called. Subclasses can 759 * also call the getGregorianXxx() methods to obtain Gregorian 760 * calendar equivalents for the given Julian day. 761 * 762 * <p>In addition, subclasses should compute any subclass-specific 763 * fields, that is, fields from BASE_FIELD_COUNT to 764 * getFieldCount() - 1. 765 */ 766 protected void handleComputeFields(int julianDay) { 767 long d = julianDay - 347997; 768 long m = (d * DAY_PARTS) / MONTH_PARTS; // Months (approx) 769 int year = (int)((19 * m + 234) / 235) + 1; // Years (approx) 770 long ys = startOfYear(year); // 1st day of year 771 int dayOfYear = (int)(d - ys); 772 773 // Because of the postponement rules, it's possible to guess wrong. Fix it. 774 while (dayOfYear < 1) { 775 year--; 776 ys = startOfYear(year); 777 dayOfYear = (int)(d - ys); 778 } 779 780 // Now figure out which month we're in, and the date within that month 781 int yearType = yearType(year); 782 int monthStart[][] = isLeapYear(year) ? LEAP_MONTH_START : MONTH_START; 783 784 int month = 0; 785 while (dayOfYear > monthStart[month][yearType]) { 786 month++; 787 } 788 month--; 789 int dayOfMonth = dayOfYear - monthStart[month][yearType]; 790 791 internalSet(ERA, 0); 792 internalSet(YEAR, year); 793 internalSet(EXTENDED_YEAR, year); 794 internalSet(MONTH, month); 795 internalSet(DAY_OF_MONTH, dayOfMonth); 796 internalSet(DAY_OF_YEAR, dayOfYear); 797 } 798 799 //------------------------------------------------------------------------- 800 // Functions for converting from field values to milliseconds 801 //------------------------------------------------------------------------- 802 803 /** 804 */ 805 protected int handleGetExtendedYear() { 806 int year; 807 if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) { 808 year = internalGet(EXTENDED_YEAR, 1); // Default to year 1 809 } else { 810 year = internalGet(YEAR, 1); // Default to year 1 811 } 812 return year; 813 } 814 815 /** 816 * Return JD of start of given month/year. 817 */ 818 protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) { 819 820 // Resolve out-of-range months. This is necessary in order to 821 // obtain the correct year. We correct to 822 // a 12- or 13-month year (add/subtract 12 or 13, depending 823 // on the year) but since we _always_ number from 0..12, and 824 // the leap year determines whether or not month 5 (Adar 1) 825 // is present, we allow 0..12 in any given year. 826 while (month < 0) { 827 month += monthsInYear(--eyear); 828 } 829 // Careful: allow 0..12 in all years 830 while (month > 12) { 831 month -= monthsInYear(eyear++); 832 } 833 834 long day = startOfYear(eyear); 835 836 if (month != 0) { 837 if (isLeapYear(eyear)) { 838 day += LEAP_MONTH_START[month][yearType(eyear)]; 839 } else { 840 day += MONTH_START[month][yearType(eyear)]; 841 } 842 } 843 844 return (int) (day + 347997); 845 } 846 847 /** 848 * {@inheritDoc} 849 */ 850 public String getType() { 851 return "hebrew"; 852 } 853 854 /* 855 private static CalendarFactory factory; 856 public static CalendarFactory factory() { 857 if (factory == null) { 858 factory = new CalendarFactory() { 859 public Calendar create(TimeZone tz, ULocale loc) { 860 return new HebrewCalendar(tz, loc); 861 } 862 863 public String factoryName() { 864 return "Hebrew"; 865 } 866 }; 867 } 868 return factory; 869 } 870 */ 871} 872