]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/util/TaiwanCalendar.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / util / TaiwanCalendar.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2008, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 \r
8 package com.ibm.icu.util;\r
9 \r
10 import com.ibm.icu.util.TimeZone;\r
11 import java.util.Date;\r
12 import java.util.Locale;\r
13 \r
14 /** \r
15  * <code>TaiwanCalendar</code> is a subclass of <code>GregorianCalendar</code>\r
16  * that numbers years since 1912. \r
17  * <p>\r
18  * The Taiwan calendar is identical to the Gregorian calendar in all respects\r
19  * except for the year and era.  Years are numbered since 1912 AD (Gregorian).\r
20  * <p>\r
21  * The Taiwan Calendar has one era: <code>MINGUO</code>.\r
22  * <p>\r
23  * This class should not be subclassed.</p>\r
24  * <p>\r
25  * TaiwanCalendar usually should be instantiated using \r
26  * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code>\r
27  * with the tag <code>"@calendar=roc"</code>.</p>\r
28  * \r
29  * @see com.ibm.icu.util.Calendar\r
30  * @see com.ibm.icu.util.GregorianCalendar\r
31  *\r
32  * @author Laura Werner\r
33  * @author Alan Liu\r
34  * @author Steven R. Loomis\r
35  * @stable ICU 3.8\r
36  */\r
37 public class TaiwanCalendar extends GregorianCalendar {\r
38     // jdk1.4.2 serialver\r
39     private static final long serialVersionUID = 2583005278132380631L;\r
40 \r
41     //-------------------------------------------------------------------------\r
42     // Constructors...\r
43     //-------------------------------------------------------------------------\r
44 \r
45     /**\r
46      * Constant for the Taiwan Era for years before Minguo 1.\r
47      * Brefore Minuo 1 is Gregorian 1911, Before Minguo 2 is Gregorian 1910\r
48      * and so on.\r
49      *\r
50      * @see com.ibm.icu.util.Calendar#ERA\r
51      * @stable ICU 3.8\r
52      */\r
53     public static final int BEFORE_MINGUO = 0;\r
54 \r
55     /**\r
56      * Constant for the Taiwan Era for Minguo.  Minguo 1 is 1912 in\r
57      * Gregorian calendar.\r
58      *\r
59      * @see com.ibm.icu.util.Calendar#ERA\r
60      * @stable ICU 3.8\r
61      */\r
62     public static final int MINGUO = 1;\r
63 \r
64     /**\r
65      * Constructs a <code>TaiwanCalendar</code> using the current time\r
66      * in the default time zone with the default locale.\r
67      * @stable ICU 3.8\r
68      */\r
69     public TaiwanCalendar() {\r
70         super();\r
71     }\r
72 \r
73     /**\r
74      * Constructs a <code>TaiwanCalendar</code> based on the current time\r
75      * in the given time zone with the default locale.\r
76      *\r
77      * @param zone the given time zone.\r
78      * @stable ICU 3.8\r
79      */\r
80     public TaiwanCalendar(TimeZone zone) {\r
81         super(zone);\r
82     }\r
83 \r
84     /**\r
85      * Constructs a <code>TaiwanCalendar</code> based on the current time\r
86      * in the default time zone with the given locale.\r
87      *\r
88      * @param aLocale the given locale.\r
89      * @stable ICU 3.8\r
90      */\r
91     public TaiwanCalendar(Locale aLocale) {\r
92         super(aLocale);\r
93     }\r
94 \r
95     /**\r
96      * Constructs a <code>TaiwanCalendar</code> based on the current time\r
97      * in the default time zone with the given locale.\r
98      *\r
99      * @param locale the given ulocale.\r
100      * @stable ICU 3.8\r
101      */\r
102     public TaiwanCalendar(ULocale locale) {\r
103         super(locale);\r
104     }\r
105 \r
106     /**\r
107      * Constructs a <code>TaiwanCalendar</code> based on the current time\r
108      * in the given time zone with the given locale.\r
109      *\r
110      * @param zone the given time zone.\r
111      *\r
112      * @stable ICU 3.8\r
113      */\r
114     public TaiwanCalendar(TimeZone zone, Locale aLocale) {\r
115         super(zone, aLocale);\r
116     }\r
117 \r
118     /**\r
119      * Constructs a <code>TaiwanCalendar</code> based on the current time\r
120      * in the given time zone with the given locale.\r
121      *\r
122      * @param zone the given time zone.\r
123      * @param locale the given ulocale.\r
124      * @stable ICU 3.8\r
125      */\r
126     public TaiwanCalendar(TimeZone zone, ULocale locale) {\r
127         super(zone, locale);\r
128     }\r
129 \r
130     /**\r
131      * Constructs a <code>TaiwanCalendar</code> with the given date set\r
132      * in the default time zone with the default locale.\r
133      *\r
134      * @param date      The date to which the new calendar is set.\r
135      * @stable ICU 3.8\r
136      */\r
137     public TaiwanCalendar(Date date) {\r
138         this();\r
139         setTime(date);\r
140     }\r
141 \r
142     /**\r
143      * Constructs a <code>TaiwanCalendar</code> with the given date set\r
144      * in the default time zone with the default locale.\r
145      *\r
146      * @param year      The value used to set the calendar's {@link #YEAR YEAR} time field.\r
147      *\r
148      * @param month     The value used to set the calendar's {@link #MONTH MONTH} time field.\r
149      *                  The value is 0-based. e.g., 0 for January.\r
150      *\r
151      * @param date      The value used to set the calendar's {@link #DATE DATE} time field.\r
152      * @stable ICU 3.8\r
153      */\r
154     public TaiwanCalendar(int year, int month, int date) {\r
155         super(year, month, date);\r
156     }\r
157 \r
158     /**\r
159      * Constructs a TaiwanCalendar with the given date\r
160      * and time set for the default time zone with the default locale.\r
161      *\r
162      * @param year      The value used to set the calendar's {@link #YEAR YEAR} time field.\r
163      *\r
164      * @param month     The value used to set the calendar's {@link #MONTH MONTH} time field.\r
165      *                  The value is 0-based. e.g., 0 for January.\r
166      * @param date      The value used to set the calendar's {@link #DATE DATE} time field.\r
167      * @param hour      The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field.\r
168      * @param minute    The value used to set the calendar's {@link #MINUTE MINUTE} time field.\r
169      * @param second    The value used to set the calendar's {@link #SECOND SECOND} time field.\r
170      * @stable ICU 3.8\r
171      */\r
172     public TaiwanCalendar(int year, int month, int date, int hour,\r
173                              int minute, int second)\r
174     {\r
175         super(year, month, date, hour, minute, second);\r
176     }\r
177 \r
178 \r
179     //-------------------------------------------------------------------------\r
180     // The only practical difference from a Gregorian calendar is that years\r
181     // are numbered since 1912, inclusive.  A couple of overrides will\r
182     // take care of that....\r
183     //-------------------------------------------------------------------------\r
184     \r
185     private static final int Taiwan_ERA_START = 1911; // 0=1911, 1=1912\r
186 \r
187     // Use 1970 as the default value of EXTENDED_YEAR\r
188     private static final int GREGORIAN_EPOCH = 1970;\r
189 \r
190 \r
191     /**\r
192      * {@inheritDoc}\r
193      * @stable ICU 3.8\r
194      */    \r
195     protected int handleGetExtendedYear() {\r
196         // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year\r
197         // The default value of EXTENDED_YEAR is 1970 (Minguo 59)\r
198         int year = GREGORIAN_EPOCH;\r
199         if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR\r
200                 && newerField(EXTENDED_YEAR, ERA) == EXTENDED_YEAR) {\r
201             year = internalGet(EXTENDED_YEAR, GREGORIAN_EPOCH);\r
202         } else {\r
203             int era = internalGet(ERA, MINGUO);\r
204             if (era == MINGUO) {\r
205                 year = internalGet(YEAR, 1) + Taiwan_ERA_START;\r
206             } else {\r
207                 year = 1 - internalGet(YEAR, 1) + Taiwan_ERA_START;\r
208             }\r
209         }\r
210         return year;\r
211     }\r
212 \r
213     /**\r
214      * {@inheritDoc}\r
215      * @stable ICU 3.8\r
216      */\r
217     protected void handleComputeFields(int julianDay) {\r
218         super.handleComputeFields(julianDay);\r
219         int y = internalGet(EXTENDED_YEAR) - Taiwan_ERA_START;\r
220         if (y > 0) {\r
221             internalSet(ERA, MINGUO);\r
222             internalSet(YEAR, y);\r
223         } else {\r
224             internalSet(ERA, BEFORE_MINGUO);\r
225             internalSet(YEAR, 1- y);\r
226         }\r
227     }\r
228 \r
229     /**\r
230      * Override GregorianCalendar.  There is only one Taiwan ERA.  We\r
231      * should really handle YEAR, YEAR_WOY, and EXTENDED_YEAR here too to\r
232      * implement the 1..5000000 range, but it's not critical.\r
233      * @stable ICU 3.8\r
234      */\r
235     protected int handleGetLimit(int field, int limitType) {\r
236         if (field == ERA) {\r
237             if (limitType == MINIMUM || limitType == GREATEST_MINIMUM) {\r
238                 return BEFORE_MINGUO;\r
239             } else {\r
240                 return MINGUO;\r
241             }\r
242         }\r
243         return super.handleGetLimit(field, limitType);\r
244     }\r
245     \r
246     /**\r
247      * {@inheritDoc}\r
248      * @stable ICU 3.8\r
249      */\r
250     public String getType() {\r
251         return "roc";\r
252     }\r
253 }\r