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