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