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