2 *******************************************************************************
\r
3 * Copyright (C) 1996-2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.util;
\r
10 import com.ibm.icu.util.TimeZone;
\r
11 import java.util.Date;
\r
12 import java.util.Locale;
\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
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
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
29 * This class should not be subclassed.</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
35 * @see com.ibm.icu.util.Calendar
\r
36 * @see com.ibm.icu.util.GregorianCalendar
\r
38 * @author Laura Werner
\r
42 public class BuddhistCalendar extends GregorianCalendar {
\r
43 // jdk1.4.2 serialver
\r
44 private static final long serialVersionUID = 2583005278132380631L;
\r
46 //-------------------------------------------------------------------------
\r
48 //-------------------------------------------------------------------------
\r
51 * Constant for the Buddhist Era. This is the only allowable <code>ERA</code>
\r
52 * value for the Buddhist calendar.
\r
54 * @see com.ibm.icu.util.Calendar#ERA
\r
57 public static final int BE = 0;
\r
60 * Constructs a <code>BuddhistCalendar</code> using the current time
\r
61 * in the default time zone with the default locale.
\r
64 public BuddhistCalendar() {
\r
69 * Constructs a <code>BuddhistCalendar</code> based on the current time
\r
70 * in the given time zone with the default locale.
\r
72 * @param zone the given time zone.
\r
75 public BuddhistCalendar(TimeZone zone) {
\r
80 * Constructs a <code>BuddhistCalendar</code> based on the current time
\r
81 * in the default time zone with the given locale.
\r
83 * @param aLocale the given locale.
\r
86 public BuddhistCalendar(Locale aLocale) {
\r
91 * Constructs a <code>BuddhistCalendar</code> based on the current time
\r
92 * in the default time zone with the given locale.
\r
94 * @param locale the given ulocale.
\r
97 public BuddhistCalendar(ULocale locale) {
\r
102 * Constructs a <code>BuddhistCalendar</code> based on the current time
\r
103 * in the given time zone with the given locale.
\r
105 * @param zone the given time zone.
\r
107 * @param aLocale the given locale.
\r
110 public BuddhistCalendar(TimeZone zone, Locale aLocale) {
\r
111 super(zone, aLocale);
\r
115 * Constructs a <code>BuddhistCalendar</code> based on the current time
\r
116 * in the given time zone with the given locale.
\r
118 * @param zone the given time zone.
\r
120 * @param locale the given ulocale.
\r
123 public BuddhistCalendar(TimeZone zone, ULocale locale) {
\r
124 super(zone, locale);
\r
128 * Constructs a <code>BuddhistCalendar</code> with the given date set
\r
129 * in the default time zone with the default locale.
\r
131 * @param date The date to which the new calendar is set.
\r
134 public BuddhistCalendar(Date date) {
\r
140 * Constructs a <code>BuddhistCalendar</code> with the given date set
\r
141 * in the default time zone with the default locale.
\r
143 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
\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
148 * @param date The value used to set the calendar's {@link #DATE DATE} time field.
\r
151 public BuddhistCalendar(int year, int month, int date) {
\r
152 super(year, month, date);
\r
156 * Constructs a BuddhistCalendar with the given date
\r
157 * and time set for the default time zone with the default locale.
\r
159 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
\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
164 * @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
168 * @param minute The value used to set the calendar's {@link #MINUTE MINUTE} time field.
\r
170 * @param second The value used to set the calendar's {@link #SECOND SECOND} time field.
\r
173 public BuddhistCalendar(int year, int month, int date, int hour,
\r
174 int minute, int second)
\r
176 super(year, month, date, hour, minute, second);
\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
186 // Starts in -543 AD, ie 544 BC
\r
187 private static final int BUDDHIST_ERA_START = -543;
\r
189 // Use 1970 as the default value of EXTENDED_YEAR
\r
190 private static final int GREGORIAN_EPOCH = 1970;
\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
199 if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
\r
200 year = internalGet(EXTENDED_YEAR, GREGORIAN_EPOCH);
\r
202 year = internalGet(YEAR, GREGORIAN_EPOCH - BUDDHIST_ERA_START)
\r
203 + BUDDHIST_ERA_START;
\r
208 // Return JD of start of given month/year
\r
212 protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) {
\r
213 return super.handleComputeMonthStart(eyear, month, useMonth);
\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
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
232 protected int handleGetLimit(int field, int limitType) {
\r
233 if (field == ERA) {
\r
236 return super.handleGetLimit(field, limitType);
\r
240 * Return the current Calendar type.
\r
241 * @return type of calendar
\r
244 public String getType() {
\r