1 /* 2 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util; 27 28 import java.io.IOException; 29 import java.io.ObjectInputStream; 30 import sun.util.locale.provider.CalendarDataUtility; 31 import sun.util.calendar.BaseCalendar; 32 import sun.util.calendar.CalendarDate; 33 import sun.util.calendar.CalendarSystem; 34 import sun.util.calendar.CalendarUtils; 35 import sun.util.calendar.Era; 36 import sun.util.calendar.Gregorian; 37 import sun.util.calendar.LocalGregorianCalendar; 38 import sun.util.calendar.ZoneInfo; 39 40 /** 41 * {@code JapaneseImperialCalendar} implements a Japanese 42 * calendar system in which the imperial era-based year numbering is 43 * supported from the Meiji era. The following are the eras supported 44 * by this calendar system. 45 * <pre>{@code 46 * ERA value Era name Since (in Gregorian) 47 * ------------------------------------------------------ 48 * 0 N/A N/A 49 * 1 Meiji 1868-01-01T00:00:00 local time 50 * 2 Taisho 1912-07-30T00:00:00 local time 51 * 3 Showa 1926-12-25T00:00:00 local time 52 * 4 Heisei 1989-01-08T00:00:00 local time 53 * 5 Reiwa 2019-05-01T00:00:00 local time 54 * ------------------------------------------------------ 55 * }</pre> 56 * 57 * <p><code>ERA</code> value 0 specifies the years before Meiji and 58 * the Gregorian year values are used. Unlike {@link 59 * GregorianCalendar}, the Julian to Gregorian transition is not 60 * supported because it doesn't make any sense to the Japanese 61 * calendar systems used before Meiji. To represent the years before 62 * Gregorian year 1, 0 and negative values are used. The Japanese 63 * Imperial rescripts and government decrees don't specify how to deal 64 * with time differences for applying the era transitions. This 65 * calendar implementation assumes local time for all transitions. 66 * 67 * @author Masayoshi Okutsu 68 * @since 1.6 69 */ 70 class JapaneseImperialCalendar extends Calendar { 71 /* 72 * Implementation Notes 73 * 74 * This implementation uses 75 * sun.util.calendar.LocalGregorianCalendar to perform most of the 76 * calendar calculations. LocalGregorianCalendar is configurable 77 * and reads <JRE_HOME>/lib/calendars.properties at the start-up. 78 */ 79 80 /** 81 * The ERA constant designating the era before Meiji. 82 */ 83 public static final int BEFORE_MEIJI = 0; 84 85 /** 86 * The ERA constant designating the Meiji era. 87 */ 88 public static final int MEIJI = 1; 89 90 /** 91 * The ERA constant designating the Taisho era. 92 */ 93 public static final int TAISHO = 2; 94 95 /** 96 * The ERA constant designating the Showa era. 97 */ 98 public static final int SHOWA = 3; 99 100 /** 101 * The ERA constant designating the Heisei era. 102 */ 103 public static final int HEISEI = 4; 104 105 /** 106 * The ERA constant designating the Reiwa era. 107 */ 108 private static final int REIWA = 5; 109 110 private static final int EPOCH_OFFSET = 719163; // Fixed date of January 1, 1970 (Gregorian) 111 112 // Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit 113 // into ints, they must be longs in order to prevent arithmetic overflow 114 // when performing (bug 4173516). 115 private static final int ONE_SECOND = 1000; 116 private static final int ONE_MINUTE = 60*ONE_SECOND; 117 private static final int ONE_HOUR = 60*ONE_MINUTE; 118 private static final long ONE_DAY = 24*ONE_HOUR; 119 private static final long ONE_WEEK = 7*ONE_DAY; 120 121 // Reference to the sun.util.calendar.LocalGregorianCalendar instance (singleton). 122 private static final LocalGregorianCalendar jcal 123 = (LocalGregorianCalendar) CalendarSystem.forName("japanese"); 124 125 // Gregorian calendar instance. This is required because era 126 // transition dates are given in Gregorian dates. 127 private static final Gregorian gcal = CalendarSystem.getGregorianCalendar(); 128 129 // The Era instance representing "before Meiji". 130 private static final Era BEFORE_MEIJI_ERA = new Era("BeforeMeiji", "BM", Long.MIN_VALUE, false); 131 132 // Imperial eras. The sun.util.calendar.LocalGregorianCalendar 133 // doesn't have an Era representing before Meiji, which is 134 // inconvenient for a Calendar. So, era[0] is a reference to 135 // BEFORE_MEIJI_ERA. 136 private static final Era[] eras; 137 138 // Fixed date of the first date of each era. 139 private static final long[] sinceFixedDates; 140 141 // The current era 142 private static final int currentEra; 143 144 /* 145 * <pre> 146 * Greatest Least 147 * Field name Minimum Minimum Maximum Maximum 148 * ---------- ------- ------- ------- ------- 149 * ERA 0 0 1 1 150 * YEAR -292275055 1 ? ? 151 * MONTH 0 0 11 11 152 * WEEK_OF_YEAR 1 1 52* 53 153 * WEEK_OF_MONTH 0 0 4* 6 154 * DAY_OF_MONTH 1 1 28* 31 155 * DAY_OF_YEAR 1 1 365* 366 156 * DAY_OF_WEEK 1 1 7 7 157 * DAY_OF_WEEK_IN_MONTH -1 -1 4* 6 158 * AM_PM 0 0 1 1 159 * HOUR 0 0 11 11 160 * HOUR_OF_DAY 0 0 23 23 161 * MINUTE 0 0 59 59 162 * SECOND 0 0 59 59 163 * MILLISECOND 0 0 999 999 164 * ZONE_OFFSET -13:00 -13:00 14:00 14:00 165 * DST_OFFSET 0:00 0:00 0:20 2:00 166 * </pre> 167 * *: depends on eras 168 */ 169 static final int MIN_VALUES[] = { 170 0, // ERA 171 -292275055, // YEAR 172 JANUARY, // MONTH 173 1, // WEEK_OF_YEAR 174 0, // WEEK_OF_MONTH 175 1, // DAY_OF_MONTH 176 1, // DAY_OF_YEAR 177 SUNDAY, // DAY_OF_WEEK 178 1, // DAY_OF_WEEK_IN_MONTH 179 AM, // AM_PM 180 0, // HOUR 181 0, // HOUR_OF_DAY 182 0, // MINUTE 183 0, // SECOND 184 0, // MILLISECOND 185 -13*ONE_HOUR, // ZONE_OFFSET (UNIX compatibility) 186 0 // DST_OFFSET 187 }; 188 static final int LEAST_MAX_VALUES[] = { 189 0, // ERA (initialized later) 190 0, // YEAR (initialized later) 191 JANUARY, // MONTH (Showa 64 ended in January.) 192 0, // WEEK_OF_YEAR (Showa 1 has only 6 days which could be 0 weeks.) 193 4, // WEEK_OF_MONTH 194 28, // DAY_OF_MONTH 195 0, // DAY_OF_YEAR (initialized later) 196 SATURDAY, // DAY_OF_WEEK 197 4, // DAY_OF_WEEK_IN 198 PM, // AM_PM 199 11, // HOUR 200 23, // HOUR_OF_DAY 201 59, // MINUTE 202 59, // SECOND 203 999, // MILLISECOND 204 14*ONE_HOUR, // ZONE_OFFSET 205 20*ONE_MINUTE // DST_OFFSET (historical least maximum) 206 }; 207 static final int MAX_VALUES[] = { 208 0, // ERA 209 292278994, // YEAR 210 DECEMBER, // MONTH 211 53, // WEEK_OF_YEAR 212 6, // WEEK_OF_MONTH 213 31, // DAY_OF_MONTH 214 366, // DAY_OF_YEAR 215 SATURDAY, // DAY_OF_WEEK 216 6, // DAY_OF_WEEK_IN 217 PM, // AM_PM 218 11, // HOUR 219 23, // HOUR_OF_DAY 220 59, // MINUTE 221 59, // SECOND 222 999, // MILLISECOND 223 14*ONE_HOUR, // ZONE_OFFSET 224 2*ONE_HOUR // DST_OFFSET (double summer time) 225 }; 226 227 // Proclaim serialization compatibility with JDK 1.6 228 private static final long serialVersionUID = -3364572813905467929L; 229 230 static { 231 Era[] es = jcal.getEras(); 232 int length = es.length + 1; 233 eras = new Era[length]; 234 sinceFixedDates = new long[length]; 235 236 // eras[BEFORE_MEIJI] and sinceFixedDate[BEFORE_MEIJI] are the 237 // same as Gregorian. 238 int index = BEFORE_MEIJI; 239 int current = index; 240 sinceFixedDates[index] = gcal.getFixedDate(BEFORE_MEIJI_ERA.getSinceDate()); 241 eras[index++] = BEFORE_MEIJI_ERA; 242 for (Era e : es) { 243 if(e.getSince(TimeZone.NO_TIMEZONE) < System.currentTimeMillis()) { 244 current = index; 245 } 246 CalendarDate d = e.getSinceDate(); 247 sinceFixedDates[index] = gcal.getFixedDate(d); 248 eras[index++] = e; 249 } 250 currentEra = current; 251 252 LEAST_MAX_VALUES[ERA] = MAX_VALUES[ERA] = eras.length - 1; 253 254 // Calculate the least maximum year and least day of Year 255 // values. The following code assumes that there's at most one 256 // era transition in a Gregorian year. 257 int year = Integer.MAX_VALUE; 258 int dayOfYear = Integer.MAX_VALUE; 259 CalendarDate date = gcal.newCalendarDate(TimeZone.NO_TIMEZONE); 260 for (int i = 1; i < eras.length; i++) { 261 long fd = sinceFixedDates[i]; 262 CalendarDate transitionDate = eras[i].getSinceDate(); transitionDate.getYear()263 date.setDate(transitionDate.getYear(), BaseCalendar.JANUARY, 1); 264 long fdd = gcal.getFixedDate(date); 265 if (fd != fdd) { 266 dayOfYear = Math.min((int)(fd - fdd) + 1, dayOfYear); 267 } transitionDate.getYear()268 date.setDate(transitionDate.getYear(), BaseCalendar.DECEMBER, 31); 269 fdd = gcal.getFixedDate(date); 270 if (fd != fdd) { 271 dayOfYear = Math.min((int)(fdd - fd) + 1, dayOfYear); 272 } 273 LocalGregorianCalendar.Date lgd = getCalendarDate(fd - 1); 274 int y = lgd.getYear(); 275 // Unless the first year starts from January 1, the actual 276 // max value could be one year short. For example, if it's 277 // Showa 63 January 8, 63 is the actual max value since 278 // Showa 64 January 8 doesn't exist. 279 if (!(lgd.getMonth() == BaseCalendar.JANUARY && lgd.getDayOfMonth() == 1)) { 280 y--; 281 } 282 year = Math.min(y, year); 283 } 284 LEAST_MAX_VALUES[YEAR] = year; // Max year could be smaller than this value. 285 LEAST_MAX_VALUES[DAY_OF_YEAR] = dayOfYear; 286 } 287 288 /** 289 * jdate always has a sun.util.calendar.LocalGregorianCalendar.Date instance to 290 * avoid overhead of creating it for each calculation. 291 */ 292 private transient LocalGregorianCalendar.Date jdate; 293 294 /** 295 * Temporary int[2] to get time zone offsets. zoneOffsets[0] gets 296 * the GMT offset value and zoneOffsets[1] gets the daylight saving 297 * value. 298 */ 299 private transient int[] zoneOffsets; 300 301 /** 302 * Temporary storage for saving original fields[] values in 303 * non-lenient mode. 304 */ 305 private transient int[] originalFields; 306 307 /** 308 * Constructs a <code>JapaneseImperialCalendar</code> based on the current time 309 * in the given time zone with the given locale. 310 * 311 * @param zone the given time zone. 312 * @param aLocale the given locale. 313 */ JapaneseImperialCalendar(TimeZone zone, Locale aLocale)314 JapaneseImperialCalendar(TimeZone zone, Locale aLocale) { 315 super(zone, aLocale); 316 jdate = jcal.newCalendarDate(zone); 317 setTimeInMillis(System.currentTimeMillis()); 318 } 319 320 /** 321 * Constructs an "empty" {@code JapaneseImperialCalendar}. 322 * 323 * @param zone the given time zone 324 * @param aLocale the given locale 325 * @param flag the flag requesting an empty instance 326 */ JapaneseImperialCalendar(TimeZone zone, Locale aLocale, boolean flag)327 JapaneseImperialCalendar(TimeZone zone, Locale aLocale, boolean flag) { 328 super(zone, aLocale); 329 jdate = jcal.newCalendarDate(zone); 330 } 331 332 /** 333 * Returns {@code "japanese"} as the calendar type of this {@code 334 * JapaneseImperialCalendar}. 335 * 336 * @return {@code "japanese"} 337 */ 338 @Override getCalendarType()339 public String getCalendarType() { 340 return "japanese"; 341 } 342 343 /** 344 * Compares this <code>JapaneseImperialCalendar</code> to the specified 345 * <code>Object</code>. The result is <code>true</code> if and 346 * only if the argument is a <code>JapaneseImperialCalendar</code> object 347 * that represents the same time value (millisecond offset from 348 * the <a href="Calendar.html#Epoch">Epoch</a>) under the same 349 * <code>Calendar</code> parameters. 350 * 351 * @param obj the object to compare with. 352 * @return <code>true</code> if this object is equal to <code>obj</code>; 353 * <code>false</code> otherwise. 354 * @see Calendar#compareTo(Calendar) 355 */ 356 @Override equals(Object obj)357 public boolean equals(Object obj) { 358 return obj instanceof JapaneseImperialCalendar && 359 super.equals(obj); 360 } 361 362 /** 363 * Generates the hash code for this 364 * <code>JapaneseImperialCalendar</code> object. 365 */ 366 @Override hashCode()367 public int hashCode() { 368 return super.hashCode() ^ jdate.hashCode(); 369 } 370 371 /** 372 * Adds the specified (signed) amount of time to the given calendar field, 373 * based on the calendar's rules. 374 * 375 * <p><em>Add rule 1</em>. The value of <code>field</code> 376 * after the call minus the value of <code>field</code> before the 377 * call is <code>amount</code>, modulo any overflow that has occurred in 378 * <code>field</code>. Overflow occurs when a field value exceeds its 379 * range and, as a result, the next larger field is incremented or 380 * decremented and the field value is adjusted back into its range.</p> 381 * 382 * <p><em>Add rule 2</em>. If a smaller field is expected to be 383 * invariant, but it is impossible for it to be equal to its 384 * prior value because of changes in its minimum or maximum after 385 * <code>field</code> is changed, then its value is adjusted to be as close 386 * as possible to its expected value. A smaller field represents a 387 * smaller unit of time. <code>HOUR</code> is a smaller field than 388 * <code>DAY_OF_MONTH</code>. No adjustment is made to smaller fields 389 * that are not expected to be invariant. The calendar system 390 * determines what fields are expected to be invariant.</p> 391 * 392 * @param field the calendar field. 393 * @param amount the amount of date or time to be added to the field. 394 * @exception IllegalArgumentException if <code>field</code> is 395 * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown, 396 * or if any calendar fields have out-of-range values in 397 * non-lenient mode. 398 */ 399 @Override add(int field, int amount)400 public void add(int field, int amount) { 401 // If amount == 0, do nothing even the given field is out of 402 // range. This is tested by JCK. 403 if (amount == 0) { 404 return; // Do nothing! 405 } 406 407 if (field < 0 || field >= ZONE_OFFSET) { 408 throw new IllegalArgumentException(); 409 } 410 411 // Sync the time and calendar fields. 412 complete(); 413 414 if (field == YEAR) { 415 LocalGregorianCalendar.Date d = (LocalGregorianCalendar.Date) jdate.clone(); 416 d.addYear(amount); 417 pinDayOfMonth(d); 418 set(ERA, getEraIndex(d)); 419 set(YEAR, d.getYear()); 420 set(MONTH, d.getMonth() - 1); 421 set(DAY_OF_MONTH, d.getDayOfMonth()); 422 } else if (field == MONTH) { 423 LocalGregorianCalendar.Date d = (LocalGregorianCalendar.Date) jdate.clone(); 424 d.addMonth(amount); 425 pinDayOfMonth(d); 426 set(ERA, getEraIndex(d)); 427 set(YEAR, d.getYear()); 428 set(MONTH, d.getMonth() - 1); 429 set(DAY_OF_MONTH, d.getDayOfMonth()); 430 } else if (field == ERA) { 431 int era = internalGet(ERA) + amount; 432 if (era < 0) { 433 era = 0; 434 } else if (era > eras.length - 1) { 435 era = eras.length - 1; 436 } 437 set(ERA, era); 438 } else { 439 long delta = amount; 440 long timeOfDay = 0; 441 switch (field) { 442 // Handle the time fields here. Convert the given 443 // amount to milliseconds and call setTimeInMillis. 444 case HOUR: 445 case HOUR_OF_DAY: 446 delta *= 60 * 60 * 1000; // hours to milliseconds 447 break; 448 449 case MINUTE: 450 delta *= 60 * 1000; // minutes to milliseconds 451 break; 452 453 case SECOND: 454 delta *= 1000; // seconds to milliseconds 455 break; 456 457 case MILLISECOND: 458 break; 459 460 // Handle week, day and AM_PM fields which involves 461 // time zone offset change adjustment. Convert the 462 // given amount to the number of days. 463 case WEEK_OF_YEAR: 464 case WEEK_OF_MONTH: 465 case DAY_OF_WEEK_IN_MONTH: 466 delta *= 7; 467 break; 468 469 case DAY_OF_MONTH: // synonym of DATE 470 case DAY_OF_YEAR: 471 case DAY_OF_WEEK: 472 break; 473 474 case AM_PM: 475 // Convert the amount to the number of days (delta) 476 // and +12 or -12 hours (timeOfDay). 477 delta = amount / 2; 478 timeOfDay = 12 * (amount % 2); 479 break; 480 } 481 482 // The time fields don't require time zone offset change 483 // adjustment. 484 if (field >= HOUR) { 485 setTimeInMillis(time + delta); 486 return; 487 } 488 489 // The rest of the fields (week, day or AM_PM fields) 490 // require time zone offset (both GMT and DST) change 491 // adjustment. 492 493 // Translate the current time to the fixed date and time 494 // of the day. 495 long fd = cachedFixedDate; 496 timeOfDay += internalGet(HOUR_OF_DAY); 497 timeOfDay *= 60; 498 timeOfDay += internalGet(MINUTE); 499 timeOfDay *= 60; 500 timeOfDay += internalGet(SECOND); 501 timeOfDay *= 1000; 502 timeOfDay += internalGet(MILLISECOND); 503 if (timeOfDay >= ONE_DAY) { 504 fd++; 505 timeOfDay -= ONE_DAY; 506 } else if (timeOfDay < 0) { 507 fd--; 508 timeOfDay += ONE_DAY; 509 } 510 511 fd += delta; // fd is the expected fixed date after the calculation 512 int zoneOffset = internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET); 513 setTimeInMillis((fd - EPOCH_OFFSET) * ONE_DAY + timeOfDay - zoneOffset); 514 zoneOffset -= internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET); 515 // If the time zone offset has changed, then adjust the difference. 516 if (zoneOffset != 0) { 517 setTimeInMillis(time + zoneOffset); 518 long fd2 = cachedFixedDate; 519 // If the adjustment has changed the date, then take 520 // the previous one. 521 if (fd2 != fd) { 522 setTimeInMillis(time - zoneOffset); 523 } 524 } 525 } 526 } 527 528 @Override roll(int field, boolean up)529 public void roll(int field, boolean up) { 530 roll(field, up ? +1 : -1); 531 } 532 533 /** 534 * Adds a signed amount to the specified calendar field without changing larger fields. 535 * A negative roll amount means to subtract from field without changing 536 * larger fields. If the specified amount is 0, this method performs nothing. 537 * 538 * <p>This method calls {@link #complete()} before adding the 539 * amount so that all the calendar fields are normalized. If there 540 * is any calendar field having an out-of-range value in non-lenient mode, then an 541 * <code>IllegalArgumentException</code> is thrown. 542 * 543 * @param field the calendar field. 544 * @param amount the signed amount to add to <code>field</code>. 545 * @exception IllegalArgumentException if <code>field</code> is 546 * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown, 547 * or if any calendar fields have out-of-range values in 548 * non-lenient mode. 549 * @see #roll(int,boolean) 550 * @see #add(int,int) 551 * @see #set(int,int) 552 */ 553 @Override roll(int field, int amount)554 public void roll(int field, int amount) { 555 // If amount == 0, do nothing even the given field is out of 556 // range. This is tested by JCK. 557 if (amount == 0) { 558 return; 559 } 560 561 if (field < 0 || field >= ZONE_OFFSET) { 562 throw new IllegalArgumentException(); 563 } 564 565 // Sync the time and calendar fields. 566 complete(); 567 568 int min = getMinimum(field); 569 int max = getMaximum(field); 570 571 switch (field) { 572 case ERA: 573 case AM_PM: 574 case MINUTE: 575 case SECOND: 576 case MILLISECOND: 577 // These fields are handled simply, since they have fixed 578 // minima and maxima. Other fields are complicated, since 579 // the range within they must roll varies depending on the 580 // date, a time zone and the era transitions. 581 break; 582 583 case HOUR: 584 case HOUR_OF_DAY: 585 { 586 int unit = max + 1; // 12 or 24 hours 587 int h = internalGet(field); 588 int nh = (h + amount) % unit; 589 if (nh < 0) { 590 nh += unit; 591 } 592 time += ONE_HOUR * (nh - h); 593 594 // The day might have changed, which could happen if 595 // the daylight saving time transition brings it to 596 // the next day, although it's very unlikely. But we 597 // have to make sure not to change the larger fields. 598 CalendarDate d = jcal.getCalendarDate(time, getZone()); 599 if (internalGet(DAY_OF_MONTH) != d.getDayOfMonth()) { 600 d.setEra(jdate.getEra()); 601 d.setDate(internalGet(YEAR), 602 internalGet(MONTH) + 1, 603 internalGet(DAY_OF_MONTH)); 604 if (field == HOUR) { 605 assert (internalGet(AM_PM) == PM); 606 d.addHours(+12); // restore PM 607 } 608 time = jcal.getTime(d); 609 } 610 int hourOfDay = d.getHours(); 611 internalSet(field, hourOfDay % unit); 612 if (field == HOUR) { 613 internalSet(HOUR_OF_DAY, hourOfDay); 614 } else { 615 internalSet(AM_PM, hourOfDay / 12); 616 internalSet(HOUR, hourOfDay % 12); 617 } 618 619 // Time zone offset and/or daylight saving might have changed. 620 int zoneOffset = d.getZoneOffset(); 621 int saving = d.getDaylightSaving(); 622 internalSet(ZONE_OFFSET, zoneOffset - saving); 623 internalSet(DST_OFFSET, saving); 624 return; 625 } 626 627 case YEAR: 628 min = getActualMinimum(field); 629 max = getActualMaximum(field); 630 break; 631 632 case MONTH: 633 // Rolling the month involves both pinning the final value to [0, 11] 634 // and adjusting the DAY_OF_MONTH if necessary. We only adjust the 635 // DAY_OF_MONTH if, after updating the MONTH field, it is illegal. 636 // E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>. 637 { 638 if (!isTransitionYear(jdate.getNormalizedYear())) { 639 int year = jdate.getYear(); 640 if (year == getMaximum(YEAR)) { 641 CalendarDate jd = jcal.getCalendarDate(time, getZone()); 642 CalendarDate d = jcal.getCalendarDate(Long.MAX_VALUE, getZone()); 643 max = d.getMonth() - 1; 644 int n = getRolledValue(internalGet(field), amount, min, max); 645 if (n == max) { 646 // To avoid overflow, use an equivalent year. 647 jd.addYear(-400); 648 jd.setMonth(n + 1); 649 if (jd.getDayOfMonth() > d.getDayOfMonth()) { 650 jd.setDayOfMonth(d.getDayOfMonth()); 651 jcal.normalize(jd); 652 } 653 if (jd.getDayOfMonth() == d.getDayOfMonth() 654 && jd.getTimeOfDay() > d.getTimeOfDay()) { 655 jd.setMonth(n + 1); 656 jd.setDayOfMonth(d.getDayOfMonth() - 1); 657 jcal.normalize(jd); 658 // Month may have changed by the normalization. 659 n = jd.getMonth() - 1; 660 } 661 set(DAY_OF_MONTH, jd.getDayOfMonth()); 662 } 663 set(MONTH, n); 664 } else if (year == getMinimum(YEAR)) { 665 CalendarDate jd = jcal.getCalendarDate(time, getZone()); 666 CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); 667 min = d.getMonth() - 1; 668 int n = getRolledValue(internalGet(field), amount, min, max); 669 if (n == min) { 670 // To avoid underflow, use an equivalent year. 671 jd.addYear(+400); 672 jd.setMonth(n + 1); 673 if (jd.getDayOfMonth() < d.getDayOfMonth()) { 674 jd.setDayOfMonth(d.getDayOfMonth()); 675 jcal.normalize(jd); 676 } 677 if (jd.getDayOfMonth() == d.getDayOfMonth() 678 && jd.getTimeOfDay() < d.getTimeOfDay()) { 679 jd.setMonth(n + 1); 680 jd.setDayOfMonth(d.getDayOfMonth() + 1); 681 jcal.normalize(jd); 682 // Month may have changed by the normalization. 683 n = jd.getMonth() - 1; 684 } 685 set(DAY_OF_MONTH, jd.getDayOfMonth()); 686 } 687 set(MONTH, n); 688 } else { 689 int mon = (internalGet(MONTH) + amount) % 12; 690 if (mon < 0) { 691 mon += 12; 692 } 693 set(MONTH, mon); 694 695 // Keep the day of month in the range. We 696 // don't want to spill over into the next 697 // month; e.g., we don't want jan31 + 1 mo -> 698 // feb31 -> mar3. 699 int monthLen = monthLength(mon); 700 if (internalGet(DAY_OF_MONTH) > monthLen) { 701 set(DAY_OF_MONTH, monthLen); 702 } 703 } 704 } else { 705 int eraIndex = getEraIndex(jdate); 706 CalendarDate transition = null; 707 if (jdate.getYear() == 1) { 708 transition = eras[eraIndex].getSinceDate(); 709 min = transition.getMonth() - 1; 710 } else { 711 if (eraIndex < eras.length - 1) { 712 transition = eras[eraIndex + 1].getSinceDate(); 713 if (transition.getYear() == jdate.getNormalizedYear()) { 714 max = transition.getMonth() - 1; 715 if (transition.getDayOfMonth() == 1) { 716 max--; 717 } 718 } 719 } 720 } 721 722 if (min == max) { 723 // The year has only one month. No need to 724 // process further. (Showa Gan-nen (year 1) 725 // and the last year have only one month.) 726 return; 727 } 728 int n = getRolledValue(internalGet(field), amount, min, max); 729 set(MONTH, n); 730 if (n == min) { 731 if (!(transition.getMonth() == BaseCalendar.JANUARY 732 && transition.getDayOfMonth() == 1)) { 733 if (jdate.getDayOfMonth() < transition.getDayOfMonth()) { 734 set(DAY_OF_MONTH, transition.getDayOfMonth()); 735 } 736 } 737 } else if (n == max && (transition.getMonth() - 1 == n)) { 738 int dom = transition.getDayOfMonth(); 739 if (jdate.getDayOfMonth() >= dom) { 740 set(DAY_OF_MONTH, dom - 1); 741 } 742 } 743 } 744 return; 745 } 746 747 case WEEK_OF_YEAR: 748 { 749 int y = jdate.getNormalizedYear(); 750 max = getActualMaximum(WEEK_OF_YEAR); 751 set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK)); // update stamp[field] 752 int woy = internalGet(WEEK_OF_YEAR); 753 int value = woy + amount; 754 if (!isTransitionYear(jdate.getNormalizedYear())) { 755 int year = jdate.getYear(); 756 if (year == getMaximum(YEAR)) { 757 max = getActualMaximum(WEEK_OF_YEAR); 758 } else if (year == getMinimum(YEAR)) { 759 min = getActualMinimum(WEEK_OF_YEAR); 760 max = getActualMaximum(WEEK_OF_YEAR); 761 if (value > min && value < max) { 762 set(WEEK_OF_YEAR, value); 763 return; 764 } 765 766 } 767 // If the new value is in between min and max 768 // (exclusive), then we can use the value. 769 if (value > min && value < max) { 770 set(WEEK_OF_YEAR, value); 771 return; 772 } 773 long fd = cachedFixedDate; 774 // Make sure that the min week has the current DAY_OF_WEEK 775 long day1 = fd - (7 * (woy - min)); 776 if (year != getMinimum(YEAR)) { 777 if (gcal.getYearFromFixedDate(day1) != y) { 778 min++; 779 } 780 } else { 781 CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); 782 if (day1 < jcal.getFixedDate(d)) { 783 min++; 784 } 785 } 786 787 // Make sure the same thing for the max week 788 fd += 7 * (max - internalGet(WEEK_OF_YEAR)); 789 if (gcal.getYearFromFixedDate(fd) != y) { 790 max--; 791 } 792 break; 793 } 794 795 // Handle transition here. 796 long fd = cachedFixedDate; 797 long day1 = fd - (7 * (woy - min)); 798 // Make sure that the min week has the current DAY_OF_WEEK 799 LocalGregorianCalendar.Date d = getCalendarDate(day1); 800 if (!(d.getEra() == jdate.getEra() && d.getYear() == jdate.getYear())) { 801 min++; 802 } 803 804 // Make sure the same thing for the max week 805 fd += 7 * (max - woy); 806 jcal.getCalendarDateFromFixedDate(d, fd); 807 if (!(d.getEra() == jdate.getEra() && d.getYear() == jdate.getYear())) { 808 max--; 809 } 810 // value: the new WEEK_OF_YEAR which must be converted 811 // to month and day of month. 812 value = getRolledValue(woy, amount, min, max) - 1; 813 d = getCalendarDate(day1 + value * 7); 814 set(MONTH, d.getMonth() - 1); 815 set(DAY_OF_MONTH, d.getDayOfMonth()); 816 return; 817 } 818 819 case WEEK_OF_MONTH: 820 { 821 boolean isTransitionYear = isTransitionYear(jdate.getNormalizedYear()); 822 // dow: relative day of week from the first day of week 823 int dow = internalGet(DAY_OF_WEEK) - getFirstDayOfWeek(); 824 if (dow < 0) { 825 dow += 7; 826 } 827 828 long fd = cachedFixedDate; 829 long month1; // fixed date of the first day (usually 1) of the month 830 int monthLength; // actual month length 831 if (isTransitionYear) { 832 month1 = getFixedDateMonth1(jdate, fd); 833 monthLength = actualMonthLength(); 834 } else { 835 month1 = fd - internalGet(DAY_OF_MONTH) + 1; 836 monthLength = jcal.getMonthLength(jdate); 837 } 838 839 // the first day of week of the month. 840 long monthDay1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(month1 + 6, 841 getFirstDayOfWeek()); 842 // if the week has enough days to form a week, the 843 // week starts from the previous month. 844 if ((int)(monthDay1st - month1) >= getMinimalDaysInFirstWeek()) { 845 monthDay1st -= 7; 846 } 847 max = getActualMaximum(field); 848 849 // value: the new WEEK_OF_MONTH value 850 int value = getRolledValue(internalGet(field), amount, 1, max) - 1; 851 852 // nfd: fixed date of the rolled date 853 long nfd = monthDay1st + value * 7 + dow; 854 855 // Unlike WEEK_OF_YEAR, we need to change day of week if the 856 // nfd is out of the month. 857 if (nfd < month1) { 858 nfd = month1; 859 } else if (nfd >= (month1 + monthLength)) { 860 nfd = month1 + monthLength - 1; 861 } 862 set(DAY_OF_MONTH, (int)(nfd - month1) + 1); 863 return; 864 } 865 866 case DAY_OF_MONTH: 867 { 868 if (!isTransitionYear(jdate.getNormalizedYear())) { 869 max = jcal.getMonthLength(jdate); 870 break; 871 } 872 873 // TODO: Need to change the spec to be usable DAY_OF_MONTH rolling... 874 875 // Transition handling. We can't change year and era 876 // values here due to the Calendar roll spec! 877 long month1 = getFixedDateMonth1(jdate, cachedFixedDate); 878 879 // It may not be a regular month. Convert the date and range to 880 // the relative values, perform the roll, and 881 // convert the result back to the rolled date. 882 int value = getRolledValue((int)(cachedFixedDate - month1), amount, 883 0, actualMonthLength() - 1); 884 LocalGregorianCalendar.Date d = getCalendarDate(month1 + value); 885 assert getEraIndex(d) == internalGetEra() 886 && d.getYear() == internalGet(YEAR) && d.getMonth()-1 == internalGet(MONTH); 887 set(DAY_OF_MONTH, d.getDayOfMonth()); 888 return; 889 } 890 891 case DAY_OF_YEAR: 892 { 893 max = getActualMaximum(field); 894 if (!isTransitionYear(jdate.getNormalizedYear())) { 895 break; 896 } 897 898 // Handle transition. We can't change year and era values 899 // here due to the Calendar roll spec. 900 int value = getRolledValue(internalGet(DAY_OF_YEAR), amount, min, max); 901 long jan0 = cachedFixedDate - internalGet(DAY_OF_YEAR); 902 LocalGregorianCalendar.Date d = getCalendarDate(jan0 + value); 903 assert getEraIndex(d) == internalGetEra() && d.getYear() == internalGet(YEAR); 904 set(MONTH, d.getMonth() - 1); 905 set(DAY_OF_MONTH, d.getDayOfMonth()); 906 return; 907 } 908 909 case DAY_OF_WEEK: 910 { 911 int normalizedYear = jdate.getNormalizedYear(); 912 if (!isTransitionYear(normalizedYear) && !isTransitionYear(normalizedYear - 1)) { 913 // If the week of year is in the same year, we can 914 // just change DAY_OF_WEEK. 915 int weekOfYear = internalGet(WEEK_OF_YEAR); 916 if (weekOfYear > 1 && weekOfYear < 52) { 917 set(WEEK_OF_YEAR, internalGet(WEEK_OF_YEAR)); 918 max = SATURDAY; 919 break; 920 } 921 } 922 923 // We need to handle it in a different way around year 924 // boundaries and in the transition year. Note that 925 // changing era and year values violates the roll 926 // rule: not changing larger calendar fields... 927 amount %= 7; 928 if (amount == 0) { 929 return; 930 } 931 long fd = cachedFixedDate; 932 long dowFirst = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fd, getFirstDayOfWeek()); 933 fd += amount; 934 if (fd < dowFirst) { 935 fd += 7; 936 } else if (fd >= dowFirst + 7) { 937 fd -= 7; 938 } 939 LocalGregorianCalendar.Date d = getCalendarDate(fd); 940 set(ERA, getEraIndex(d)); 941 set(d.getYear(), d.getMonth() - 1, d.getDayOfMonth()); 942 return; 943 } 944 945 case DAY_OF_WEEK_IN_MONTH: 946 { 947 min = 1; // after having normalized, min should be 1. 948 if (!isTransitionYear(jdate.getNormalizedYear())) { 949 int dom = internalGet(DAY_OF_MONTH); 950 int monthLength = jcal.getMonthLength(jdate); 951 int lastDays = monthLength % 7; 952 max = monthLength / 7; 953 int x = (dom - 1) % 7; 954 if (x < lastDays) { 955 max++; 956 } 957 set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK)); 958 break; 959 } 960 961 // Transition year handling. 962 long fd = cachedFixedDate; 963 long month1 = getFixedDateMonth1(jdate, fd); 964 int monthLength = actualMonthLength(); 965 int lastDays = monthLength % 7; 966 max = monthLength / 7; 967 int x = (int)(fd - month1) % 7; 968 if (x < lastDays) { 969 max++; 970 } 971 int value = getRolledValue(internalGet(field), amount, min, max) - 1; 972 fd = month1 + value * 7 + x; 973 LocalGregorianCalendar.Date d = getCalendarDate(fd); 974 set(DAY_OF_MONTH, d.getDayOfMonth()); 975 return; 976 } 977 } 978 979 set(field, getRolledValue(internalGet(field), amount, min, max)); 980 } 981 982 @Override getDisplayName(int field, int style, Locale locale)983 public String getDisplayName(int field, int style, Locale locale) { 984 if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale, 985 ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) { 986 return null; 987 } 988 989 int fieldValue = get(field); 990 991 // "GanNen" is supported only in the LONG style. 992 if (field == YEAR 993 && (getBaseStyle(style) != LONG || fieldValue != 1 || get(ERA) == 0)) { 994 return null; 995 } 996 997 String name = CalendarDataUtility.retrieveFieldValueName(getCalendarType(), field, 998 fieldValue, style, locale); 999 // If the ERA value is null or empty, then 1000 // try to get its name or abbreviation from the Era instance. 1001 if ((name == null || name.isEmpty()) && 1002 field == ERA && 1003 fieldValue < eras.length) { 1004 Era era = eras[fieldValue]; 1005 name = (style == SHORT) ? era.getAbbreviation() : era.getName(); 1006 } 1007 return name; 1008 } 1009 1010 @Override getDisplayNames(int field, int style, Locale locale)1011 public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) { 1012 if (!checkDisplayNameParams(field, style, ALL_STYLES, NARROW_FORMAT, locale, 1013 ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) { 1014 return null; 1015 } 1016 Map<String, Integer> names; 1017 names = CalendarDataUtility.retrieveFieldValueNames(getCalendarType(), field, style, locale); 1018 // If strings[] has fewer than eras[], get more names from eras[]. 1019 if (names != null) { 1020 if (field == ERA) { 1021 int size = names.size(); 1022 if (style == ALL_STYLES) { 1023 Set<Integer> values = new HashSet<>(); 1024 // count unique era values 1025 for (String key : names.keySet()) { 1026 values.add(names.get(key)); 1027 } 1028 size = values.size(); 1029 } 1030 if (size < eras.length) { 1031 int baseStyle = getBaseStyle(style); 1032 for (int i = size; i < eras.length; i++) { 1033 Era era = eras[i]; 1034 if (baseStyle == ALL_STYLES || baseStyle == SHORT 1035 || baseStyle == NARROW_FORMAT) { 1036 names.put(era.getAbbreviation(), i); 1037 } 1038 if (baseStyle == ALL_STYLES || baseStyle == LONG) { 1039 names.put(era.getName(), i); 1040 } 1041 } 1042 } 1043 } 1044 } 1045 return names; 1046 } 1047 1048 /** 1049 * Returns the minimum value for the given calendar field of this 1050 * <code>Calendar</code> instance. The minimum value is 1051 * defined as the smallest value returned by the {@link 1052 * Calendar#get(int) get} method for any possible time value, 1053 * taking into consideration the current values of the 1054 * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, 1055 * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, 1056 * and {@link Calendar#getTimeZone() getTimeZone} methods. 1057 * 1058 * @param field the calendar field. 1059 * @return the minimum value for the given calendar field. 1060 * @see #getMaximum(int) 1061 * @see #getGreatestMinimum(int) 1062 * @see #getLeastMaximum(int) 1063 * @see #getActualMinimum(int) 1064 * @see #getActualMaximum(int) 1065 */ getMinimum(int field)1066 public int getMinimum(int field) { 1067 return MIN_VALUES[field]; 1068 } 1069 1070 /** 1071 * Returns the maximum value for the given calendar field of this 1072 * <code>GregorianCalendar</code> instance. The maximum value is 1073 * defined as the largest value returned by the {@link 1074 * Calendar#get(int) get} method for any possible time value, 1075 * taking into consideration the current values of the 1076 * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, 1077 * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, 1078 * and {@link Calendar#getTimeZone() getTimeZone} methods. 1079 * 1080 * @param field the calendar field. 1081 * @return the maximum value for the given calendar field. 1082 * @see #getMinimum(int) 1083 * @see #getGreatestMinimum(int) 1084 * @see #getLeastMaximum(int) 1085 * @see #getActualMinimum(int) 1086 * @see #getActualMaximum(int) 1087 */ getMaximum(int field)1088 public int getMaximum(int field) { 1089 switch (field) { 1090 case YEAR: 1091 { 1092 // The value should depend on the time zone of this calendar. 1093 LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE, 1094 getZone()); 1095 return Math.max(LEAST_MAX_VALUES[YEAR], d.getYear()); 1096 } 1097 } 1098 return MAX_VALUES[field]; 1099 } 1100 1101 /** 1102 * Returns the highest minimum value for the given calendar field 1103 * of this <code>GregorianCalendar</code> instance. The highest 1104 * minimum value is defined as the largest value returned by 1105 * {@link #getActualMinimum(int)} for any possible time value, 1106 * taking into consideration the current values of the 1107 * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, 1108 * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, 1109 * and {@link Calendar#getTimeZone() getTimeZone} methods. 1110 * 1111 * @param field the calendar field. 1112 * @return the highest minimum value for the given calendar field. 1113 * @see #getMinimum(int) 1114 * @see #getMaximum(int) 1115 * @see #getLeastMaximum(int) 1116 * @see #getActualMinimum(int) 1117 * @see #getActualMaximum(int) 1118 */ getGreatestMinimum(int field)1119 public int getGreatestMinimum(int field) { 1120 return field == YEAR ? 1 : MIN_VALUES[field]; 1121 } 1122 1123 /** 1124 * Returns the lowest maximum value for the given calendar field 1125 * of this <code>GregorianCalendar</code> instance. The lowest 1126 * maximum value is defined as the smallest value returned by 1127 * {@link #getActualMaximum(int)} for any possible time value, 1128 * taking into consideration the current values of the 1129 * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, 1130 * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, 1131 * and {@link Calendar#getTimeZone() getTimeZone} methods. 1132 * 1133 * @param field the calendar field 1134 * @return the lowest maximum value for the given calendar field. 1135 * @see #getMinimum(int) 1136 * @see #getMaximum(int) 1137 * @see #getGreatestMinimum(int) 1138 * @see #getActualMinimum(int) 1139 * @see #getActualMaximum(int) 1140 */ getLeastMaximum(int field)1141 public int getLeastMaximum(int field) { 1142 switch (field) { 1143 case YEAR: 1144 { 1145 return Math.min(LEAST_MAX_VALUES[YEAR], getMaximum(YEAR)); 1146 } 1147 } 1148 return LEAST_MAX_VALUES[field]; 1149 } 1150 1151 /** 1152 * Returns the minimum value that this calendar field could have, 1153 * taking into consideration the given time value and the current 1154 * values of the 1155 * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, 1156 * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, 1157 * and {@link Calendar#getTimeZone() getTimeZone} methods. 1158 * 1159 * @param field the calendar field 1160 * @return the minimum of the given field for the time value of 1161 * this <code>JapaneseImperialCalendar</code> 1162 * @see #getMinimum(int) 1163 * @see #getMaximum(int) 1164 * @see #getGreatestMinimum(int) 1165 * @see #getLeastMaximum(int) 1166 * @see #getActualMaximum(int) 1167 */ getActualMinimum(int field)1168 public int getActualMinimum(int field) { 1169 if (!isFieldSet(YEAR_MASK|MONTH_MASK|WEEK_OF_YEAR_MASK, field)) { 1170 return getMinimum(field); 1171 } 1172 1173 int value = 0; 1174 JapaneseImperialCalendar jc = getNormalizedCalendar(); 1175 // Get a local date which includes time of day and time zone, 1176 // which are missing in jc.jdate. 1177 LocalGregorianCalendar.Date jd = jcal.getCalendarDate(jc.getTimeInMillis(), 1178 getZone()); 1179 int eraIndex = getEraIndex(jd); 1180 switch (field) { 1181 case YEAR: 1182 { 1183 if (eraIndex > BEFORE_MEIJI) { 1184 value = 1; 1185 long since = eras[eraIndex].getSince(getZone()); 1186 CalendarDate d = jcal.getCalendarDate(since, getZone()); 1187 // Use the same year in jd to take care of leap 1188 // years. i.e., both jd and d must agree on leap 1189 // or common years. 1190 jd.setYear(d.getYear()); 1191 jcal.normalize(jd); 1192 assert jd.isLeapYear() == d.isLeapYear(); 1193 if (getYearOffsetInMillis(jd) < getYearOffsetInMillis(d)) { 1194 value++; 1195 } 1196 } else { 1197 value = getMinimum(field); 1198 CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); 1199 // Use an equvalent year of d.getYear() if 1200 // possible. Otherwise, ignore the leap year and 1201 // common year difference. 1202 int y = d.getYear(); 1203 if (y > 400) { 1204 y -= 400; 1205 } 1206 jd.setYear(y); 1207 jcal.normalize(jd); 1208 if (getYearOffsetInMillis(jd) < getYearOffsetInMillis(d)) { 1209 value++; 1210 } 1211 } 1212 } 1213 break; 1214 1215 case MONTH: 1216 { 1217 // In Before Meiji and Meiji, January is the first month. 1218 if (eraIndex > MEIJI && jd.getYear() == 1) { 1219 long since = eras[eraIndex].getSince(getZone()); 1220 CalendarDate d = jcal.getCalendarDate(since, getZone()); 1221 value = d.getMonth() - 1; 1222 if (jd.getDayOfMonth() < d.getDayOfMonth()) { 1223 value++; 1224 } 1225 } 1226 } 1227 break; 1228 1229 case WEEK_OF_YEAR: 1230 { 1231 value = 1; 1232 CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); 1233 // shift 400 years to avoid underflow 1234 d.addYear(+400); 1235 jcal.normalize(d); 1236 jd.setEra(d.getEra()); 1237 jd.setYear(d.getYear()); 1238 jcal.normalize(jd); 1239 1240 long jan1 = jcal.getFixedDate(d); 1241 long fd = jcal.getFixedDate(jd); 1242 int woy = getWeekNumber(jan1, fd); 1243 long day1 = fd - (7 * (woy - 1)); 1244 if ((day1 < jan1) || 1245 (day1 == jan1 && 1246 jd.getTimeOfDay() < d.getTimeOfDay())) { 1247 value++; 1248 } 1249 } 1250 break; 1251 } 1252 return value; 1253 } 1254 1255 /** 1256 * Returns the maximum value that this calendar field could have, 1257 * taking into consideration the given time value and the current 1258 * values of the 1259 * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek}, 1260 * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek}, 1261 * and 1262 * {@link Calendar#getTimeZone() getTimeZone} methods. 1263 * For example, if the date of this instance is Heisei 16February 1, 1264 * the actual maximum value of the <code>DAY_OF_MONTH</code> field 1265 * is 29 because Heisei 16 is a leap year, and if the date of this 1266 * instance is Heisei 17 February 1, it's 28. 1267 * 1268 * @param field the calendar field 1269 * @return the maximum of the given field for the time value of 1270 * this <code>JapaneseImperialCalendar</code> 1271 * @see #getMinimum(int) 1272 * @see #getMaximum(int) 1273 * @see #getGreatestMinimum(int) 1274 * @see #getLeastMaximum(int) 1275 * @see #getActualMinimum(int) 1276 */ getActualMaximum(int field)1277 public int getActualMaximum(int field) { 1278 final int fieldsForFixedMax = ERA_MASK|DAY_OF_WEEK_MASK|HOUR_MASK|AM_PM_MASK| 1279 HOUR_OF_DAY_MASK|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK| 1280 ZONE_OFFSET_MASK|DST_OFFSET_MASK; 1281 if ((fieldsForFixedMax & (1<<field)) != 0) { 1282 return getMaximum(field); 1283 } 1284 1285 JapaneseImperialCalendar jc = getNormalizedCalendar(); 1286 LocalGregorianCalendar.Date date = jc.jdate; 1287 int normalizedYear = date.getNormalizedYear(); 1288 1289 int value = -1; 1290 switch (field) { 1291 case MONTH: 1292 { 1293 value = DECEMBER; 1294 if (isTransitionYear(date.getNormalizedYear())) { 1295 // TODO: there may be multiple transitions in a year. 1296 int eraIndex = getEraIndex(date); 1297 if (date.getYear() != 1) { 1298 eraIndex++; 1299 assert eraIndex < eras.length; 1300 } 1301 long transition = sinceFixedDates[eraIndex]; 1302 long fd = jc.cachedFixedDate; 1303 if (fd < transition) { 1304 LocalGregorianCalendar.Date ldate 1305 = (LocalGregorianCalendar.Date) date.clone(); 1306 jcal.getCalendarDateFromFixedDate(ldate, transition - 1); 1307 value = ldate.getMonth() - 1; 1308 } 1309 } else { 1310 LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE, 1311 getZone()); 1312 if (date.getEra() == d.getEra() && date.getYear() == d.getYear()) { 1313 value = d.getMonth() - 1; 1314 } 1315 } 1316 } 1317 break; 1318 1319 case DAY_OF_MONTH: 1320 value = jcal.getMonthLength(date); 1321 break; 1322 1323 case DAY_OF_YEAR: 1324 { 1325 if (isTransitionYear(date.getNormalizedYear())) { 1326 // Handle transition year. 1327 // TODO: there may be multiple transitions in a year. 1328 int eraIndex = getEraIndex(date); 1329 if (date.getYear() != 1) { 1330 eraIndex++; 1331 assert eraIndex < eras.length; 1332 } 1333 long transition = sinceFixedDates[eraIndex]; 1334 long fd = jc.cachedFixedDate; 1335 CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE); 1336 d.setDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1); 1337 if (fd < transition) { 1338 value = (int)(transition - gcal.getFixedDate(d)); 1339 } else { 1340 d.addYear(+1); 1341 value = (int)(gcal.getFixedDate(d) - transition); 1342 } 1343 } else { 1344 LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE, 1345 getZone()); 1346 if (date.getEra() == d.getEra() && date.getYear() == d.getYear()) { 1347 long fd = jcal.getFixedDate(d); 1348 long jan1 = getFixedDateJan1(d, fd); 1349 value = (int)(fd - jan1) + 1; 1350 } else if (date.getYear() == getMinimum(YEAR)) { 1351 CalendarDate d1 = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); 1352 long fd1 = jcal.getFixedDate(d1); 1353 d1.addYear(1); 1354 d1.setMonth(BaseCalendar.JANUARY).setDayOfMonth(1); 1355 jcal.normalize(d1); 1356 long fd2 = jcal.getFixedDate(d1); 1357 value = (int)(fd2 - fd1); 1358 } else { 1359 value = jcal.getYearLength(date); 1360 } 1361 } 1362 } 1363 break; 1364 1365 case WEEK_OF_YEAR: 1366 { 1367 if (!isTransitionYear(date.getNormalizedYear())) { 1368 LocalGregorianCalendar.Date jd = jcal.getCalendarDate(Long.MAX_VALUE, 1369 getZone()); 1370 if (date.getEra() == jd.getEra() && date.getYear() == jd.getYear()) { 1371 long fd = jcal.getFixedDate(jd); 1372 long jan1 = getFixedDateJan1(jd, fd); 1373 value = getWeekNumber(jan1, fd); 1374 } else if (date.getEra() == null && date.getYear() == getMinimum(YEAR)) { 1375 CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); 1376 // shift 400 years to avoid underflow 1377 d.addYear(+400); 1378 jcal.normalize(d); 1379 jd.setEra(d.getEra()); 1380 jd.setDate(d.getYear() + 1, BaseCalendar.JANUARY, 1); 1381 jcal.normalize(jd); 1382 long jan1 = jcal.getFixedDate(d); 1383 long nextJan1 = jcal.getFixedDate(jd); 1384 long nextJan1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6, 1385 getFirstDayOfWeek()); 1386 int ndays = (int)(nextJan1st - nextJan1); 1387 if (ndays >= getMinimalDaysInFirstWeek()) { 1388 nextJan1st -= 7; 1389 } 1390 value = getWeekNumber(jan1, nextJan1st); 1391 } else { 1392 // Get the day of week of January 1 of the year 1393 CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE); 1394 d.setDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1); 1395 int dayOfWeek = gcal.getDayOfWeek(d); 1396 // Normalize the day of week with the firstDayOfWeek value 1397 dayOfWeek -= getFirstDayOfWeek(); 1398 if (dayOfWeek < 0) { 1399 dayOfWeek += 7; 1400 } 1401 value = 52; 1402 int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1; 1403 if ((magic == 6) || 1404 (date.isLeapYear() && (magic == 5 || magic == 12))) { 1405 value++; 1406 } 1407 } 1408 break; 1409 } 1410 1411 if (jc == this) { 1412 jc = (JapaneseImperialCalendar) jc.clone(); 1413 } 1414 int max = getActualMaximum(DAY_OF_YEAR); 1415 jc.set(DAY_OF_YEAR, max); 1416 value = jc.get(WEEK_OF_YEAR); 1417 if (value == 1 && max > 7) { 1418 jc.add(WEEK_OF_YEAR, -1); 1419 value = jc.get(WEEK_OF_YEAR); 1420 } 1421 } 1422 break; 1423 1424 case WEEK_OF_MONTH: 1425 { 1426 LocalGregorianCalendar.Date jd = jcal.getCalendarDate(Long.MAX_VALUE, 1427 getZone()); 1428 if (!(date.getEra() == jd.getEra() && date.getYear() == jd.getYear())) { 1429 CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE); 1430 d.setDate(date.getNormalizedYear(), date.getMonth(), 1); 1431 int dayOfWeek = gcal.getDayOfWeek(d); 1432 int monthLength = gcal.getMonthLength(d); 1433 dayOfWeek -= getFirstDayOfWeek(); 1434 if (dayOfWeek < 0) { 1435 dayOfWeek += 7; 1436 } 1437 int nDaysFirstWeek = 7 - dayOfWeek; // # of days in the first week 1438 value = 3; 1439 if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) { 1440 value++; 1441 } 1442 monthLength -= nDaysFirstWeek + 7 * 3; 1443 if (monthLength > 0) { 1444 value++; 1445 if (monthLength > 7) { 1446 value++; 1447 } 1448 } 1449 } else { 1450 long fd = jcal.getFixedDate(jd); 1451 long month1 = fd - jd.getDayOfMonth() + 1; 1452 value = getWeekNumber(month1, fd); 1453 } 1454 } 1455 break; 1456 1457 case DAY_OF_WEEK_IN_MONTH: 1458 { 1459 int ndays, dow1; 1460 int dow = date.getDayOfWeek(); 1461 BaseCalendar.Date d = (BaseCalendar.Date) date.clone(); 1462 ndays = jcal.getMonthLength(d); 1463 d.setDayOfMonth(1); 1464 jcal.normalize(d); 1465 dow1 = d.getDayOfWeek(); 1466 int x = dow - dow1; 1467 if (x < 0) { 1468 x += 7; 1469 } 1470 ndays -= x; 1471 value = (ndays + 6) / 7; 1472 } 1473 break; 1474 1475 case YEAR: 1476 { 1477 CalendarDate jd = jcal.getCalendarDate(jc.getTimeInMillis(), getZone()); 1478 CalendarDate d; 1479 int eraIndex = getEraIndex(date); 1480 if (eraIndex == eras.length - 1) { 1481 d = jcal.getCalendarDate(Long.MAX_VALUE, getZone()); 1482 value = d.getYear(); 1483 // Use an equivalent year for the 1484 // getYearOffsetInMillis call to avoid overflow. 1485 if (value > 400) { 1486 jd.setYear(value - 400); 1487 } 1488 } else { 1489 d = jcal.getCalendarDate(eras[eraIndex + 1].getSince(getZone()) - 1, 1490 getZone()); 1491 value = d.getYear(); 1492 // Use the same year as d.getYear() to be 1493 // consistent with leap and common years. 1494 jd.setYear(value); 1495 } 1496 jcal.normalize(jd); 1497 if (getYearOffsetInMillis(jd) > getYearOffsetInMillis(d)) { 1498 value--; 1499 } 1500 } 1501 break; 1502 1503 default: 1504 throw new ArrayIndexOutOfBoundsException(field); 1505 } 1506 return value; 1507 } 1508 1509 /** 1510 * Returns the millisecond offset from the beginning of the 1511 * year. In the year for Long.MIN_VALUE, it's a pseudo value 1512 * beyond the limit. The given CalendarDate object must have been 1513 * normalized before calling this method. 1514 */ getYearOffsetInMillis(CalendarDate date)1515 private long getYearOffsetInMillis(CalendarDate date) { 1516 long t = (jcal.getDayOfYear(date) - 1) * ONE_DAY; 1517 return t + date.getTimeOfDay() - date.getZoneOffset(); 1518 } 1519 clone()1520 public Object clone() { 1521 JapaneseImperialCalendar other = (JapaneseImperialCalendar) super.clone(); 1522 1523 other.jdate = (LocalGregorianCalendar.Date) jdate.clone(); 1524 other.originalFields = null; 1525 other.zoneOffsets = null; 1526 return other; 1527 } 1528 getTimeZone()1529 public TimeZone getTimeZone() { 1530 TimeZone zone = super.getTimeZone(); 1531 // To share the zone by the CalendarDate 1532 jdate.setZone(zone); 1533 return zone; 1534 } 1535 setTimeZone(TimeZone zone)1536 public void setTimeZone(TimeZone zone) { 1537 super.setTimeZone(zone); 1538 // To share the zone by the CalendarDate 1539 jdate.setZone(zone); 1540 } 1541 1542 /** 1543 * The fixed date corresponding to jdate. If the value is 1544 * Long.MIN_VALUE, the fixed date value is unknown. 1545 */ 1546 transient private long cachedFixedDate = Long.MIN_VALUE; 1547 1548 /** 1549 * Converts the time value (millisecond offset from the <a 1550 * href="Calendar.html#Epoch">Epoch</a>) to calendar field values. 1551 * The time is <em>not</em> 1552 * recomputed first; to recompute the time, then the fields, call the 1553 * <code>complete</code> method. 1554 * 1555 * @see Calendar#complete 1556 */ computeFields()1557 protected void computeFields() { 1558 int mask = 0; 1559 if (isPartiallyNormalized()) { 1560 // Determine which calendar fields need to be computed. 1561 mask = getSetStateFields(); 1562 int fieldMask = ~mask & ALL_FIELDS; 1563 if (fieldMask != 0 || cachedFixedDate == Long.MIN_VALUE) { 1564 mask |= computeFields(fieldMask, 1565 mask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK)); 1566 assert mask == ALL_FIELDS; 1567 } 1568 } else { 1569 // Specify all fields 1570 mask = ALL_FIELDS; 1571 computeFields(mask, 0); 1572 } 1573 // After computing all the fields, set the field state to `COMPUTED'. 1574 setFieldsComputed(mask); 1575 } 1576 1577 /** 1578 * This computeFields implements the conversion from UTC 1579 * (millisecond offset from the Epoch) to calendar 1580 * field values. fieldMask specifies which fields to change the 1581 * setting state to COMPUTED, although all fields are set to 1582 * the correct values. This is required to fix 4685354. 1583 * 1584 * @param fieldMask a bit mask to specify which fields to change 1585 * the setting state. 1586 * @param tzMask a bit mask to specify which time zone offset 1587 * fields to be used for time calculations 1588 * @return a new field mask that indicates what field values have 1589 * actually been set. 1590 */ computeFields(int fieldMask, int tzMask)1591 private int computeFields(int fieldMask, int tzMask) { 1592 int zoneOffset = 0; 1593 TimeZone tz = getZone(); 1594 if (zoneOffsets == null) { 1595 zoneOffsets = new int[2]; 1596 } 1597 if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) { 1598 if (tz instanceof ZoneInfo) { 1599 zoneOffset = ((ZoneInfo)tz).getOffsets(time, zoneOffsets); 1600 } else { 1601 zoneOffset = tz.getOffset(time); 1602 zoneOffsets[0] = tz.getRawOffset(); 1603 zoneOffsets[1] = zoneOffset - zoneOffsets[0]; 1604 } 1605 } 1606 if (tzMask != 0) { 1607 if (isFieldSet(tzMask, ZONE_OFFSET)) { 1608 zoneOffsets[0] = internalGet(ZONE_OFFSET); 1609 } 1610 if (isFieldSet(tzMask, DST_OFFSET)) { 1611 zoneOffsets[1] = internalGet(DST_OFFSET); 1612 } 1613 zoneOffset = zoneOffsets[0] + zoneOffsets[1]; 1614 } 1615 1616 // By computing time and zoneOffset separately, we can take 1617 // the wider range of time+zoneOffset than the previous 1618 // implementation. 1619 long fixedDate = zoneOffset / ONE_DAY; 1620 int timeOfDay = zoneOffset % (int)ONE_DAY; 1621 fixedDate += time / ONE_DAY; 1622 timeOfDay += (int) (time % ONE_DAY); 1623 if (timeOfDay >= ONE_DAY) { 1624 timeOfDay -= ONE_DAY; 1625 ++fixedDate; 1626 } else { 1627 while (timeOfDay < 0) { 1628 timeOfDay += ONE_DAY; 1629 --fixedDate; 1630 } 1631 } 1632 fixedDate += EPOCH_OFFSET; 1633 1634 // See if we can use jdate to avoid date calculation. 1635 if (fixedDate != cachedFixedDate || fixedDate < 0) { 1636 jcal.getCalendarDateFromFixedDate(jdate, fixedDate); 1637 cachedFixedDate = fixedDate; 1638 } 1639 int era = getEraIndex(jdate); 1640 int year = jdate.getYear(); 1641 1642 // Always set the ERA and YEAR values. 1643 internalSet(ERA, era); 1644 internalSet(YEAR, year); 1645 int mask = fieldMask | (ERA_MASK|YEAR_MASK); 1646 1647 int month = jdate.getMonth() - 1; // 0-based 1648 int dayOfMonth = jdate.getDayOfMonth(); 1649 1650 // Set the basic date fields. 1651 if ((fieldMask & (MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK)) 1652 != 0) { 1653 internalSet(MONTH, month); 1654 internalSet(DAY_OF_MONTH, dayOfMonth); 1655 internalSet(DAY_OF_WEEK, jdate.getDayOfWeek()); 1656 mask |= MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK; 1657 } 1658 1659 if ((fieldMask & (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK 1660 |MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK)) != 0) { 1661 if (timeOfDay != 0) { 1662 int hours = timeOfDay / ONE_HOUR; 1663 internalSet(HOUR_OF_DAY, hours); 1664 internalSet(AM_PM, hours / 12); // Assume AM == 0 1665 internalSet(HOUR, hours % 12); 1666 int r = timeOfDay % ONE_HOUR; 1667 internalSet(MINUTE, r / ONE_MINUTE); 1668 r %= ONE_MINUTE; 1669 internalSet(SECOND, r / ONE_SECOND); 1670 internalSet(MILLISECOND, r % ONE_SECOND); 1671 } else { 1672 internalSet(HOUR_OF_DAY, 0); 1673 internalSet(AM_PM, AM); 1674 internalSet(HOUR, 0); 1675 internalSet(MINUTE, 0); 1676 internalSet(SECOND, 0); 1677 internalSet(MILLISECOND, 0); 1678 } 1679 mask |= (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK 1680 |MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK); 1681 } 1682 1683 if ((fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) != 0) { 1684 internalSet(ZONE_OFFSET, zoneOffsets[0]); 1685 internalSet(DST_OFFSET, zoneOffsets[1]); 1686 mask |= (ZONE_OFFSET_MASK|DST_OFFSET_MASK); 1687 } 1688 1689 if ((fieldMask & (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK 1690 |WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK)) != 0) { 1691 int normalizedYear = jdate.getNormalizedYear(); 1692 // If it's a year of an era transition, we need to handle 1693 // irregular year boundaries. 1694 boolean transitionYear = isTransitionYear(jdate.getNormalizedYear()); 1695 int dayOfYear; 1696 long fixedDateJan1; 1697 if (transitionYear) { 1698 fixedDateJan1 = getFixedDateJan1(jdate, fixedDate); 1699 dayOfYear = (int)(fixedDate - fixedDateJan1) + 1; 1700 } else if (normalizedYear == MIN_VALUES[YEAR]) { 1701 CalendarDate dx = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); 1702 fixedDateJan1 = jcal.getFixedDate(dx); 1703 dayOfYear = (int)(fixedDate - fixedDateJan1) + 1; 1704 } else { 1705 dayOfYear = (int) jcal.getDayOfYear(jdate); 1706 fixedDateJan1 = fixedDate - dayOfYear + 1; 1707 } 1708 long fixedDateMonth1 = transitionYear ? 1709 getFixedDateMonth1(jdate, fixedDate) : fixedDate - dayOfMonth + 1; 1710 1711 internalSet(DAY_OF_YEAR, dayOfYear); 1712 internalSet(DAY_OF_WEEK_IN_MONTH, (dayOfMonth - 1) / 7 + 1); 1713 1714 int weekOfYear = getWeekNumber(fixedDateJan1, fixedDate); 1715 1716 // The spec is to calculate WEEK_OF_YEAR in the 1717 // ISO8601-style. This creates problems, though. 1718 if (weekOfYear == 0) { 1719 // If the date belongs to the last week of the 1720 // previous year, use the week number of "12/31" of 1721 // the "previous" year. Again, if the previous year is 1722 // a transition year, we need to take care of it. 1723 // Usually the previous day of the first day of a year 1724 // is December 31, which is not always true in the 1725 // Japanese imperial calendar system. 1726 long fixedDec31 = fixedDateJan1 - 1; 1727 long prevJan1; 1728 LocalGregorianCalendar.Date d = getCalendarDate(fixedDec31); 1729 if (!(transitionYear || isTransitionYear(d.getNormalizedYear()))) { 1730 prevJan1 = fixedDateJan1 - 365; 1731 if (d.isLeapYear()) { 1732 --prevJan1; 1733 } 1734 } else if (transitionYear) { 1735 if (jdate.getYear() == 1) { 1736 // As of Reiwa (since Meiji) there's no case 1737 // that there are multiple transitions in a 1738 // year. Historically there was such 1739 // case. There might be such case again in the 1740 // future. 1741 if (era > REIWA) { 1742 CalendarDate pd = eras[era - 1].getSinceDate(); 1743 if (normalizedYear == pd.getYear()) { 1744 d.setMonth(pd.getMonth()).setDayOfMonth(pd.getDayOfMonth()); 1745 } 1746 } else { 1747 d.setMonth(LocalGregorianCalendar.JANUARY).setDayOfMonth(1); 1748 } 1749 jcal.normalize(d); 1750 prevJan1 = jcal.getFixedDate(d); 1751 } else { 1752 prevJan1 = fixedDateJan1 - 365; 1753 if (d.isLeapYear()) { 1754 --prevJan1; 1755 } 1756 } 1757 } else { 1758 CalendarDate cd = eras[getEraIndex(jdate)].getSinceDate(); 1759 d.setMonth(cd.getMonth()).setDayOfMonth(cd.getDayOfMonth()); 1760 jcal.normalize(d); 1761 prevJan1 = jcal.getFixedDate(d); 1762 } 1763 weekOfYear = getWeekNumber(prevJan1, fixedDec31); 1764 } else { 1765 if (!transitionYear) { 1766 // Regular years 1767 if (weekOfYear >= 52) { 1768 long nextJan1 = fixedDateJan1 + 365; 1769 if (jdate.isLeapYear()) { 1770 nextJan1++; 1771 } 1772 long nextJan1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6, 1773 getFirstDayOfWeek()); 1774 int ndays = (int)(nextJan1st - nextJan1); 1775 if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) { 1776 // The first days forms a week in which the date is included. 1777 weekOfYear = 1; 1778 } 1779 } 1780 } else { 1781 LocalGregorianCalendar.Date d = (LocalGregorianCalendar.Date) jdate.clone(); 1782 long nextJan1; 1783 if (jdate.getYear() == 1) { 1784 d.addYear(+1); 1785 d.setMonth(LocalGregorianCalendar.JANUARY).setDayOfMonth(1); 1786 nextJan1 = jcal.getFixedDate(d); 1787 } else { 1788 int nextEraIndex = getEraIndex(d) + 1; 1789 CalendarDate cd = eras[nextEraIndex].getSinceDate(); 1790 d.setEra(eras[nextEraIndex]); 1791 d.setDate(1, cd.getMonth(), cd.getDayOfMonth()); 1792 jcal.normalize(d); 1793 nextJan1 = jcal.getFixedDate(d); 1794 } 1795 long nextJan1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6, 1796 getFirstDayOfWeek()); 1797 int ndays = (int)(nextJan1st - nextJan1); 1798 if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) { 1799 // The first days forms a week in which the date is included. 1800 weekOfYear = 1; 1801 } 1802 } 1803 } 1804 internalSet(WEEK_OF_YEAR, weekOfYear); 1805 internalSet(WEEK_OF_MONTH, getWeekNumber(fixedDateMonth1, fixedDate)); 1806 mask |= (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK); 1807 } 1808 return mask; 1809 } 1810 1811 /** 1812 * Returns the number of weeks in a period between fixedDay1 and 1813 * fixedDate. The getFirstDayOfWeek-getMinimalDaysInFirstWeek rule 1814 * is applied to calculate the number of weeks. 1815 * 1816 * @param fixedDay1 the fixed date of the first day of the period 1817 * @param fixedDate the fixed date of the last day of the period 1818 * @return the number of weeks of the given period 1819 */ getWeekNumber(long fixedDay1, long fixedDate)1820 private int getWeekNumber(long fixedDay1, long fixedDate) { 1821 // We can always use `jcal' since Julian and Gregorian are the 1822 // same thing for this calculation. 1823 long fixedDay1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDay1 + 6, 1824 getFirstDayOfWeek()); 1825 int ndays = (int)(fixedDay1st - fixedDay1); 1826 assert ndays <= 7; 1827 if (ndays >= getMinimalDaysInFirstWeek()) { 1828 fixedDay1st -= 7; 1829 } 1830 int normalizedDayOfPeriod = (int)(fixedDate - fixedDay1st); 1831 if (normalizedDayOfPeriod >= 0) { 1832 return normalizedDayOfPeriod / 7 + 1; 1833 } 1834 return CalendarUtils.floorDivide(normalizedDayOfPeriod, 7) + 1; 1835 } 1836 1837 /** 1838 * Converts calendar field values to the time value (millisecond 1839 * offset from the <a href="Calendar.html#Epoch">Epoch</a>). 1840 * 1841 * @exception IllegalArgumentException if any calendar fields are invalid. 1842 */ computeTime()1843 protected void computeTime() { 1844 // In non-lenient mode, perform brief checking of calendar 1845 // fields which have been set externally. Through this 1846 // checking, the field values are stored in originalFields[] 1847 // to see if any of them are normalized later. 1848 if (!isLenient()) { 1849 if (originalFields == null) { 1850 originalFields = new int[FIELD_COUNT]; 1851 } 1852 for (int field = 0; field < FIELD_COUNT; field++) { 1853 int value = internalGet(field); 1854 if (isExternallySet(field)) { 1855 // Quick validation for any out of range values 1856 if (value < getMinimum(field) || value > getMaximum(field)) { 1857 throw new IllegalArgumentException(getFieldName(field)); 1858 } 1859 } 1860 originalFields[field] = value; 1861 } 1862 } 1863 1864 // Let the super class determine which calendar fields to be 1865 // used to calculate the time. 1866 int fieldMask = selectFields(); 1867 1868 int year; 1869 int era; 1870 1871 if (isSet(ERA)) { 1872 era = internalGet(ERA); 1873 year = isSet(YEAR) ? internalGet(YEAR) : 1; 1874 } else { 1875 if (isSet(YEAR)) { 1876 era = currentEra; 1877 year = internalGet(YEAR); 1878 } else { 1879 // Equivalent to 1970 (Gregorian) 1880 era = SHOWA; 1881 year = 45; 1882 } 1883 } 1884 1885 // Calculate the time of day. We rely on the convention that 1886 // an UNSET field has 0. 1887 long timeOfDay = 0; 1888 if (isFieldSet(fieldMask, HOUR_OF_DAY)) { 1889 timeOfDay += (long) internalGet(HOUR_OF_DAY); 1890 } else { 1891 timeOfDay += internalGet(HOUR); 1892 // The default value of AM_PM is 0 which designates AM. 1893 if (isFieldSet(fieldMask, AM_PM)) { 1894 timeOfDay += 12 * internalGet(AM_PM); 1895 } 1896 } 1897 timeOfDay *= 60; 1898 timeOfDay += internalGet(MINUTE); 1899 timeOfDay *= 60; 1900 timeOfDay += internalGet(SECOND); 1901 timeOfDay *= 1000; 1902 timeOfDay += internalGet(MILLISECOND); 1903 1904 // Convert the time of day to the number of days and the 1905 // millisecond offset from midnight. 1906 long fixedDate = timeOfDay / ONE_DAY; 1907 timeOfDay %= ONE_DAY; 1908 while (timeOfDay < 0) { 1909 timeOfDay += ONE_DAY; 1910 --fixedDate; 1911 } 1912 1913 // Calculate the fixed date since January 1, 1 (Gregorian). 1914 fixedDate += getFixedDate(era, year, fieldMask); 1915 1916 // millis represents local wall-clock time in milliseconds. 1917 long millis = (fixedDate - EPOCH_OFFSET) * ONE_DAY + timeOfDay; 1918 1919 // Compute the time zone offset and DST offset. There are two potential 1920 // ambiguities here. We'll assume a 2:00 am (wall time) switchover time 1921 // for discussion purposes here. 1922 // 1. The transition into DST. Here, a designated time of 2:00 am - 2:59 am 1923 // can be in standard or in DST depending. However, 2:00 am is an invalid 1924 // representation (the representation jumps from 1:59:59 am Std to 3:00:00 am DST). 1925 // We assume standard time. 1926 // 2. The transition out of DST. Here, a designated time of 1:00 am - 1:59 am 1927 // can be in standard or DST. Both are valid representations (the rep 1928 // jumps from 1:59:59 DST to 1:00:00 Std). 1929 // Again, we assume standard time. 1930 // We use the TimeZone object, unless the user has explicitly set the ZONE_OFFSET 1931 // or DST_OFFSET fields; then we use those fields. 1932 TimeZone zone = getZone(); 1933 if (zoneOffsets == null) { 1934 zoneOffsets = new int[2]; 1935 } 1936 int tzMask = fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK); 1937 if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) { 1938 if (zone instanceof ZoneInfo) { 1939 ((ZoneInfo)zone).getOffsetsByWall(millis, zoneOffsets); 1940 } else { 1941 zone.getOffsets(millis - zone.getRawOffset(), zoneOffsets); 1942 } 1943 } 1944 if (tzMask != 0) { 1945 if (isFieldSet(tzMask, ZONE_OFFSET)) { 1946 zoneOffsets[0] = internalGet(ZONE_OFFSET); 1947 } 1948 if (isFieldSet(tzMask, DST_OFFSET)) { 1949 zoneOffsets[1] = internalGet(DST_OFFSET); 1950 } 1951 } 1952 1953 // Adjust the time zone offset values to get the UTC time. 1954 millis -= zoneOffsets[0] + zoneOffsets[1]; 1955 1956 // Set this calendar's time in milliseconds 1957 time = millis; 1958 1959 int mask = computeFields(fieldMask | getSetStateFields(), tzMask); 1960 1961 if (!isLenient()) { 1962 for (int field = 0; field < FIELD_COUNT; field++) { 1963 if (!isExternallySet(field)) { 1964 continue; 1965 } 1966 if (originalFields[field] != internalGet(field)) { 1967 int wrongValue = internalGet(field); 1968 // Restore the original field values 1969 System.arraycopy(originalFields, 0, fields, 0, fields.length); 1970 throw new IllegalArgumentException(getFieldName(field) + "=" + wrongValue 1971 + ", expected " + originalFields[field]); 1972 } 1973 } 1974 } 1975 setFieldsNormalized(mask); 1976 } 1977 1978 /** 1979 * Computes the fixed date under either the Gregorian or the 1980 * Julian calendar, using the given year and the specified calendar fields. 1981 * 1982 * @param era era index 1983 * @param year the normalized year number, with 0 indicating the 1984 * year 1 BCE, -1 indicating 2 BCE, etc. 1985 * @param fieldMask the calendar fields to be used for the date calculation 1986 * @return the fixed date 1987 * @see Calendar#selectFields 1988 */ getFixedDate(int era, int year, int fieldMask)1989 private long getFixedDate(int era, int year, int fieldMask) { 1990 int month = JANUARY; 1991 int firstDayOfMonth = 1; 1992 if (isFieldSet(fieldMask, MONTH)) { 1993 // No need to check if MONTH has been set (no isSet(MONTH) 1994 // call) since its unset value happens to be JANUARY (0). 1995 month = internalGet(MONTH); 1996 1997 // If the month is out of range, adjust it into range. 1998 if (month > DECEMBER) { 1999 year += month / 12; 2000 month %= 12; 2001 } else if (month < JANUARY) { 2002 int[] rem = new int[1]; 2003 year += CalendarUtils.floorDivide(month, 12, rem); 2004 month = rem[0]; 2005 } 2006 } else { 2007 if (year == 1 && era != 0) { 2008 CalendarDate d = eras[era].getSinceDate(); 2009 month = d.getMonth() - 1; 2010 firstDayOfMonth = d.getDayOfMonth(); 2011 } 2012 } 2013 2014 // Adjust the base date if year is the minimum value. 2015 if (year == MIN_VALUES[YEAR]) { 2016 CalendarDate dx = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); 2017 int m = dx.getMonth() - 1; 2018 if (month < m) { 2019 month = m; 2020 } 2021 if (month == m) { 2022 firstDayOfMonth = dx.getDayOfMonth(); 2023 } 2024 } 2025 2026 LocalGregorianCalendar.Date date = jcal.newCalendarDate(TimeZone.NO_TIMEZONE); 2027 date.setEra(era > 0 ? eras[era] : null); 2028 date.setDate(year, month + 1, firstDayOfMonth); 2029 jcal.normalize(date); 2030 2031 // Get the fixed date since Jan 1, 1 (Gregorian). We are on 2032 // the first day of either `month' or January in 'year'. 2033 long fixedDate = jcal.getFixedDate(date); 2034 2035 if (isFieldSet(fieldMask, MONTH)) { 2036 // Month-based calculations 2037 if (isFieldSet(fieldMask, DAY_OF_MONTH)) { 2038 // We are on the "first day" of the month (which may 2039 // not be 1). Just add the offset if DAY_OF_MONTH is 2040 // set. If the isSet call returns false, that means 2041 // DAY_OF_MONTH has been selected just because of the 2042 // selected combination. We don't need to add any 2043 // since the default value is the "first day". 2044 if (isSet(DAY_OF_MONTH)) { 2045 // To avoid underflow with DAY_OF_MONTH-firstDayOfMonth, add 2046 // DAY_OF_MONTH, then subtract firstDayOfMonth. 2047 fixedDate += internalGet(DAY_OF_MONTH); 2048 fixedDate -= firstDayOfMonth; 2049 } 2050 } else { 2051 if (isFieldSet(fieldMask, WEEK_OF_MONTH)) { 2052 long firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6, 2053 getFirstDayOfWeek()); 2054 // If we have enough days in the first week, then 2055 // move to the previous week. 2056 if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) { 2057 firstDayOfWeek -= 7; 2058 } 2059 if (isFieldSet(fieldMask, DAY_OF_WEEK)) { 2060 firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, 2061 internalGet(DAY_OF_WEEK)); 2062 } 2063 // In lenient mode, we treat days of the previous 2064 // months as a part of the specified 2065 // WEEK_OF_MONTH. See 4633646. 2066 fixedDate = firstDayOfWeek + 7 * (internalGet(WEEK_OF_MONTH) - 1); 2067 } else { 2068 int dayOfWeek; 2069 if (isFieldSet(fieldMask, DAY_OF_WEEK)) { 2070 dayOfWeek = internalGet(DAY_OF_WEEK); 2071 } else { 2072 dayOfWeek = getFirstDayOfWeek(); 2073 } 2074 // We are basing this on the day-of-week-in-month. The only 2075 // trickiness occurs if the day-of-week-in-month is 2076 // negative. 2077 int dowim; 2078 if (isFieldSet(fieldMask, DAY_OF_WEEK_IN_MONTH)) { 2079 dowim = internalGet(DAY_OF_WEEK_IN_MONTH); 2080 } else { 2081 dowim = 1; 2082 } 2083 if (dowim >= 0) { 2084 fixedDate = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + (7 * dowim) - 1, 2085 dayOfWeek); 2086 } else { 2087 // Go to the first day of the next week of 2088 // the specified week boundary. 2089 int lastDate = monthLength(month, year) + (7 * (dowim + 1)); 2090 // Then, get the day of week date on or before the last date. 2091 fixedDate = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1, 2092 dayOfWeek); 2093 } 2094 } 2095 } 2096 } else { 2097 // We are on the first day of the year. 2098 if (isFieldSet(fieldMask, DAY_OF_YEAR)) { 2099 if (isTransitionYear(date.getNormalizedYear())) { 2100 fixedDate = getFixedDateJan1(date, fixedDate); 2101 } 2102 // Add the offset, then subtract 1. (Make sure to avoid underflow.) 2103 fixedDate += internalGet(DAY_OF_YEAR); 2104 fixedDate--; 2105 } else { 2106 long firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6, 2107 getFirstDayOfWeek()); 2108 // If we have enough days in the first week, then move 2109 // to the previous week. 2110 if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) { 2111 firstDayOfWeek -= 7; 2112 } 2113 if (isFieldSet(fieldMask, DAY_OF_WEEK)) { 2114 int dayOfWeek = internalGet(DAY_OF_WEEK); 2115 if (dayOfWeek != getFirstDayOfWeek()) { 2116 firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, 2117 dayOfWeek); 2118 } 2119 } 2120 fixedDate = firstDayOfWeek + 7 * ((long)internalGet(WEEK_OF_YEAR) - 1); 2121 } 2122 } 2123 return fixedDate; 2124 } 2125 2126 /** 2127 * Returns the fixed date of the first day of the year (usually 2128 * January 1) before the specified date. 2129 * 2130 * @param date the date for which the first day of the year is 2131 * calculated. The date has to be in the cut-over year. 2132 * @param fixedDate the fixed date representation of the date 2133 */ getFixedDateJan1(LocalGregorianCalendar.Date date, long fixedDate)2134 private long getFixedDateJan1(LocalGregorianCalendar.Date date, long fixedDate) { 2135 Era era = date.getEra(); 2136 if (date.getEra() != null && date.getYear() == 1) { 2137 for (int eraIndex = getEraIndex(date); eraIndex > 0; eraIndex--) { 2138 CalendarDate d = eras[eraIndex].getSinceDate(); 2139 long fd = gcal.getFixedDate(d); 2140 // There might be multiple era transitions in a year. 2141 if (fd > fixedDate) { 2142 continue; 2143 } 2144 return fd; 2145 } 2146 } 2147 CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE); 2148 d.setDate(date.getNormalizedYear(), Gregorian.JANUARY, 1); 2149 return gcal.getFixedDate(d); 2150 } 2151 2152 /** 2153 * Returns the fixed date of the first date of the month (usually 2154 * the 1st of the month) before the specified date. 2155 * 2156 * @param date the date for which the first day of the month is 2157 * calculated. The date must be in the era transition year. 2158 * @param fixedDate the fixed date representation of the date 2159 */ getFixedDateMonth1(LocalGregorianCalendar.Date date, long fixedDate)2160 private long getFixedDateMonth1(LocalGregorianCalendar.Date date, 2161 long fixedDate) { 2162 int eraIndex = getTransitionEraIndex(date); 2163 if (eraIndex != -1) { 2164 long transition = sinceFixedDates[eraIndex]; 2165 // If the given date is on or after the transition date, then 2166 // return the transition date. 2167 if (transition <= fixedDate) { 2168 return transition; 2169 } 2170 } 2171 2172 // Otherwise, we can use the 1st day of the month. 2173 return fixedDate - date.getDayOfMonth() + 1; 2174 } 2175 2176 /** 2177 * Returns a LocalGregorianCalendar.Date produced from the specified fixed date. 2178 * 2179 * @param fd the fixed date 2180 */ getCalendarDate(long fd)2181 private static LocalGregorianCalendar.Date getCalendarDate(long fd) { 2182 LocalGregorianCalendar.Date d = jcal.newCalendarDate(TimeZone.NO_TIMEZONE); 2183 jcal.getCalendarDateFromFixedDate(d, fd); 2184 return d; 2185 } 2186 2187 /** 2188 * Returns the length of the specified month in the specified 2189 * Gregorian year. The year number must be normalized. 2190 * 2191 * @see GregorianCalendar#isLeapYear(int) 2192 */ monthLength(int month, int gregorianYear)2193 private int monthLength(int month, int gregorianYear) { 2194 return CalendarUtils.isGregorianLeapYear(gregorianYear) ? 2195 GregorianCalendar.LEAP_MONTH_LENGTH[month] : GregorianCalendar.MONTH_LENGTH[month]; 2196 } 2197 2198 /** 2199 * Returns the length of the specified month in the year provided 2200 * by internalGet(YEAR). 2201 * 2202 * @see GregorianCalendar#isLeapYear(int) 2203 */ monthLength(int month)2204 private int monthLength(int month) { 2205 assert jdate.isNormalized(); 2206 return jdate.isLeapYear() ? 2207 GregorianCalendar.LEAP_MONTH_LENGTH[month] : GregorianCalendar.MONTH_LENGTH[month]; 2208 } 2209 actualMonthLength()2210 private int actualMonthLength() { 2211 int length = jcal.getMonthLength(jdate); 2212 int eraIndex = getTransitionEraIndex(jdate); 2213 if (eraIndex == -1) { 2214 long transitionFixedDate = sinceFixedDates[eraIndex]; 2215 CalendarDate d = eras[eraIndex].getSinceDate(); 2216 if (transitionFixedDate <= cachedFixedDate) { 2217 length -= d.getDayOfMonth() - 1; 2218 } else { 2219 length = d.getDayOfMonth() - 1; 2220 } 2221 } 2222 return length; 2223 } 2224 2225 /** 2226 * Returns the index to the new era if the given date is in a 2227 * transition month. For example, if the give date is Heisei 1 2228 * (1989) January 20, then the era index for Heisei is 2229 * returned. Likewise, if the given date is Showa 64 (1989) 2230 * January 3, then the era index for Heisei is returned. If the 2231 * given date is not in any transition month, then -1 is returned. 2232 */ getTransitionEraIndex(LocalGregorianCalendar.Date date)2233 private static int getTransitionEraIndex(LocalGregorianCalendar.Date date) { 2234 int eraIndex = getEraIndex(date); 2235 CalendarDate transitionDate = eras[eraIndex].getSinceDate(); 2236 if (transitionDate.getYear() == date.getNormalizedYear() && 2237 transitionDate.getMonth() == date.getMonth()) { 2238 return eraIndex; 2239 } 2240 if (eraIndex < eras.length - 1) { 2241 transitionDate = eras[++eraIndex].getSinceDate(); 2242 if (transitionDate.getYear() == date.getNormalizedYear() && 2243 transitionDate.getMonth() == date.getMonth()) { 2244 return eraIndex; 2245 } 2246 } 2247 return -1; 2248 } 2249 isTransitionYear(int normalizedYear)2250 private boolean isTransitionYear(int normalizedYear) { 2251 for (int i = eras.length - 1; i > 0; i--) { 2252 int transitionYear = eras[i].getSinceDate().getYear(); 2253 if (normalizedYear == transitionYear) { 2254 return true; 2255 } 2256 if (normalizedYear > transitionYear) { 2257 break; 2258 } 2259 } 2260 return false; 2261 } 2262 getEraIndex(LocalGregorianCalendar.Date date)2263 private static int getEraIndex(LocalGregorianCalendar.Date date) { 2264 Era era = date.getEra(); 2265 for (int i = eras.length - 1; i > 0; i--) { 2266 if (eras[i] == era) { 2267 return i; 2268 } 2269 } 2270 return 0; 2271 } 2272 2273 /** 2274 * Returns this object if it's normalized (all fields and time are 2275 * in sync). Otherwise, a cloned object is returned after calling 2276 * complete() in lenient mode. 2277 */ getNormalizedCalendar()2278 private JapaneseImperialCalendar getNormalizedCalendar() { 2279 JapaneseImperialCalendar jc; 2280 if (isFullyNormalized()) { 2281 jc = this; 2282 } else { 2283 // Create a clone and normalize the calendar fields 2284 jc = (JapaneseImperialCalendar) this.clone(); 2285 jc.setLenient(true); 2286 jc.complete(); 2287 } 2288 return jc; 2289 } 2290 2291 /** 2292 * After adjustments such as add(MONTH), add(YEAR), we don't want the 2293 * month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar 2294 * 3, we want it to go to Feb 28. Adjustments which might run into this 2295 * problem call this method to retain the proper month. 2296 */ pinDayOfMonth(LocalGregorianCalendar.Date date)2297 private void pinDayOfMonth(LocalGregorianCalendar.Date date) { 2298 int year = date.getYear(); 2299 int dom = date.getDayOfMonth(); 2300 if (year != getMinimum(YEAR)) { 2301 date.setDayOfMonth(1); 2302 jcal.normalize(date); 2303 int monthLength = jcal.getMonthLength(date); 2304 if (dom > monthLength) { 2305 date.setDayOfMonth(monthLength); 2306 } else { 2307 date.setDayOfMonth(dom); 2308 } 2309 jcal.normalize(date); 2310 } else { 2311 LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MIN_VALUE, getZone()); 2312 LocalGregorianCalendar.Date realDate = jcal.getCalendarDate(time, getZone()); 2313 long tod = realDate.getTimeOfDay(); 2314 // Use an equivalent year. 2315 realDate.addYear(+400); 2316 realDate.setMonth(date.getMonth()); 2317 realDate.setDayOfMonth(1); 2318 jcal.normalize(realDate); 2319 int monthLength = jcal.getMonthLength(realDate); 2320 if (dom > monthLength) { 2321 realDate.setDayOfMonth(monthLength); 2322 } else { 2323 if (dom < d.getDayOfMonth()) { 2324 realDate.setDayOfMonth(d.getDayOfMonth()); 2325 } else { 2326 realDate.setDayOfMonth(dom); 2327 } 2328 } 2329 if (realDate.getDayOfMonth() == d.getDayOfMonth() && tod < d.getTimeOfDay()) { 2330 realDate.setDayOfMonth(Math.min(dom + 1, monthLength)); 2331 } 2332 // restore the year. 2333 date.setDate(year, realDate.getMonth(), realDate.getDayOfMonth()); 2334 // Don't normalize date here so as not to cause underflow. 2335 } 2336 } 2337 2338 /** 2339 * Returns the new value after 'roll'ing the specified value and amount. 2340 */ getRolledValue(int value, int amount, int min, int max)2341 private static int getRolledValue(int value, int amount, int min, int max) { 2342 assert value >= min && value <= max; 2343 int range = max - min + 1; 2344 amount %= range; 2345 int n = value + amount; 2346 if (n > max) { 2347 n -= range; 2348 } else if (n < min) { 2349 n += range; 2350 } 2351 assert n >= min && n <= max; 2352 return n; 2353 } 2354 2355 /** 2356 * Returns the ERA. We need a special method for this because the 2357 * default ERA is the current era, but a zero (unset) ERA means before Meiji. 2358 */ internalGetEra()2359 private int internalGetEra() { 2360 return isSet(ERA) ? internalGet(ERA) : currentEra; 2361 } 2362 2363 /** 2364 * Updates internal state. 2365 */ readObject(ObjectInputStream stream)2366 private void readObject(ObjectInputStream stream) 2367 throws IOException, ClassNotFoundException { 2368 stream.defaultReadObject(); 2369 if (jdate == null) { 2370 jdate = jcal.newCalendarDate(getZone()); 2371 cachedFixedDate = Long.MIN_VALUE; 2372 } 2373 } 2374 } 2375