1/* GENERATED SOURCE. DO NOT MODIFY. */ 2// © 2016 and later: Unicode, Inc. and others. 3// License & terms of use: http://www.unicode.org/copyright.html#License 4/* 5 ******************************************************************************* 6 * Copyright (C) 1996-2014, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10package android.icu.util; 11import java.util.Date; 12import java.util.Locale; 13 14/** 15 * <code>JapaneseCalendar</code> is a subclass of <code>GregorianCalendar</code> 16 * that numbers years and eras based on the reigns of the Japanese emperors. 17 * The Japanese calendar is identical to the Gregorian calendar in all respects 18 * except for the year and era. The ascension of each emperor to the throne 19 * begins a new era, and the years of that era are numbered starting with the 20 * year of ascension as year 1. 21 * <p> 22 * Note that in the year of an imperial ascension, there are two possible sets 23 * of year and era values: that for the old era and for the new. For example, a 24 * new era began on January 7, 1989 AD. Strictly speaking, the first six days 25 * of that year were in the Showa era, e.g. "January 6, 64 Showa", while the rest 26 * of the year was in the Heisei era, e.g. "January 7, 1 Heisei". This class 27 * handles this distinction correctly when computing dates. However, in lenient 28 * mode either form of date is acceptable as input. 29 * <p> 30 * In modern times, eras have started on January 8, 1868 AD, Gregorian (Meiji), 31 * July 30, 1912 (Taisho), December 25, 1926 (Showa), and January 7, 1989 (Heisei). Constants 32 * for these eras, suitable for use in the <code>ERA</code> field, are provided 33 * in this class. Note that the <em>number</em> used for each era is more or 34 * less arbitrary. Currently, the era starting in 1053 AD is era #0; however this 35 * may change in the future as we add more historical data. Use the predefined 36 * constants rather than using actual, absolute numbers. 37 * <p> 38 * This class should not be subclassed.</p> 39 * <p> 40 * JapaneseCalendar usually should be instantiated using 41 * {@link android.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code> 42 * with the tag <code>"@calendar=japanese"</code>.</p> 43 * 44 * @see android.icu.util.GregorianCalendar 45 * @see android.icu.util.Calendar 46 * 47 * @author Laura Werner 48 * @author Alan Liu 49 */ 50public class JapaneseCalendar extends GregorianCalendar { 51 // jdk1.4.2 serialver 52 private static final long serialVersionUID = -2977189902603704691L; 53 54 //------------------------------------------------------------------------- 55 // Constructors... 56 //------------------------------------------------------------------------- 57 58 /** 59 * Constructs a default <code>JapaneseCalendar</code> using the current time 60 * in the default time zone with the default locale. 61 */ 62 public JapaneseCalendar() { 63 super(); 64 } 65 66 /** 67 * Constructs a <code>JapaneseCalendar</code> based on the current time 68 * in the given time zone with the default locale. 69 * @param zone the given time zone. 70 */ 71 public JapaneseCalendar(TimeZone zone) { 72 super(zone); 73 } 74 75 /** 76 * Constructs a <code>JapaneseCalendar</code> based on the current time 77 * in the default time zone with the given locale. 78 * @param aLocale the given locale. 79 */ 80 public JapaneseCalendar(Locale aLocale) { 81 super(aLocale); 82 } 83 84 /** 85 * Constructs a <code>JapaneseCalendar</code> based on the current time 86 * in the default time zone with the given locale. 87 * @param locale the given ulocale. 88 */ 89 public JapaneseCalendar(ULocale locale) { 90 super(locale); 91 } 92 93 /** 94 * Constructs a <code>JapaneseCalendar</code> based on the current time 95 * in the given time zone with the given locale. 96 * 97 * @param zone the given time zone. 98 * 99 * @param aLocale the given locale. 100 */ 101 public JapaneseCalendar(TimeZone zone, Locale aLocale) { 102 super(zone, aLocale); 103 } 104 105 /** 106 * Constructs a <code>JapaneseCalendar</code> based on the current time 107 * in the given time zone with the given locale. 108 * 109 * @param zone the given time zone. 110 * 111 * @param locale the given ulocale. 112 */ 113 public JapaneseCalendar(TimeZone zone, ULocale locale) { 114 super(zone, locale); 115 } 116 117 /** 118 * Constructs a <code>JapaneseCalendar</code> with the given date set 119 * in the default time zone with the default locale. 120 * 121 * @param date The date to which the new calendar is set. 122 */ 123 public JapaneseCalendar(Date date) { 124 this(); 125 setTime(date); 126 } 127 128 /** 129 * Constructs a <code>JapaneseCalendar</code> with the given date set 130 * in the default time zone with the default locale. 131 * 132 * @param era The imperial era used to set the calendar's {@link #ERA ERA} field. 133 * Eras are numbered starting with the Tenki era, which 134 * began in 1053 AD Gregorian, as era zero. Recent 135 * eras can be specified using the constants 136 * {@link #MEIJI} (which started in 1868 AD), 137 * {@link #TAISHO} (1912 AD), 138 * {@link #SHOWA} (1926 AD), and 139 * {@link #HEISEI} (1989 AD). 140 * 141 * @param year The value used to set the calendar's {@link #YEAR YEAR} field, 142 * in terms of the era. 143 * 144 * @param month The value used to set the calendar's {@link #MONTH MONTH} field. 145 * The value is 0-based. e.g., 0 for January. 146 * 147 * @param date The value used to set the calendar's DATE field. 148 */ 149 public JapaneseCalendar(int era, int year, int month, int date) { 150 super(year, month, date); 151 set(ERA, era); 152 } 153 154 /** 155 * Constructs a <code>JapaneseCalendar</code> with the given date set 156 * in the default time zone with the default locale. 157 * 158 * @param year The value used to set the calendar's {@link #YEAR YEAR} field, 159 * in the era Heisei, the most current at the time this 160 * class was last updated. 161 * 162 * @param month The value used to set the calendar's {@link #MONTH MONTH} field. 163 * The value is 0-based. e.g., 0 for January. 164 * 165 * @param date The value used to set the calendar's {@link #DATE DATE} field. 166 */ 167 public JapaneseCalendar(int year, int month, int date) { 168 super(year, month, date); 169 set(ERA, CURRENT_ERA); 170 } 171 172 /** 173 * Constructs a <code>JapaneseCalendar</code> with the given date 174 * and time set for the default time zone with the default locale. 175 * 176 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field, 177 * in the era Heisei, the most current at the time of this 178 * writing. 179 * 180 * @param month The value used to set the calendar's {@link #MONTH MONTH} time field. 181 * The value is 0-based. e.g., 0 for January. 182 * 183 * @param date The value used to set the calendar's {@link #DATE DATE} time field. 184 * 185 * @param hour The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field. 186 * 187 * @param minute The value used to set the calendar's {@link #MINUTE MINUTE} time field. 188 * 189 * @param second The value used to set the calendar's {@link #SECOND SECOND} time field. 190 */ 191 public JapaneseCalendar(int year, int month, int date, int hour, 192 int minute, int second) 193 { 194 super(year, month, date, hour, minute, second); 195 set(ERA, CURRENT_ERA); 196 } 197 198 //------------------------------------------------------------------------- 199 200 // Use 1970 as the default value of EXTENDED_YEAR 201 private static final int GREGORIAN_EPOCH = 1970; 202 203 /** 204 */ 205 protected int handleGetExtendedYear() { 206 // EXTENDED_YEAR in JapaneseCalendar is a Gregorian year 207 // The default value of EXTENDED_YEAR is 1970 (Showa 45) 208 int year; 209 if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR && 210 newerField(EXTENDED_YEAR, ERA) == EXTENDED_YEAR) { 211 year = internalGet(EXTENDED_YEAR, GREGORIAN_EPOCH); 212 } else { 213 // extended year is a gregorian year, where 1 = 1AD, 0 = 1BC, -1 = 2BC, etc 214 year = internalGet(YEAR, 1) // pin to minimum of year 1 (first year) 215 + ERAS[internalGet(ERA, CURRENT_ERA) * 3] // add gregorian starting year 216 - 1; // Subtract one because year starts at 1 217 } 218 return year; 219 } 220 221 /** 222 * Called by handleComputeJulianDay. Returns the default month (0-based) for the year, 223 * taking year and era into account. Defaults to 0 (JANUARY) for Gregorian. 224 * @param extendedYear the extendedYear, as returned by handleGetExtendedYear 225 * @return the default month 226 * @see #MONTH 227 * @hide draft / provisional / internal are hidden on Android 228 */ 229 protected int getDefaultMonthInYear(int extendedYear) 230 { 231 int era = internalGet(ERA, CURRENT_ERA); 232 //computeFields(status); // No need to compute fields here - expect the caller already did so. 233 234 // Find out if we are at the edge of an era 235 if(extendedYear == ERAS[era*3]) { 236 return ERAS[(era*3)+1] // month.. 237 -1; // return 0-based month 238 } else { 239 return super.getDefaultMonthInYear(extendedYear); 240 } 241 } 242 243 /** 244 * Called by handleComputeJulianDay. Returns the default day (1-based) for the month, 245 * taking currently-set year and era into account. Defaults to 1 for Gregorian. 246 * @param extendedYear the extendedYear, as returned by handleGetExtendedYear 247 * @param month the month, as returned by getDefaultMonthInYear 248 * @return the default day of the month 249 * @see #DAY_OF_MONTH 250 * @hide draft / provisional / internal are hidden on Android 251 */ 252 protected int getDefaultDayInMonth(int extendedYear, int month) { 253 int era = internalGet(ERA, CURRENT_ERA); 254 255 if(extendedYear == ERAS[era*3]) { // if it is year 1.. 256 if(month == ((ERAS[(era*3)+1])-1)) { // if it is the emperor's first month.. 257 return ERAS[(era*3)+2]; // return the D_O_M of acession 258 } 259 } 260 261 return super.getDefaultDayInMonth(extendedYear, month); 262 } 263 264 /** 265 */ 266 protected void handleComputeFields(int julianDay) { 267 super.handleComputeFields(julianDay); 268 int year = internalGet(EXTENDED_YEAR); 269 270 int low = 0; 271 272 // Short circuit for recent years. Most modern computations will 273 // occur in the current era and won't require the binary search. 274 // Note that if the year is == the current era year, then we use 275 // the binary search to handle the month/dom comparison. 276 if (year > ERAS[ERAS.length - 3]) { 277 low = CURRENT_ERA; 278 } else { 279 // Binary search 280 int high = ERAS.length / 3; 281 282 while (low < high - 1) { 283 int i = (low + high) / 2; 284 int diff = year - ERAS[i*3]; 285 286 // If years are the same, then compare the months, and if those 287 // are the same, compare days of month. In the ERAS array 288 // months are 1-based for easier maintenance. 289 if (diff == 0) { 290 diff = internalGet(MONTH) - (ERAS[i*3 + 1] - 1); 291 if (diff == 0) { 292 diff = internalGet(DAY_OF_MONTH) - ERAS[i*3 + 2]; 293 } 294 } 295 if (diff >= 0) { 296 low = i; 297 } else { 298 high = i; 299 } 300 } 301 } 302 303 // Now we've found the last era that starts before this date, so 304 // adjust the year to count from the start of that era. Note that 305 // all dates before the first era will fall into the first era by 306 // the algorithm. 307 internalSet(ERA, low); 308 internalSet(YEAR, year - ERAS[low*3] + 1); 309 } 310 311 private static final int[] ERAS = { 312 // Gregorian date of each emperor's ascension 313 // Years are AD, months are 1-based. 314 // Year Month Day 315 645, 6, 19, // Taika 316 650, 2, 15, // Hakuchi 317 672, 1, 1, // Hakuho 318 686, 7, 20, // Shucho 319 701, 3, 21, // Taiho 320 704, 5, 10, // Keiun 321 708, 1, 11, // Wado 322 715, 9, 2, // Reiki 323 717, 11, 17, // Yoro 324 724, 2, 4, // Jinki 325 729, 8, 5, // Tempyo 326 749, 4, 14, // Tempyo-kampo 327 749, 7, 2, // Tempyo-shoho 328 757, 8, 18, // Tempyo-hoji 329 765, 1, 7, // Tempho-jingo 330 767, 8, 16, // Jingo-keiun 331 770, 10, 1, // Hoki 332 781, 1, 1, // Ten-o 333 782, 8, 19, // Enryaku 334 806, 5, 18, // Daido 335 810, 9, 19, // Konin 336 824, 1, 5, // Tencho 337 834, 1, 3, // Showa 338 848, 6, 13, // Kajo 339 851, 4, 28, // Ninju 340 854, 11, 30, // Saiko 341 857, 2, 21, // Tennan 342 859, 4, 15, // Jogan 343 877, 4, 16, // Genkei 344 885, 2, 21, // Ninna 345 889, 4, 27, // Kampyo 346 898, 4, 26, // Shotai 347 901, 7, 15, // Engi 348 923, 4, 11, // Encho 349 931, 4, 26, // Shohei 350 938, 5, 22, // Tengyo 351 947, 4, 22, // Tenryaku 352 957, 10, 27, // Tentoku 353 961, 2, 16, // Owa 354 964, 7, 10, // Koho 355 968, 8, 13, // Anna 356 970, 3, 25, // Tenroku 357 973, 12, 20, // Ten-en 358 976, 7, 13, // Jogen 359 978, 11, 29, // Tengen 360 983, 4, 15, // Eikan 361 985, 4, 27, // Kanna 362 987, 4, 5, // Ei-en 363 989, 8, 8, // Eiso 364 990, 11, 7, // Shoryaku 365 995, 2, 22, // Chotoku 366 999, 1, 13, // Choho 367 1004, 7, 20, // Kanko 368 1012, 12, 25, // Chowa 369 1017, 4, 23, // Kannin 370 1021, 2, 2, // Jian 371 1024, 7, 13, // Manju 372 1028, 7, 25, // Chogen 373 1037, 4, 21, // Choryaku 374 1040, 11, 10, // Chokyu 375 1044, 11, 24, // Kantoku 376 1046, 4, 14, // Eisho 377 1053, 1, 11, // Tengi 378 1058, 8, 29, // Kohei 379 1065, 8, 2, // Jiryaku 380 1069, 4, 13, // Enkyu 381 1074, 8, 23, // Shoho 382 1077, 11, 17, // Shoryaku 383 1081, 2, 10, // Eiho 384 1084, 2, 7, // Otoku 385 1087, 4, 7, // Kanji 386 1094, 12, 15, // Kaho 387 1096, 12, 17, // Eicho 388 1097, 11, 21, // Shotoku 389 1099, 8, 28, // Kowa 390 1104, 2, 10, // Choji 391 1106, 4, 9, // Kasho 392 1108, 8, 3, // Tennin 393 1110, 7, 13, // Ten-ei 394 1113, 7, 13, // Eikyu 395 1118, 4, 3, // Gen-ei 396 1120, 4, 10, // Hoan 397 1124, 4, 3, // Tenji 398 1126, 1, 22, // Daiji 399 1131, 1, 29, // Tensho 400 1132, 8, 11, // Chosho 401 1135, 4, 27, // Hoen 402 1141, 7, 10, // Eiji 403 1142, 4, 28, // Koji 404 1144, 2, 23, // Tenyo 405 1145, 7, 22, // Kyuan 406 1151, 1, 26, // Ninpei 407 1154, 10, 28, // Kyuju 408 1156, 4, 27, // Hogen 409 1159, 4, 20, // Heiji 410 1160, 1, 10, // Eiryaku 411 1161, 9, 4, // Oho 412 1163, 3, 29, // Chokan 413 1165, 6, 5, // Eiman 414 1166, 8, 27, // Nin-an 415 1169, 4, 8, // Kao 416 1171, 4, 21, // Shoan 417 1175, 7, 28, // Angen 418 1177, 8, 4, // Jisho 419 1181, 7, 14, // Yowa 420 1182, 5, 27, // Juei 421 1184, 4, 16, // Genryuku 422 1185, 8, 14, // Bunji 423 1190, 4, 11, // Kenkyu 424 1199, 4, 27, // Shoji 425 1201, 2, 13, // Kennin 426 1204, 2, 20, // Genkyu 427 1206, 4, 27, // Ken-ei 428 1207, 10, 25, // Shogen 429 1211, 3, 9, // Kenryaku 430 1213, 12, 6, // Kenpo 431 1219, 4, 12, // Shokyu 432 1222, 4, 13, // Joo 433 1224, 11, 20, // Gennin 434 1225, 4, 20, // Karoku 435 1227, 12, 10, // Antei 436 1229, 3, 5, // Kanki 437 1232, 4, 2, // Joei 438 1233, 4, 15, // Tempuku 439 1234, 11, 5, // Bunryaku 440 1235, 9, 19, // Katei 441 1238, 11, 23, // Ryakunin 442 1239, 2, 7, // En-o 443 1240, 7, 16, // Ninji 444 1243, 2, 26, // Kangen 445 1247, 2, 28, // Hoji 446 1249, 3, 18, // Kencho 447 1256, 10, 5, // Kogen 448 1257, 3, 14, // Shoka 449 1259, 3, 26, // Shogen 450 1260, 4, 13, // Bun-o 451 1261, 2, 20, // Kocho 452 1264, 2, 28, // Bun-ei 453 1275, 4, 25, // Kenji 454 1278, 2, 29, // Koan 455 1288, 4, 28, // Shoo 456 1293, 8, 55, // Einin 457 1299, 4, 25, // Shoan 458 1302, 11, 21, // Kengen 459 1303, 8, 5, // Kagen 460 1306, 12, 14, // Tokuji 461 1308, 10, 9, // Enkei 462 1311, 4, 28, // Ocho 463 1312, 3, 20, // Showa 464 1317, 2, 3, // Bunpo 465 1319, 4, 28, // Geno 466 1321, 2, 23, // Genkyo 467 1324, 12, 9, // Shochu 468 1326, 4, 26, // Kareki 469 1329, 8, 29, // Gentoku 470 1331, 8, 9, // Genko 471 1334, 1, 29, // Kemmu 472 1336, 2, 29, // Engen 473 1340, 4, 28, // Kokoku 474 1346, 12, 8, // Shohei 475 1370, 7, 24, // Kentoku 476 1372, 4, 1, // Bunch\u0169 477 1375, 5, 27, // Tenju 478 1379, 3, 22, // Koryaku 479 1381, 2, 10, // Kowa 480 1384, 4, 28, // Gench\u0169 481 1384, 2, 27, // Meitoku 482 1387, 8, 23, // Kakei 483 1389, 2, 9, // Koo 484 1390, 3, 26, // Meitoku 485 1394, 7, 5, // Oei 486 1428, 4, 27, // Shocho 487 1429, 9, 5, // Eikyo 488 1441, 2, 17, // Kakitsu 489 1444, 2, 5, // Bun-an 490 1449, 7, 28, // Hotoku 491 1452, 7, 25, // Kyotoku 492 1455, 7, 25, // Kosho 493 1457, 9, 28, // Choroku 494 1460, 12, 21, // Kansho 495 1466, 2, 28, // Bunsho 496 1467, 3, 3, // Onin 497 1469, 4, 28, // Bunmei 498 1487, 7, 29, // Chokyo 499 1489, 8, 21, // Entoku 500 1492, 7, 19, // Meio 501 1501, 2, 29, // Bunki 502 1504, 2, 30, // Eisho 503 1521, 8, 23, // Taiei 504 1528, 8, 20, // Kyoroku 505 1532, 7, 29, // Tenmon 506 1555, 10, 23, // Koji 507 1558, 2, 28, // Eiroku 508 1570, 4, 23, // Genki 509 1573, 7, 28, // Tensho 510 1592, 12, 8, // Bunroku 511 1596, 10, 27, // Keicho 512 1615, 7, 13, // Genwa 513 1624, 2, 30, // Kan-ei 514 1644, 12, 16, // Shoho 515 1648, 2, 15, // Keian 516 1652, 9, 18, // Shoo 517 1655, 4, 13, // Meiryaku 518 1658, 7, 23, // Manji 519 1661, 4, 25, // Kanbun 520 1673, 9, 21, // Enpo 521 1681, 9, 29, // Tenwa 522 1684, 2, 21, // Jokyo 523 1688, 9, 30, // Genroku 524 1704, 3, 13, // Hoei 525 1711, 4, 25, // Shotoku 526 1716, 6, 22, // Kyoho 527 1736, 4, 28, // Genbun 528 1741, 2, 27, // Kanpo 529 1744, 2, 21, // Enkyo 530 1748, 7, 12, // Kan-en 531 1751, 10, 27, // Horyaku 532 1764, 6, 2, // Meiwa 533 1772, 11, 16, // An-ei 534 1781, 4, 2, // Tenmei 535 1789, 1, 25, // Kansei 536 1801, 2, 5, // Kyowa 537 1804, 2, 11, // Bunka 538 1818, 4, 22, // Bunsei 539 1830, 12, 10, // Tenpo 540 1844, 12, 2, // Koka 541 1848, 2, 28, // Kaei 542 1854, 11, 27, // Ansei 543 1860, 3, 18, // Man-en 544 1861, 2, 19, // Bunkyu 545 1864, 2, 20, // Genji 546 1865, 4, 7, // Keio 547 1868, 9, 8, // Meiji 548 1912, 7, 30, // Taisho 549 1926, 12, 25, // Showa 550 1989, 1, 8, // Heisei 551 }; 552 553 //------------------------------------------------------------------------- 554 // Public constants for some of the recent eras that folks might use... 555 //------------------------------------------------------------------------- 556 557 // Constant for the current era. This must be regularly updated. 558 /** 559 */ 560 static public final int CURRENT_ERA = (ERAS.length / 3) - 1; 561 562 /** 563 * Constant for the era starting on Sept. 8, 1868 AD. 564 */ 565 static public final int MEIJI = CURRENT_ERA - 3; 566 567 /** 568 * Constant for the era starting on July 30, 1912 AD. 569 */ 570 static public final int TAISHO = CURRENT_ERA - 2; 571 572 /** 573 * Constant for the era starting on Dec. 25, 1926 AD. 574 */ 575 static public final int SHOWA = CURRENT_ERA - 1; 576 577 /** 578 * Constant for the era starting on Jan. 7, 1989 AD. 579 */ 580 static public final int HEISEI = CURRENT_ERA; 581 582 /** 583 * Override GregorianCalendar. We should really handle YEAR_WOY and 584 * EXTENDED_YEAR here too to implement the 1..5000000 range, but it's 585 * not critical. 586 */ 587 @SuppressWarnings("fallthrough") 588 protected int handleGetLimit(int field, int limitType) { 589 switch (field) { 590 case ERA: 591 if (limitType == MINIMUM || limitType == GREATEST_MINIMUM) { 592 return 0; 593 } 594 return CURRENT_ERA; 595 case YEAR: 596 { 597 switch (limitType) { 598 case MINIMUM: 599 case GREATEST_MINIMUM: 600 return 1; 601 case LEAST_MAXIMUM: 602 return 1; 603 case MAXIMUM: 604 return super.handleGetLimit(field, MAXIMUM) - ERAS[CURRENT_ERA*3]; 605 } 606 //Fall through to the default if not handled above 607 } 608 default: 609 return super.handleGetLimit(field, limitType); 610 } 611 } 612 613 /** 614 * {@inheritDoc} 615 */ 616 public String getType() { 617 return "japanese"; 618 } 619 620 /** 621 * {@inheritDoc} 622 * @deprecated This API is ICU internal only. 623 * @hide original deprecated declaration 624 * @hide draft / provisional / internal are hidden on Android 625 */ 626 @Deprecated 627 public boolean haveDefaultCentury() { 628 return false; 629 } 630 631 /** 632 * {@inheritDoc} 633 */ 634 public int getActualMaximum(int field) { 635 if (field == YEAR) { 636 int era = get(Calendar.ERA); 637 if (era == CURRENT_ERA) { 638 // TODO: Investigate what value should be used here - revisit after 4.0. 639 return handleGetLimit(YEAR, MAXIMUM); 640 } else { 641 int nextEraYear = ERAS[(era+1)*3]; 642 int nextEraMonth = ERAS[(era+1)*3 + 1]; 643 int nextEraDate = ERAS[(era+1)*3 + 2]; 644 645 int maxYear = nextEraYear - ERAS[era*3] + 1; // 1-base 646 if (nextEraMonth == 1 && nextEraDate == 1) { 647 // Substract 1, because the next era starts at Jan 1 648 maxYear--; 649 } 650 return maxYear; 651 } 652 } 653 return super.getActualMaximum(field); 654 } 655 656} 657