1/* GENERATED SOURCE. DO NOT MODIFY. */
2/*
3 *******************************************************************************
4 * Copyright (C) 1996-2012, International Business Machines Corporation and    *
5 * others. All Rights Reserved.                                                *
6 *******************************************************************************
7 */
8
9package android.icu.util;
10
11import java.util.Date;
12import java.util.Locale;
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 android.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code>
33 * with the tag <code>"@calendar=buddhist"</code>.</p>
34 *
35 * @see android.icu.util.Calendar
36 * @see android.icu.util.GregorianCalendar
37 *
38 * @author Laura Werner
39 * @author Alan Liu
40 */
41public 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 android.icu.util.Calendar#ERA
54     */
55    public static final int BE = 0;
56
57    /**
58     * Constructs a <code>BuddhistCalendar</code> using the current time
59     * in the default time zone with the default locale.
60     */
61    public BuddhistCalendar() {
62        super();
63    }
64
65    /**
66     * Constructs a <code>BuddhistCalendar</code> based on the current time
67     * in the given time zone with the default locale.
68     *
69     * @param zone the given time zone.
70     */
71    public BuddhistCalendar(TimeZone zone) {
72        super(zone);
73    }
74
75    /**
76     * Constructs a <code>BuddhistCalendar</code> based on the current time
77     * in the default time zone with the given locale.
78     *
79     * @param aLocale the given locale.
80     */
81    public BuddhistCalendar(Locale aLocale) {
82        super(aLocale);
83    }
84
85    /**
86     * Constructs a <code>BuddhistCalendar</code> based on the current time
87     * in the default time zone with the given locale.
88     *
89     * @param locale the given ulocale.
90     */
91    public BuddhistCalendar(ULocale locale) {
92        super(locale);
93    }
94
95    /**
96     * Constructs a <code>BuddhistCalendar</code> based on the current time
97     * in the given time zone with the given locale.
98     *
99     * @param zone the given time zone.
100     *
101     * @param aLocale the given locale.
102     */
103    public BuddhistCalendar(TimeZone zone, Locale aLocale) {
104        super(zone, aLocale);
105    }
106
107    /**
108     * Constructs a <code>BuddhistCalendar</code> based on the current time
109     * in the given time zone with the given locale.
110     *
111     * @param zone the given time zone.
112     *
113     * @param locale the given ulocale.
114     */
115    public BuddhistCalendar(TimeZone zone, ULocale locale) {
116        super(zone, locale);
117    }
118
119    /**
120     * Constructs a <code>BuddhistCalendar</code> with the given date set
121     * in the default time zone with the default locale.
122     *
123     * @param date      The date to which the new calendar is set.
124     */
125    public BuddhistCalendar(Date date) {
126        this();
127        setTime(date);
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 year      The value used to set the calendar's {@link #YEAR YEAR} time field.
135     *
136     * @param month     The value used to set the calendar's {@link #MONTH MONTH} time field.
137     *                  The value is 0-based. e.g., 0 for January.
138     *
139     * @param date      The value used to set the calendar's {@link #DATE DATE} time field.
140     */
141    public BuddhistCalendar(int year, int month, int date) {
142        super(year, month, date);
143    }
144
145    /**
146     * Constructs a BuddhistCalendar with the given date
147     * and time set for the default time zone with the default locale.
148     *
149     * @param year      The value used to set the calendar's {@link #YEAR YEAR} time field.
150     *
151     * @param month     The value used to set the calendar's {@link #MONTH MONTH} time field.
152     *                  The value is 0-based. e.g., 0 for January.
153     *
154     * @param date      The value used to set the calendar's {@link #DATE DATE} time field.
155     *
156     * @param hour      The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field.
157     *
158     * @param minute    The value used to set the calendar's {@link #MINUTE MINUTE} time field.
159     *
160     * @param second    The value used to set the calendar's {@link #SECOND SECOND} time field.
161     */
162    public BuddhistCalendar(int year, int month, int date, int hour,
163                             int minute, int second)
164    {
165        super(year, month, date, hour, minute, second);
166    }
167
168
169    //-------------------------------------------------------------------------
170    // The only practical difference from a Gregorian calendar is that years
171    // are numbered since the birth of the Buddha.  A couple of overrides will
172    // take care of that....
173    //-------------------------------------------------------------------------
174
175    // Starts in -543 AD, ie 544 BC
176    private static final int BUDDHIST_ERA_START = -543;
177
178    // Use 1970 as the default value of EXTENDED_YEAR
179    private static final int GREGORIAN_EPOCH = 1970;
180
181    /**
182     */
183    protected int handleGetExtendedYear() {
184        // EXTENDED_YEAR in BuddhistCalendar is a Gregorian year
185        // The default value of EXTENDED_YEAR is 1970 (Buddhist 2513)
186        int year;
187        if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
188            year = internalGet(EXTENDED_YEAR, GREGORIAN_EPOCH);
189        } else {
190            year = internalGet(YEAR, GREGORIAN_EPOCH - BUDDHIST_ERA_START)
191                    + BUDDHIST_ERA_START;
192        }
193        return year;
194    }
195
196    // Return JD of start of given month/year
197    /**
198     */
199    protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) {
200        return super.handleComputeMonthStart(eyear, month, useMonth);
201    }
202
203    /**
204     */
205    protected void handleComputeFields(int julianDay) {
206        super.handleComputeFields(julianDay);
207        int y = internalGet(EXTENDED_YEAR) - BUDDHIST_ERA_START;
208        internalSet(ERA, 0);
209        internalSet(YEAR, y);
210    }
211
212    /**
213     * Override GregorianCalendar.  There is only one Buddhist ERA.  We
214     * should really handle YEAR, YEAR_WOY, and EXTENDED_YEAR here too to
215     * implement the 1..5000000 range, but it's not critical.
216     */
217    protected int handleGetLimit(int field, int limitType) {
218        if (field == ERA) {
219            return BE;
220        }
221        return super.handleGetLimit(field, limitType);
222    }
223
224    /**
225     * {@inheritDoc}
226     */
227    public String getType() {
228        return "buddhist";
229    }
230}
231