2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.util;
9 import java.util.Locale;
12 * <code>JapaneseCalendar</code> is a subclass of <code>GregorianCalendar</code>
13 * that numbers years and eras based on the reigns of the Japanese emperors.
14 * The Japanese calendar is identical to the Gregorian calendar in all respects
15 * except for the year and era. The ascension of each emperor to the throne
16 * begins a new era, and the years of that era are numbered starting with the
17 * year of ascension as year 1.
19 * Note that in the year of an imperial ascension, there are two possible sets
20 * of year and era values: that for the old era and for the new. For example, a
21 * new era began on January 7, 1989 AD. Strictly speaking, the first six days
22 * of that year were in the Showa era, e.g. "January 6, 64 Showa", while the rest
23 * of the year was in the Heisei era, e.g. "January 7, 1 Heisei". This class
24 * handles this distinction correctly when computing dates. However, in lenient
25 * mode either form of date is acceptable as input.
27 * In modern times, eras have started on January 8, 1868 AD, Gregorian (Meiji),
28 * July 30, 1912 (Taisho), December 25, 1926 (Showa), and January 7, 1989 (Heisei). Constants
29 * for these eras, suitable for use in the <code>ERA</code> field, are provided
30 * in this class. Note that the <em>number</em> used for each era is more or
31 * less arbitrary. Currently, the era starting in 1053 AD is era #0; however this
32 * may change in the future as we add more historical data. Use the predefined
33 * constants rather than using actual, absolute numbers.
35 * This class should not be subclassed.</p>
37 * JapaneseCalendar usually should be instantiated using
38 * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code>
39 * with the tag <code>"@calendar=japanese"</code>.</p>
41 * @see com.ibm.icu.util.GregorianCalendar
42 * @see com.ibm.icu.util.Calendar
44 * @author Laura Werner
48 public class JapaneseCalendar extends GregorianCalendar {
50 private static final long serialVersionUID = -2977189902603704691L;
52 //-------------------------------------------------------------------------
54 //-------------------------------------------------------------------------
57 * Constructs a default <code>JapaneseCalendar</code> using the current time
58 * in the default time zone with the default locale.
61 public JapaneseCalendar() {
66 * Constructs a <code>JapaneseCalendar</code> based on the current time
67 * in the given time zone with the default locale.
68 * @param zone the given time zone.
71 public JapaneseCalendar(TimeZone zone) {
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.
81 public JapaneseCalendar(Locale aLocale) {
86 * Constructs a <code>JapaneseCalendar</code> based on the current time
87 * in the default time zone with the given locale.
88 * @param locale the given ulocale.
91 public JapaneseCalendar(ULocale locale) {
96 * Constructs a <code>JapaneseCalendar</code> based on the current time
97 * in the given time zone with the given locale.
99 * @param zone the given time zone.
101 * @param aLocale the given locale.
104 public JapaneseCalendar(TimeZone zone, Locale aLocale) {
105 super(zone, aLocale);
109 * Constructs a <code>JapaneseCalendar</code> based on the current time
110 * in the given time zone with the given locale.
112 * @param zone the given time zone.
114 * @param locale the given ulocale.
117 public JapaneseCalendar(TimeZone zone, ULocale locale) {
122 * Constructs a <code>JapaneseCalendar</code> with the given date set
123 * in the default time zone with the default locale.
125 * @param date The date to which the new calendar is set.
128 public JapaneseCalendar(Date date) {
134 * Constructs a <code>JapaneseCalendar</code> with the given date set
135 * in the default time zone with the default locale.
137 * @param era The imperial era used to set the calendar's {@link #ERA ERA} field.
138 * Eras are numbered starting with the Tenki era, which
139 * began in 1053 AD Gregorian, as era zero. Recent
140 * eras can be specified using the constants
141 * {@link #MEIJI} (which started in 1868 AD),
142 * {@link #TAISHO} (1912 AD),
143 * {@link #SHOWA} (1926 AD), and
144 * {@link #HEISEI} (1989 AD).
146 * @param year The value used to set the calendar's {@link #YEAR YEAR} field,
147 * in terms of the era.
149 * @param month The value used to set the calendar's {@link #MONTH MONTH} field.
150 * The value is 0-based. e.g., 0 for January.
152 * @param date The value used to set the calendar's DATE field.
155 public JapaneseCalendar(int era, int year, int month, int date) {
156 super(year, month, date);
161 * Constructs a <code>JapaneseCalendar</code> with the given date set
162 * in the default time zone with the default locale.
164 * @param year The value used to set the calendar's {@link #YEAR YEAR} field,
165 * in the era Heisei, the most current at the time this
166 * class was last updated.
168 * @param month The value used to set the calendar's {@link #MONTH MONTH} field.
169 * The value is 0-based. e.g., 0 for January.
171 * @param date The value used to set the calendar's {@link #DATE DATE} field.
174 public JapaneseCalendar(int year, int month, int date) {
175 super(year, month, date);
176 set(ERA, CURRENT_ERA);
180 * Constructs a <code>JapaneseCalendar</code> with the given date
181 * and time set for the default time zone with the default locale.
183 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field,
184 * in the era Heisei, the most current at the time of this
187 * @param month The value used to set the calendar's {@link #MONTH MONTH} time field.
188 * The value is 0-based. e.g., 0 for January.
190 * @param date The value used to set the calendar's {@link #DATE DATE} time field.
192 * @param hour The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field.
194 * @param minute The value used to set the calendar's {@link #MINUTE MINUTE} time field.
196 * @param second The value used to set the calendar's {@link #SECOND SECOND} time field.
199 public JapaneseCalendar(int year, int month, int date, int hour,
200 int minute, int second)
202 super(year, month, date, hour, minute, second);
203 set(ERA, CURRENT_ERA);
206 //-------------------------------------------------------------------------
208 // Use 1970 as the default value of EXTENDED_YEAR
209 private static final int GREGORIAN_EPOCH = 1970;
214 protected int handleGetExtendedYear() {
215 // EXTENDED_YEAR in JapaneseCalendar is a Gregorian year
216 // The default value of EXTENDED_YEAR is 1970 (Showa 45)
218 if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR &&
219 newerField(EXTENDED_YEAR, ERA) == EXTENDED_YEAR) {
220 year = internalGet(EXTENDED_YEAR, GREGORIAN_EPOCH);
222 // extended year is a gregorian year, where 1 = 1AD, 0 = 1BC, -1 = 2BC, etc
223 year = internalGet(YEAR, 1) // pin to minimum of year 1 (first year)
224 + ERAS[internalGet(ERA, CURRENT_ERA) * 3] // add gregorian starting year
225 - 1; // Subtract one because year starts at 1
231 * Called by handleComputeJulianDay. Returns the default month (0-based) for the year,
232 * taking year and era into account. Defaults to 0 (JANUARY) for Gregorian.
233 * @param extendedYear the extendedYear, as returned by handleGetExtendedYear
234 * @return the default month
235 * @provisional ICU 3.6
236 * @draft ICU 3.6 (retain)
239 protected int getDefaultMonthInYear(int extendedYear)
241 int era = internalGet(ERA, CURRENT_ERA);
242 //computeFields(status); // No need to compute fields here - expect the caller already did so.
244 // Find out if we are at the edge of an era
245 if(extendedYear == ERAS[era*3]) {
246 return ERAS[(era*3)+1] // month..
247 -1; // return 0-based month
249 return super.getDefaultMonthInYear(extendedYear);
254 * Called by handleComputeJulianDay. Returns the default day (1-based) for the month,
255 * taking currently-set year and era into account. Defaults to 1 for Gregorian.
256 * @param extendedYear the extendedYear, as returned by handleGetExtendedYear
257 * @param month the month, as returned by getDefaultMonthInYear
258 * @return the default day of the month
259 * @draft ICU 3.6 (retain)
260 * @provisional ICU 3.6
263 protected int getDefaultDayInMonth(int extendedYear, int month) {
264 int era = internalGet(ERA, CURRENT_ERA);
266 if(extendedYear == ERAS[era*3]) { // if it is year 1..
267 if(month == ((ERAS[(era*3)+1])-1)) { // if it is the emperor's first month..
268 return ERAS[(era*3)+2]; // return the D_O_M of acession
272 return super.getDefaultDayInMonth(extendedYear, month);
278 protected void handleComputeFields(int julianDay) {
279 super.handleComputeFields(julianDay);
280 int year = internalGet(EXTENDED_YEAR);
284 // Short circuit for recent years. Most modern computations will
285 // occur in the current era and won't require the binary search.
286 // Note that if the year is == the current era year, then we use
287 // the binary search to handle the month/dom comparison.
288 if (year > ERAS[ERAS.length - 3]) {
292 int high = ERAS.length / 3;
294 while (low < high - 1) {
295 int i = (low + high) / 2;
296 int diff = year - ERAS[i*3];
298 // If years are the same, then compare the months, and if those
299 // are the same, compare days of month. In the ERAS array
300 // months are 1-based for easier maintenance.
302 diff = internalGet(MONTH) - (ERAS[i*3 + 1] - 1);
304 diff = internalGet(DAY_OF_MONTH) - ERAS[i*3 + 2];
315 // Now we've found the last era that starts before this date, so
316 // adjust the year to count from the start of that era. Note that
317 // all dates before the first era will fall into the first era by
319 internalSet(ERA, low);
320 internalSet(YEAR, year - ERAS[low*3] + 1);
323 private static final int[] ERAS = {
324 // Gregorian date of each emperor's ascension
325 // Years are AD, months are 1-based.
328 650, 2, 15, // Hakuchi
330 686, 7, 20, // Shucho
338 749, 4, 14, // Tempyo-kampo
339 749, 7, 2, // Tempyo-shoho
340 757, 8, 18, // Tempyo-hoji
341 765, 1, 7, // Tempho-jingo
342 767, 8, 16, // Jingo-keiun
345 782, 8, 19, // Enryaku
352 854, 11, 30, // Saiko
353 857, 2, 21, // Tennan
355 877, 4, 16, // Genkei
357 889, 4, 27, // Kampyo
358 898, 4, 26, // Shotai
361 931, 4, 26, // Shohei
362 938, 5, 22, // Tengyo
363 947, 4, 22, // Tenryaku
364 957, 10, 27, // Tentoku
368 970, 3, 25, // Tenroku
369 973, 12, 20, // Ten-en
371 978, 11, 29, // Tengen
376 990, 11, 7, // Shoryaku
377 995, 2, 22, // Chotoku
379 1004, 7, 20, // Kanko
380 1012, 12, 25, // Chowa
381 1017, 4, 23, // Kannin
383 1024, 7, 13, // Manju
384 1028, 7, 25, // Chogen
385 1037, 4, 21, // Choryaku
386 1040, 11, 10, // Chokyu
387 1044, 11, 24, // Kantoku
388 1046, 4, 14, // Eisho
389 1053, 1, 11, // Tengi
390 1058, 8, 29, // Kohei
391 1065, 8, 2, // Jiryaku
392 1069, 4, 13, // Enkyu
393 1074, 8, 23, // Shoho
394 1077, 11, 17, // Shoryaku
398 1094, 12, 15, // Kaho
399 1096, 12, 17, // Eicho
400 1097, 11, 21, // Shotoku
402 1104, 2, 10, // Choji
404 1108, 8, 3, // Tennin
405 1110, 7, 13, // Ten-ei
406 1113, 7, 13, // Eikyu
407 1118, 4, 3, // Gen-ei
410 1126, 1, 22, // Daiji
411 1131, 1, 29, // Tensho
412 1132, 8, 11, // Chosho
416 1144, 2, 23, // Tenyo
417 1145, 7, 22, // Kyuan
418 1151, 1, 26, // Ninpei
419 1154, 10, 28, // Kyuju
420 1156, 4, 27, // Hogen
421 1159, 4, 20, // Heiji
422 1160, 1, 10, // Eiryaku
424 1163, 3, 29, // Chokan
426 1166, 8, 27, // Nin-an
428 1171, 4, 21, // Shoan
429 1175, 7, 28, // Angen
433 1184, 4, 16, // Genryuku
434 1185, 8, 14, // Bunji
435 1190, 4, 11, // Kenkyu
436 1199, 4, 27, // Shoji
437 1201, 2, 13, // Kennin
438 1204, 2, 20, // Genkyu
439 1206, 4, 27, // Ken-ei
440 1207, 10, 25, // Shogen
441 1211, 3, 9, // Kenryaku
442 1213, 12, 6, // Kenpo
443 1219, 4, 12, // Shokyu
445 1224, 11, 20, // Gennin
446 1225, 4, 20, // Karoku
447 1227, 12, 10, // Antei
450 1233, 4, 15, // Tempuku
451 1234, 11, 5, // Bunryaku
452 1235, 9, 19, // Katei
453 1238, 11, 23, // Ryakunin
455 1240, 7, 16, // Ninji
456 1243, 2, 26, // Kangen
458 1249, 3, 18, // Kencho
459 1256, 10, 5, // Kogen
460 1257, 3, 14, // Shoka
461 1259, 3, 26, // Shogen
462 1260, 4, 13, // Bun-o
463 1261, 2, 20, // Kocho
464 1264, 2, 28, // Bun-ei
465 1275, 4, 25, // Kenji
468 1293, 8, 55, // Einin
469 1299, 4, 25, // Shoan
470 1302, 11, 21, // Kengen
472 1306, 12, 14, // Tokuji
473 1308, 10, 9, // Enkei
475 1312, 3, 20, // Showa
478 1321, 2, 23, // Genkyo
479 1324, 12, 9, // Shochu
480 1326, 4, 26, // Kareki
481 1329, 8, 29, // Gentoku
483 1334, 1, 29, // Kemmu
484 1336, 2, 29, // Engen
485 1340, 4, 28, // Kokoku
486 1346, 12, 8, // Shohei
487 1370, 7, 24, // Kentoku
488 1372, 4, 1, // Bunch\u0169
489 1375, 5, 27, // Tenju
490 1379, 3, 22, // Koryaku
492 1384, 4, 28, // Gench\u0169
493 1384, 2, 27, // Meitoku
494 1387, 8, 23, // Kakei
496 1390, 3, 26, // Meitoku
498 1428, 4, 27, // Shocho
500 1441, 2, 17, // Kakitsu
501 1444, 2, 5, // Bun-an
502 1449, 7, 28, // Hotoku
503 1452, 7, 25, // Kyotoku
504 1455, 7, 25, // Kosho
505 1457, 9, 28, // Choroku
506 1460, 12, 21, // Kansho
507 1466, 2, 28, // Bunsho
509 1469, 4, 28, // Bunmei
510 1487, 7, 29, // Chokyo
511 1489, 8, 21, // Entoku
513 1501, 2, 29, // Bunki
514 1504, 2, 30, // Eisho
515 1521, 8, 23, // Taiei
516 1528, 8, 20, // Kyoroku
517 1532, 7, 29, // Tenmon
518 1555, 10, 23, // Koji
519 1558, 2, 28, // Eiroku
520 1570, 4, 23, // Genki
521 1573, 7, 28, // Tensho
522 1592, 12, 8, // Bunroku
523 1596, 10, 27, // Keicho
524 1615, 7, 13, // Genwa
525 1624, 2, 30, // Kan-ei
526 1644, 12, 16, // Shoho
527 1648, 2, 15, // Keian
529 1655, 4, 13, // Meiryaku
530 1658, 7, 23, // Manji
531 1661, 4, 25, // Kanbun
533 1681, 9, 29, // Tenwa
534 1684, 2, 21, // Jokyo
535 1688, 9, 30, // Genroku
537 1711, 4, 25, // Shotoku
538 1716, 6, 22, // Kyoho
539 1736, 4, 28, // Genbun
540 1741, 2, 27, // Kanpo
541 1744, 2, 21, // Enkyo
542 1748, 7, 12, // Kan-en
543 1751, 10, 27, // Horyaku
545 1772, 11, 16, // An-ei
546 1781, 4, 2, // Tenmei
547 1789, 1, 25, // Kansei
549 1804, 2, 11, // Bunka
550 1818, 4, 22, // Bunsei
551 1830, 12, 10, // Tenpo
554 1854, 11, 27, // Ansei
555 1860, 3, 18, // Man-en
556 1861, 2, 19, // Bunkyu
557 1864, 2, 20, // Genji
560 1912, 7, 30, // Taisho
561 1926, 12, 25, // Showa
562 1989, 1, 8, // Heisei
565 //-------------------------------------------------------------------------
566 // Public constants for some of the recent eras that folks might use...
567 //-------------------------------------------------------------------------
569 // Constant for the current era. This must be regularly updated.
573 static public final int CURRENT_ERA = (ERAS.length / 3) - 1;
576 * Constant for the era starting on Sept. 8, 1868 AD.
579 static public final int MEIJI = CURRENT_ERA - 3;
582 * Constant for the era starting on July 30, 1912 AD.
585 static public final int TAISHO = CURRENT_ERA - 2;
588 * Constant for the era starting on Dec. 25, 1926 AD.
591 static public final int SHOWA = CURRENT_ERA - 1;
594 * Constant for the era starting on Jan. 7, 1989 AD.
597 static public final int HEISEI = CURRENT_ERA;
600 * Override GregorianCalendar. We should really handle YEAR_WOY and
601 * EXTENDED_YEAR here too to implement the 1..5000000 range, but it's
605 @SuppressWarnings("fallthrough")
606 protected int handleGetLimit(int field, int limitType) {
609 if (limitType == MINIMUM || limitType == GREATEST_MINIMUM) {
617 case GREATEST_MINIMUM:
622 return super.handleGetLimit(field, MAXIMUM) - ERAS[CURRENT_ERA*3];
624 //Fall through to the default if not handled above
627 return super.handleGetLimit(field, limitType);
635 public String getType() {
643 public int getActualMaximum(int field) {
645 int era = get(Calendar.ERA);
646 if (era == CURRENT_ERA) {
647 // TODO: Investigate what value should be used here - revisit after 4.0.
648 return handleGetLimit(YEAR, MAXIMUM);
650 int nextEraYear = ERAS[(era+1)*3];
651 int nextEraMonth = ERAS[(era+1)*3 + 1];
652 int nextEraDate = ERAS[(era+1)*3 + 2];
654 int maxYear = nextEraYear - ERAS[era*3] + 1; // 1-base
655 if (nextEraMonth == 1 && nextEraDate == 1) {
656 // Substract 1, because the next era starts at Jan 1
662 return super.getActualMaximum(field);