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 */ 10 package android.icu.util; 11 import java.util.Date; 12 import java.util.Locale; 13 14 import android.icu.impl.CalType; 15 import android.icu.impl.EraRules; 16 17 /** 18 * <code>JapaneseCalendar</code> is a subclass of <code>GregorianCalendar</code> 19 * that numbers years and eras based on the reigns of the Japanese emperors. 20 * The Japanese calendar is identical to the Gregorian calendar in all respects 21 * except for the year and era. The ascension of each emperor to the throne 22 * begins a new era, and the years of that era are numbered starting with the 23 * year of ascension as year 1. 24 * <p> 25 * Note that in the year of an imperial ascension, there are two possible sets 26 * of year and era values: that for the old era and for the new. For example, a 27 * new era began on January 7, 1989 AD. Strictly speaking, the first six days 28 * of that year were in the Showa era, e.g. "January 6, 64 Showa", while the rest 29 * of the year was in the Heisei era, e.g. "January 7, 1 Heisei". This class 30 * handles this distinction correctly when computing dates. However, in lenient 31 * mode either form of date is acceptable as input. 32 * <p> 33 * In modern times, eras have started on January 8, 1868 AD, Gregorian (Meiji), 34 * July 30, 1912 (Taisho), December 25, 1926 (Showa), and January 7, 1989 (Heisei). Constants 35 * for these eras, suitable for use in the <code>ERA</code> field, are provided 36 * in this class. Note that the <em>number</em> used for each era is more or 37 * less arbitrary. Currently, the era starting in 645 AD is era #0; however this 38 * may change in the future. Use the predefined constants rather than using actual, 39 * absolute numbers. 40 * <p> 41 * Since ICU4J 63, start date of each era is imported from CLDR. CLDR era data 42 * may contain tentative era in near future with placeholder names. By default, 43 * such era data is not enabled. ICU4J users who want to test the behavior of 44 * the future era can enable this by one of following settings (in the priority 45 * order): 46 * <ol> 47 * <li>Java system property <code>ICU_ENABLE_TENTATIVE_ERA=true</code>.</li> 48 * <li>Environment variable <code>ICU_ENABLE_TENTATIVE_ERA=true</code>.</li> 49 * <li>Java system property <code>jdk.calendar.japanese.supplemental.era=xxx</code>. 50 * (Note: This configuration is used for specifying a new era's start date and 51 * names in OpenJDK. ICU4J implementation enables the CLDR tentative era when 52 * this property is defined, but it does not use the start date and names specified 53 * by the property value.)</li> 54 * </ol> 55 * <p> 56 * This class should not be subclassed.</p> 57 * <p> 58 * JapaneseCalendar usually should be instantiated using 59 * {@link android.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code> 60 * with the tag <code>"@calendar=japanese"</code>.</p> 61 * 62 * @see android.icu.util.GregorianCalendar 63 * @see android.icu.util.Calendar 64 * 65 * @author Laura Werner 66 * @author Alan Liu 67 */ 68 public class JapaneseCalendar extends GregorianCalendar { 69 // jdk1.4.2 serialver 70 private static final long serialVersionUID = -2977189902603704691L; 71 72 //------------------------------------------------------------------------- 73 // Constructors... 74 //------------------------------------------------------------------------- 75 76 /** 77 * Constructs a default <code>JapaneseCalendar</code> using the current time 78 * in the default time zone with the default locale. 79 */ JapaneseCalendar()80 public JapaneseCalendar() { 81 super(); 82 } 83 84 /** 85 * Constructs a <code>JapaneseCalendar</code> based on the current time 86 * in the given time zone with the default locale. 87 * @param zone the given time zone. 88 */ JapaneseCalendar(TimeZone zone)89 public JapaneseCalendar(TimeZone zone) { 90 super(zone); 91 } 92 93 /** 94 * Constructs a <code>JapaneseCalendar</code> based on the current time 95 * in the default time zone with the given locale. 96 * @param aLocale the given locale. 97 */ JapaneseCalendar(Locale aLocale)98 public JapaneseCalendar(Locale aLocale) { 99 super(aLocale); 100 } 101 102 /** 103 * Constructs a <code>JapaneseCalendar</code> based on the current time 104 * in the default time zone with the given locale. 105 * @param locale the given ulocale. 106 */ JapaneseCalendar(ULocale locale)107 public JapaneseCalendar(ULocale locale) { 108 super(locale); 109 } 110 111 /** 112 * Constructs a <code>JapaneseCalendar</code> based on the current time 113 * in the given time zone with the given locale. 114 * 115 * @param zone the given time zone. 116 * 117 * @param aLocale the given locale. 118 */ JapaneseCalendar(TimeZone zone, Locale aLocale)119 public JapaneseCalendar(TimeZone zone, Locale aLocale) { 120 super(zone, aLocale); 121 } 122 123 /** 124 * Constructs a <code>JapaneseCalendar</code> based on the current time 125 * in the given time zone with the given locale. 126 * 127 * @param zone the given time zone. 128 * 129 * @param locale the given ulocale. 130 */ JapaneseCalendar(TimeZone zone, ULocale locale)131 public JapaneseCalendar(TimeZone zone, ULocale locale) { 132 super(zone, locale); 133 } 134 135 /** 136 * Constructs a <code>JapaneseCalendar</code> with the given date set 137 * in the default time zone with the default locale. 138 * 139 * @param date The date to which the new calendar is set. 140 */ JapaneseCalendar(Date date)141 public JapaneseCalendar(Date date) { 142 this(); 143 setTime(date); 144 } 145 146 /** 147 * Constructs a <code>JapaneseCalendar</code> with the given date set 148 * in the default time zone with the default locale. 149 * 150 * @param era The imperial era used to set the calendar's {@link #ERA ERA} field. 151 * Eras are numbered starting with the Tenki era, which 152 * began in 1053 AD Gregorian, as era zero. Recent 153 * eras can be specified using the constants 154 * {@link #MEIJI} (which started in 1868 AD), 155 * {@link #TAISHO} (1912 AD), 156 * {@link #SHOWA} (1926 AD), and 157 * {@link #HEISEI} (1989 AD). 158 * 159 * @param year The value used to set the calendar's {@link #YEAR YEAR} field, 160 * in terms of the era. 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 DATE field. 166 */ JapaneseCalendar(int era, int year, int month, int date)167 public JapaneseCalendar(int era, int year, int month, int date) { 168 super(year, month, date); 169 set(ERA, era); 170 } 171 172 /** 173 * Constructs a <code>JapaneseCalendar</code> with the given date set 174 * in the default time zone with the default locale. 175 * 176 * @param year The value used to set the calendar's {@link #YEAR YEAR} field, 177 * in the era Heisei, the most current at the time this 178 * class was last updated. 179 * 180 * @param month The value used to set the calendar's {@link #MONTH MONTH} 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} field. 184 */ JapaneseCalendar(int year, int month, int date)185 public JapaneseCalendar(int year, int month, int date) { 186 super(year, month, date); 187 set(ERA, CURRENT_ERA); 188 } 189 190 /** 191 * Constructs a <code>JapaneseCalendar</code> with the given date 192 * and time set for the default time zone with the default locale. 193 * 194 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field, 195 * in the era Heisei, the most current at the time of this 196 * writing. 197 * 198 * @param month The value used to set the calendar's {@link #MONTH MONTH} time field. 199 * The value is 0-based. e.g., 0 for January. 200 * 201 * @param date The value used to set the calendar's {@link #DATE DATE} time field. 202 * 203 * @param hour The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field. 204 * 205 * @param minute The value used to set the calendar's {@link #MINUTE MINUTE} time field. 206 * 207 * @param second The value used to set the calendar's {@link #SECOND SECOND} time field. 208 */ JapaneseCalendar(int year, int month, int date, int hour, int minute, int second)209 public JapaneseCalendar(int year, int month, int date, int hour, 210 int minute, int second) 211 { 212 super(year, month, date, hour, minute, second); 213 set(ERA, CURRENT_ERA); 214 } 215 216 //------------------------------------------------------------------------- 217 218 // Use 1970 as the default value of EXTENDED_YEAR 219 private static final int GREGORIAN_EPOCH = 1970; 220 221 private static final EraRules ERA_RULES; 222 223 static { 224 // Although start date of next Japanese era is planned ahead, a name of 225 // new era might not be available. This implementation allows tester to 226 // check a new era without era names by settings below (in priority order). 227 // By default, such tentative era is disabled. 228 // 229 // 1. System property ICU_ENABLE_TENTATIVE_ERA=true or false 230 // 2. Environment variable ICU_ENABLE_TENTATIVE_ERA=true or false 231 // 3. Java system property - jdk.calendar.japanese.supplemental.era for Japanese 232 // 233 // Note: Java system property specifies the start date of new Japanese era, 234 // but this implementation always uses the date read from ICU data. 235 236 boolean includeTentativeEra = false; 237 238 final String VAR_NAME = "ICU_ENABLE_TENTATIVE_ERA"; 239 240 String eraConf = System.getProperty(VAR_NAME); 241 if (eraConf == null) { 242 eraConf = System.getenv(VAR_NAME); 243 } 244 245 if (eraConf != null) { 246 includeTentativeEra = eraConf.equalsIgnoreCase("true"); 247 } else { 248 String jdkEraConf = System.getProperty("jdk.calendar.japanese.supplemental.era"); 249 includeTentativeEra = jdkEraConf != null; 250 } 251 252 ERA_RULES = EraRules.getInstance(CalType.JAPANESE, includeTentativeEra); 253 } 254 255 /** 256 */ 257 @Override handleGetExtendedYear()258 protected int handleGetExtendedYear() { 259 // EXTENDED_YEAR in JapaneseCalendar is a Gregorian year 260 // The default value of EXTENDED_YEAR is 1970 (Showa 45) 261 int year; 262 if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR && 263 newerField(EXTENDED_YEAR, ERA) == EXTENDED_YEAR) { 264 year = internalGet(EXTENDED_YEAR, GREGORIAN_EPOCH); 265 } else { 266 // extended year is a gregorian year, where 1 = 1AD, 0 = 1BC, -1 = 2BC, etc 267 year = internalGet(YEAR, 1) // pin to minimum of year 1 (first year) 268 + ERA_RULES.getStartYear(internalGet(ERA, CURRENT_ERA)) // add gregorian starting year 269 - 1; // Subtract one because year starts at 1 270 } 271 return year; 272 } 273 274 /** 275 * Called by handleComputeJulianDay. Returns the default month (0-based) for the year, 276 * taking year and era into account. Defaults to 0 (JANUARY) for Gregorian. 277 * @param extendedYear the extendedYear, as returned by handleGetExtendedYear 278 * @return the default month 279 * @see #MONTH 280 * @hide draft / provisional / internal are hidden on Android 281 */ 282 @Override getDefaultMonthInYear(int extendedYear)283 protected int getDefaultMonthInYear(int extendedYear) { 284 int era = internalGet(ERA, CURRENT_ERA); 285 // computeFields(status); // No need to compute fields here - expect the caller already did so. 286 287 // Find out if we are at the edge of an era 288 int[] eraStart = ERA_RULES.getStartDate(era, null); 289 if (extendedYear == eraStart[0]) { 290 return eraStart[1] // month.. 291 - 1; // return 0-based month 292 } else { 293 return super.getDefaultMonthInYear(extendedYear); 294 } 295 } 296 297 /** 298 * Called by handleComputeJulianDay. Returns the default day (1-based) for the month, 299 * taking currently-set year and era into account. Defaults to 1 for Gregorian. 300 * @param extendedYear the extendedYear, as returned by handleGetExtendedYear 301 * @param month the month, as returned by getDefaultMonthInYear 302 * @return the default day of the month 303 * @see #DAY_OF_MONTH 304 * @hide draft / provisional / internal are hidden on Android 305 */ 306 @Override getDefaultDayInMonth(int extendedYear, int month)307 protected int getDefaultDayInMonth(int extendedYear, int month) { 308 int era = internalGet(ERA, CURRENT_ERA); 309 int[] eraStart = ERA_RULES.getStartDate(era, null); 310 311 if (extendedYear == eraStart[0]) { // if it is year 1.. 312 if (month == (eraStart[1] - 1)) { // if it is the emperor's first month.. 313 return eraStart[2]; // return the D_O_M of accession 314 } 315 } 316 317 return super.getDefaultDayInMonth(extendedYear, month); 318 } 319 320 /** 321 */ 322 @Override handleComputeFields(int julianDay)323 protected void handleComputeFields(int julianDay) { 324 super.handleComputeFields(julianDay); 325 int year = internalGet(EXTENDED_YEAR); 326 int eraIdx = ERA_RULES.getEraIndex(year, internalGet(MONTH) + 1 /* 1-base */, internalGet(DAY_OF_MONTH)); 327 328 internalSet(ERA, eraIdx); 329 internalSet(YEAR, year - ERA_RULES.getStartYear(eraIdx) + 1); 330 } 331 332 //------------------------------------------------------------------------- 333 // Public constants for some of the recent eras that folks might use... 334 //------------------------------------------------------------------------- 335 336 // Constant for the current era. This must be regularly updated. 337 /** 338 * @deprecated on Android but not deprecated in ICU 339 */ 340 @Deprecated 341 static public final int CURRENT_ERA; 342 343 /** 344 * Constant for the era starting on Sept. 8, 1868 AD. 345 */ 346 static public final int MEIJI; 347 348 /** 349 * Constant for the era starting on July 30, 1912 AD. 350 */ 351 static public final int TAISHO; 352 353 /** 354 * Constant for the era starting on Dec. 25, 1926 AD. 355 */ 356 static public final int SHOWA; 357 358 /** 359 * Constant for the era starting on Jan. 7, 1989 AD. 360 */ 361 static public final int HEISEI; 362 363 // Android-changed: Cherry-pick the Reiwa constant from ICU 64. 364 /** 365 * Constant for the era starting on May 1, 2019 AD. 366 * @hide draft / provisional / internal are hidden on Android 367 */ 368 static public final int REIWA; 369 370 // We want to make these era constants initialized in a static initializer 371 // block to prevent javac to inline these values in a consumer code. 372 // By doing so, we can keep better binary compatibility across versions even 373 // these values are changed. 374 static { 375 MEIJI = 232; 376 TAISHO = 233; 377 SHOWA = 234; 378 HEISEI = 235; 379 // Android-changed: Android doesn't use system time to initialize CURRENT_ERA. 380 // Android could initialize this class during device boot with incorrect time, and 381 // all forked process, e.g. app processes, may have incorrect current era. 382 // CURRENT_ERA = ERA_RULES.getCurrentEraIndex(); 383 REIWA = 236; 384 CURRENT_ERA = REIWA; 385 } 386 387 /** 388 * Override GregorianCalendar. We should really handle YEAR_WOY and 389 * EXTENDED_YEAR here too to implement the 1..5000000 range, but it's 390 * not critical. 391 */ 392 @Override 393 @SuppressWarnings("fallthrough") handleGetLimit(int field, int limitType)394 protected int handleGetLimit(int field, int limitType) { 395 switch (field) { 396 case ERA: 397 if (limitType == MINIMUM || limitType == GREATEST_MINIMUM) { 398 return 0; 399 } 400 return CURRENT_ERA; 401 case YEAR: 402 { 403 switch (limitType) { 404 case MINIMUM: 405 case GREATEST_MINIMUM: 406 return 1; 407 case LEAST_MAXIMUM: 408 return 1; 409 case MAXIMUM: 410 return super.handleGetLimit(field, MAXIMUM) - ERA_RULES.getStartYear(CURRENT_ERA); 411 } 412 //Fall through to the default if not handled above 413 } 414 default: 415 return super.handleGetLimit(field, limitType); 416 } 417 } 418 419 /** 420 * {@inheritDoc} 421 */ 422 @Override getType()423 public String getType() { 424 return "japanese"; 425 } 426 427 /** 428 * {@inheritDoc} 429 * @deprecated This API is ICU internal only. 430 * @hide original deprecated declaration 431 * @hide draft / provisional / internal are hidden on Android 432 */ 433 @Override 434 @Deprecated haveDefaultCentury()435 public boolean haveDefaultCentury() { 436 return false; 437 } 438 439 /** 440 * {@inheritDoc} 441 */ 442 @Override getActualMaximum(int field)443 public int getActualMaximum(int field) { 444 if (field == YEAR) { 445 int era = get(Calendar.ERA); 446 if (era == CURRENT_ERA) { 447 // TODO: Investigate what value should be used here - revisit after 4.0. 448 return handleGetLimit(YEAR, MAXIMUM); 449 } else { 450 int[] nextEraStart = ERA_RULES.getStartDate(era + 1, null); 451 int nextEraYear = nextEraStart[0]; 452 int nextEraMonth = nextEraStart[1]; // 1-base 453 int nextEraDate = nextEraStart[2]; 454 455 int maxYear = nextEraYear - ERA_RULES.getStartYear(era) + 1; // 1-base 456 if (nextEraMonth == 1 && nextEraDate == 1) { 457 // Substract 1, because the next era starts at Jan 1 458 maxYear--; 459 } 460 return maxYear; 461 } 462 } 463 return super.getActualMaximum(field); 464 } 465 466 } 467