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