KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > util > BuddhistCalendar


1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7
8 package com.ibm.icu.util;
9
10 import com.ibm.icu.util.TimeZone;
11 import java.util.Date JavaDoc;
12 import java.util.Locale JavaDoc;
13
14 /**
15  * <code>BuddhistCalendar</code> is a subclass of <code>GregorianCalendar</code>
16  * that numbers years since the birth of the Buddha. This is the civil calendar
17  * in some predominantly Buddhist countries such as Thailand, and it is used for
18  * religious purposes elsewhere.
19  * <p>
20  * The Buddhist calendar is identical to the Gregorian calendar in all respects
21  * except for the year and era. Years are numbered since the birth of the
22  * Buddha in 543 BC (Gregorian), so that 1 AD (Gregorian) is equivalent to 544
23  * BE (Buddhist Era) and 1998 AD is 2541 BE.
24  * <p>
25  * The Buddhist Calendar has only one allowable era: <code>BE</code>. If the
26  * calendar is not in lenient mode (see <code>setLenient</code>), dates before
27  * 1/1/1 BE are rejected with an <code>IllegalArgumentException</code>.
28  * <p>
29  * This class should not be subclassed.</p>
30  * <p>
31  * BuddhistCalendar usually should be instantiated using
32  * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code>
33  * with the tag <code>"@calendar=buddhist"</code>.</p>
34  *
35  * @see com.ibm.icu.util.Calendar
36  * @see com.ibm.icu.util.GregorianCalendar
37  *
38  * @author Laura Werner
39  * @author Alan Liu
40  * @stable ICU 2.8
41  */

