2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 package com.ibm.icu.util;
10 import java.util.Date;
11 import java.util.Locale;
14 * <code>TaiwanCalendar</code> is a subclass of <code>GregorianCalendar</code>
15 * that numbers years since 1912.
17 * The Taiwan calendar is identical to the Gregorian calendar in all respects
18 * except for the year and era. Years are numbered since 1912 AD (Gregorian).
20 * The Taiwan Calendar has one era: <code>MINGUO</code>.
22 * This class should not be subclassed.</p>
24 * TaiwanCalendar usually should be instantiated using
25 * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code>
26 * with the tag <code>"@calendar=roc"</code>.</p>
28 * @see com.ibm.icu.util.Calendar
29 * @see com.ibm.icu.util.GregorianCalendar
31 * @author Laura Werner
33 * @author Steven R. Loomis
36 public class TaiwanCalendar extends GregorianCalendar {
38 private static final long serialVersionUID = 2583005278132380631L;
40 //-------------------------------------------------------------------------
42 //-------------------------------------------------------------------------
45 * Constant for the Taiwan Era for years before Minguo 1.
46 * Brefore Minuo 1 is Gregorian 1911, Before Minguo 2 is Gregorian 1910
49 * @see com.ibm.icu.util.Calendar#ERA
52 public static final int BEFORE_MINGUO = 0;
55 * Constant for the Taiwan Era for Minguo. Minguo 1 is 1912 in
58 * @see com.ibm.icu.util.Calendar#ERA
61 public static final int MINGUO = 1;
64 * Constructs a <code>TaiwanCalendar</code> using the current time
65 * in the default time zone with the default locale.
68 public TaiwanCalendar() {
73 * Constructs a <code>TaiwanCalendar</code> based on the current time
74 * in the given time zone with the default locale.
76 * @param zone the given time zone.
79 public TaiwanCalendar(TimeZone zone) {
84 * Constructs a <code>TaiwanCalendar</code> based on the current time
85 * in the default time zone with the given locale.
87 * @param aLocale the given locale.
90 public TaiwanCalendar(Locale aLocale) {
95 * Constructs a <code>TaiwanCalendar</code> based on the current time
96 * in the default time zone with the given locale.
98 * @param locale the given ulocale.
101 public TaiwanCalendar(ULocale locale) {
106 * Constructs a <code>TaiwanCalendar</code> based on the current time
107 * in the given time zone with the given locale.
109 * @param zone the given time zone.
113 public TaiwanCalendar(TimeZone zone, Locale aLocale) {
114 super(zone, aLocale);
118 * Constructs a <code>TaiwanCalendar</code> based on the current time
119 * in the given time zone with the given locale.
121 * @param zone the given time zone.
122 * @param locale the given ulocale.
125 public TaiwanCalendar(TimeZone zone, ULocale locale) {
130 * Constructs a <code>TaiwanCalendar</code> with the given date set
131 * in the default time zone with the default locale.
133 * @param date The date to which the new calendar is set.
136 public TaiwanCalendar(Date date) {
142 * Constructs a <code>TaiwanCalendar</code> with the given date set
143 * in the default time zone with the default locale.
145 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
147 * @param month The value used to set the calendar's {@link #MONTH MONTH} time field.
148 * The value is 0-based. e.g., 0 for January.
150 * @param date The value used to set the calendar's {@link #DATE DATE} time field.
153 public TaiwanCalendar(int year, int month, int date) {
154 super(year, month, date);
158 * Constructs a TaiwanCalendar with the given date
159 * and time set for the default time zone with the default locale.
161 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
163 * @param month The value used to set the calendar's {@link #MONTH MONTH} time field.
164 * The value is 0-based. e.g., 0 for January.
165 * @param date The value used to set the calendar's {@link #DATE DATE} time field.
166 * @param hour The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field.
167 * @param minute The value used to set the calendar's {@link #MINUTE MINUTE} time field.
168 * @param second The value used to set the calendar's {@link #SECOND SECOND} time field.
171 public TaiwanCalendar(int year, int month, int date, int hour,
172 int minute, int second)
174 super(year, month, date, hour, minute, second);
178 //-------------------------------------------------------------------------
179 // The only practical difference from a Gregorian calendar is that years
180 // are numbered since 1912, inclusive. A couple of overrides will
181 // take care of that....
182 //-------------------------------------------------------------------------
184 private static final int Taiwan_ERA_START = 1911; // 0=1911, 1=1912
186 // Use 1970 as the default value of EXTENDED_YEAR
187 private static final int GREGORIAN_EPOCH = 1970;
194 protected int handleGetExtendedYear() {
195 // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year
196 // The default value of EXTENDED_YEAR is 1970 (Minguo 59)
197 int year = GREGORIAN_EPOCH;
198 if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR
199 && newerField(EXTENDED_YEAR, ERA) == EXTENDED_YEAR) {
200 year = internalGet(EXTENDED_YEAR, GREGORIAN_EPOCH);
202 int era = internalGet(ERA, MINGUO);
204 year = internalGet(YEAR, 1) + Taiwan_ERA_START;
206 year = 1 - internalGet(YEAR, 1) + Taiwan_ERA_START;
216 protected void handleComputeFields(int julianDay) {
217 super.handleComputeFields(julianDay);
218 int y = internalGet(EXTENDED_YEAR) - Taiwan_ERA_START;
220 internalSet(ERA, MINGUO);
221 internalSet(YEAR, y);
223 internalSet(ERA, BEFORE_MINGUO);
224 internalSet(YEAR, 1- y);
229 * Override GregorianCalendar. There is only one Taiwan ERA. We
230 * should really handle YEAR, YEAR_WOY, and EXTENDED_YEAR here too to
231 * implement the 1..5000000 range, but it's not critical.
234 protected int handleGetLimit(int field, int limitType) {
236 if (limitType == MINIMUM || limitType == GREATEST_MINIMUM) {
237 return BEFORE_MINGUO;
242 return super.handleGetLimit(field, limitType);
249 public String getType() {