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 ohos.global.icu.util; 11 import java.util.Date; 12 import java.util.Locale; 13 14 import ohos.global.icu.impl.CalType; 15 import ohos.global.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 ohos.global.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code> 60 * with the tag <code>"@calendar=japanese"</code>.</p> 61 * 62 * @see ohos.global.icu.util.GregorianCalendar 63 * @see ohos.global.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 ERA_RULES = EraRules.getInstance(CalType.JAPANESE, enableTentativeEra()); 225 } 226 227 /** 228 * Check environment variable that enables use of future eras. 229 * @deprecated This API is ICU internal only. 230 * @hide draft / provisional / internal are hidden on OHOS 231 */ 232 @Deprecated enableTentativeEra()233 public static boolean enableTentativeEra() { 234 // Although start date of next Japanese era is planned ahead, a name of 235 // new era might not be available. This implementation allows tester to 236 // check a new era without era names by settings below (in priority order). 237 // By default, such tentative era is disabled. 238 // 239 // 1. System property ICU_ENABLE_TENTATIVE_ERA=true or false 240 // 2. Environment variable ICU_ENABLE_TENTATIVE_ERA=true or false 241 // 3. Java system property - jdk.calendar.japanese.supplemental.era for Japanese 242 // 243 // Note: Java system property specifies the start date of new Japanese era, 244 // but this implementation always uses the date read from ICU data. 245 246 boolean includeTentativeEra = false; 247 248 final String VAR_NAME = "ICU_ENABLE_TENTATIVE_ERA"; 249 250 String eraConf = System.getProperty(VAR_NAME); 251 if (eraConf == null) { 252 eraConf = System.getenv(VAR_NAME); 253 } 254 255 if (eraConf != null) { 256 includeTentativeEra = eraConf.equalsIgnoreCase("true"); 257 } else { 258 String jdkEraConf = System.getProperty("jdk.calendar.japanese.supplemental.era"); 259 includeTentativeEra = jdkEraConf != null; 260 } 261 return includeTentativeEra; 262 } 263 264 /** 265 */ 266 @Override handleGetExtendedYear()267 protected int handleGetExtendedYear() { 268 // EXTENDED_YEAR in JapaneseCalendar is a Gregorian year 269 // The default value of EXTENDED_YEAR is 1970 (Showa 45) 270 int year; 271 if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR && 272 newerField(EXTENDED_YEAR, ERA) == EXTENDED_YEAR) { 273 year = internalGet(EXTENDED_YEAR, GREGORIAN_EPOCH); 274 } else { 275 // extended year is a gregorian year, where 1 = 1AD, 0 = 1BC, -1 = 2BC, etc 276 year = internalGet(YEAR, 1) // pin to minimum of year 1 (first year) 277 + ERA_RULES.getStartYear(internalGet(ERA, CURRENT_ERA)) // add gregorian starting year 278 - 1; // Subtract one because year starts at 1 279 } 280 return year; 281 } 282 283 /** 284 * Called by handleComputeJulianDay. Returns the default month (0-based) for the year, 285 * taking year and era into account. Defaults to 0 (JANUARY) for Gregorian. 286 * @param extendedYear the extendedYear, as returned by handleGetExtendedYear 287 * @return the default month 288 * @see #MONTH 289 * @hide draft / provisional / internal are hidden on OHOS 290 */ 291 @Override getDefaultMonthInYear(int extendedYear)292 protected int getDefaultMonthInYear(int extendedYear) { 293 int era = internalGet(ERA, CURRENT_ERA); 294 // computeFields(status); // No need to compute fields here - expect the caller already did so. 295 296 // Find out if we are at the edge of an era 297 int[] eraStart = ERA_RULES.getStartDate(era, null); 298 if (extendedYear == eraStart[0]) { 299 return eraStart[1] // month.. 300 - 1; // return 0-based month 301 } else { 302 return super.getDefaultMonthInYear(extendedYear); 303 } 304 } 305 306 /** 307 * Called by handleComputeJulianDay. Returns the default day (1-based) for the month, 308 * taking currently-set year and era into account. Defaults to 1 for Gregorian. 309 * @param extendedYear the extendedYear, as returned by handleGetExtendedYear 310 * @param month the month, as returned by getDefaultMonthInYear 311 * @return the default day of the month 312 * @see #DAY_OF_MONTH 313 * @hide draft / provisional / internal are hidden on OHOS 314 */ 315 @Override getDefaultDayInMonth(int extendedYear, int month)316 protected int getDefaultDayInMonth(int extendedYear, int month) { 317 int era = internalGet(ERA, CURRENT_ERA); 318 int[] eraStart = ERA_RULES.getStartDate(era, null); 319 320 if (extendedYear == eraStart[0]) { // if it is year 1.. 321 if (month == (eraStart[1] - 1)) { // if it is the emperor's first month.. 322 return eraStart[2]; // return the D_O_M of accession 323 } 324 } 325 326 return super.getDefaultDayInMonth(extendedYear, month); 327 } 328 329 /** 330 */ 331 @Override handleComputeFields(int julianDay)332 protected void handleComputeFields(int julianDay) { 333 super.handleComputeFields(julianDay); 334 int year = internalGet(EXTENDED_YEAR); 335 int eraIdx = ERA_RULES.getEraIndex(year, internalGet(MONTH) + 1 /* 1-base */, internalGet(DAY_OF_MONTH)); 336 337 internalSet(ERA, eraIdx); 338 internalSet(YEAR, year - ERA_RULES.getStartYear(eraIdx) + 1); 339 } 340 341 //------------------------------------------------------------------------- 342 // Public constants for some of the recent eras that folks might use... 343 //------------------------------------------------------------------------- 344 345 // Constant for the current era. This must be regularly updated. 346 /** 347 */ 348 static public final int CURRENT_ERA; 349 350 /** 351 * Constant for the era starting on Sept. 8, 1868 AD. 352 */ 353 static public final int MEIJI; 354 355 /** 356 * Constant for the era starting on July 30, 1912 AD. 357 */ 358 static public final int TAISHO; 359 360 /** 361 * Constant for the era starting on Dec. 25, 1926 AD. 362 */ 363 static public final int SHOWA; 364 365 /** 366 * Constant for the era starting on Jan. 7, 1989 AD. 367 */ 368 static public final int HEISEI; 369 370 /** 371 * Constant for the era starting on May 1, 2019 AD. 372 */ 373 static public final int REIWA; 374 375 // We want to make these era constants initialized in a static initializer 376 // block to prevent javac to inline these values in a consumer code. 377 // By doing so, we can keep better binary compatibility across versions even 378 // these values are changed. 379 static { 380 MEIJI = 232; 381 TAISHO = 233; 382 SHOWA = 234; 383 HEISEI = 235; 384 REIWA = 236; 385 CURRENT_ERA = ERA_RULES.getCurrentEraIndex(); 386 } 387 388 /** 389 * Override GregorianCalendar. We should really handle YEAR_WOY and 390 * EXTENDED_YEAR here too to implement the 1..5000000 range, but it's 391 * not critical. 392 */ 393 @Override 394 @SuppressWarnings("fallthrough") handleGetLimit(int field, int limitType)395 protected int handleGetLimit(int field, int limitType) { 396 switch (field) { 397 case ERA: 398 if (limitType == MINIMUM || limitType == GREATEST_MINIMUM) { 399 return 0; 400 } 401 return ERA_RULES.getNumberOfEras() - 1; // max known era, not always CURRENT_ERA 402 case YEAR: 403 { 404 switch (limitType) { 405 case MINIMUM: 406 case GREATEST_MINIMUM: 407 return 1; 408 case LEAST_MAXIMUM: 409 return 1; 410 case MAXIMUM: 411 return super.handleGetLimit(field, MAXIMUM) - ERA_RULES.getStartYear(CURRENT_ERA); 412 } 413 //Fall through to the default if not handled above 414 } 415 default: 416 return super.handleGetLimit(field, limitType); 417 } 418 } 419 420 /** 421 * {@inheritDoc} 422 */ 423 @Override getType()424 public String getType() { 425 return "japanese"; 426 } 427 428 /** 429 * {@inheritDoc} 430 * @deprecated This API is ICU internal only. 431 * @hide deprecated on icu4j-org 432 * @hide draft / provisional / internal are hidden on OHOS 433 */ 434 @Override 435 @Deprecated haveDefaultCentury()436 public boolean haveDefaultCentury() { 437 return false; 438 } 439 440 /** 441 * {@inheritDoc} 442 */ 443 @Override getActualMaximum(int field)444 public int getActualMaximum(int field) { 445 if (field == YEAR) { 446 int era = get(Calendar.ERA); 447 if (era == ERA_RULES.getNumberOfEras() - 1) { 448 // TODO: Investigate what value should be used here - revisit after 4.0. 449 return handleGetLimit(YEAR, MAXIMUM); 450 } else { 451 int[] nextEraStart = ERA_RULES.getStartDate(era + 1, null); 452 int nextEraYear = nextEraStart[0]; 453 int nextEraMonth = nextEraStart[1]; // 1-base 454 int nextEraDate = nextEraStart[2]; 455 456 int maxYear = nextEraYear - ERA_RULES.getStartYear(era) + 1; // 1-base 457 if (nextEraMonth == 1 && nextEraDate == 1) { 458 // Substract 1, because the next era starts at Jan 1 459 maxYear--; 460 } 461 return maxYear; 462 } 463 } 464 return super.getActualMaximum(field); 465 } 466 467 } 468