42 public class BuddhistCalendar extends GregorianCalendar {
43     // jdk1.4.2 serialver
44
private static final long serialVersionUID = 2583005278132380631L;
45     private static String JavaDoc copyright = "Copyright \u00a9 1998 IBM Corp. All Rights Reserved.";
46
47     //-------------------------------------------------------------------------
48
// Constructors...
49
//-------------------------------------------------------------------------
50

51     /**
52      * Constant for the Buddhist Era. This is the only allowable <code>ERA</code>
53      * value for the Buddhist calendar.
54      *
55      * @see com.ibm.icu.util.Calendar#ERA
56      * @stable ICU 2.8
57      */

58     public static final int BE = 0;
59     
60     /**
61      * Constructs a <code>BuddhistCalendar</code> using the current time
62      * in the default time zone with the default locale.
63      * @stable ICU 2.8
64      */

65     public BuddhistCalendar() {
66         super();
67     }
68
69     /**
70      * Constructs a <code>BuddhistCalendar</code> based on the current time
71      * in the given time zone with the default locale.
72      *
73      * @param zone the given time zone.
74      * @stable ICU 2.8
75      */

76     public BuddhistCalendar(TimeZone zone) {
77         super(zone);
78     }
79
80     /**
81      * Constructs a <code>BuddhistCalendar</code> based on the current time
82      * in the default time zone with the given locale.
83      *
84      * @param aLocale the given locale.
85      * @stable ICU 2.8
86      */

87     public BuddhistCalendar(Locale JavaDoc aLocale) {
88         super(aLocale);
89     }
90
91     /**
92      * Constructs a <code>BuddhistCalendar</code> based on the current time
93      * in the default time zone with the given locale.
94      *
95      * @param locale the given ulocale.
96      * @draft ICU 3.2
97      * @provisional This API might change or be removed in a future release.
98      */

99     public BuddhistCalendar(ULocale locale) {
100         super(locale);
101     }
102
103     /**
104      * Constructs a <code>BuddhistCalendar</code> based on the current time
105      * in the given time zone with the given locale.
106      *
107      * @param zone the given time zone.
108      *
109      * @param aLocale the given locale.
110      * @stable ICU 2.8
111      */

112     public BuddhistCalendar(TimeZone zone, Locale JavaDoc aLocale) {
113         super(zone, aLocale);
114     }
115
116     /**
117      * Constructs a <code>BuddhistCalendar</code> based on the current time
118      * in the given time zone with the given locale.
119      *
120      * @param zone the given time zone.
121      *
122      * @param locale the given ulocale.
123      * @draft ICU 3.2
124      * @provisional This API might change or be removed in a future release.
125      */

126     public BuddhistCalendar(TimeZone zone, ULocale locale) {
127         super(zone, locale);
128     }
129
130     /**
131      * Constructs a <code>BuddhistCalendar</code> with the given date set
132      * in the default time zone with the default locale.
133      *
134      * @param date The date to which the new calendar is set.
135      * @stable ICU 2.8
136      */

137     public BuddhistCalendar(Date JavaDoc date) {
138         this();
139         setTime(date);
140     }
141
142     /**
143      * Constructs a <code>BuddhistCalendar</code> with the given date set
144      * in the default time zone with the default locale.
145      *
146      * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
147      *
148      * @param month The value used to set the calendar's {@link #MONTH MONTH} time field.
149      * The value is 0-based. e.g., 0 for January.
150      *
151      * @param date The value used to set the calendar's {@link #DATE DATE} time field.
152      * @stable ICU 2.8
153      */

154     public BuddhistCalendar(int year, int month, int date) {
155         super(year, month, date);
156     }
157
158     /**
159      * Constructs a BuddhistCalendar with the given date
160      * and time set for the default time zone with the default locale.
161      *
162      * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
163      *
164      * @param month The value used to set the calendar's {@link #MONTH MONTH} time field.
165      * The value is 0-based. e.g., 0 for January.
166      *
167      * @param date The value used to set the calendar's {@link #DATE DATE} time field.
168      *
169      * @param hour The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field.
170      *
171      * @param minute The value used to set the calendar's {@link #MINUTE MINUTE} time field.
172      *
173      * @param second The value used to set the calendar's {@link #SECOND SECOND} time field.
174      * @stable ICU 2.8
175      */

176     public BuddhistCalendar(int year, int month, int date, int hour,
177                              int minute, int second)
178     {
179         super(year, month, date, hour, minute, second);
180     }
181
182
183     //-------------------------------------------------------------------------
184
// The only practical difference from a Gregorian calendar is that years
185
// are numbered since the birth of the Buddha. A couple of overrides will
186
// take care of that....
187
//-------------------------------------------------------------------------
188

189     // Starts in -543 AD, ie 544 BC
190
private static final int BUDDHIST_ERA_START = -543;
191
192     /**
193      * @stable ICU 2.8
194      */

195     protected int handleGetExtendedYear() {
196         int year;
197         if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
198             year = internalGet(EXTENDED_YEAR, 1);
199         } else {
200             // Ignore the era, as there is only one
201
year = internalGet(YEAR, 1);
202         }
203         return year;
204     }
205
206     // Return JD of start of given month/year
207
/**
208      * @stable ICU 2.8
209      */

210     protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) {
211         return super.handleComputeMonthStart(eyear + BUDDHIST_ERA_START, month, useMonth);
212     }
213
214     /**
215      * @stable ICU 2.8
216      */

217     protected void handleComputeFields(int julianDay) {
218         super.handleComputeFields(julianDay);
219         int y = internalGet(EXTENDED_YEAR) - BUDDHIST_ERA_START;
220         internalSet(EXTENDED_YEAR, y);
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      * Return the current Calendar type.
240      * @return type of calendar (gregorian, etc.)
241      * @internal ICU 3.0
242      * @deprecated This API is ICU internal only.
243      */

244     public String JavaDoc getType() {
245         return "buddhist";
246     }
247
248     /*
249     private static CalendarFactory factory;
250     public static CalendarFactory factory() {
251         if (factory == null) {
252             factory = new CalendarFactory() {
253                 public Calendar create(TimeZone tz, ULocale loc) {
254                     return new BuddhistCalendar(tz, loc);
255                 }
256
257                 public String factoryName() {
258                     return "Buddhist";
259                 }
260             };
261         }
262         return factory;
263     }
264     */

265 }
266
Popular Tags