2 *******************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.util;
\r
10 import java.util.Date;
\r
11 import java.util.Locale;
\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
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
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
28 * This class should not be subclassed.</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
34 * @see com.ibm.icu.util.Calendar
\r
35 * @see com.ibm.icu.util.GregorianCalendar
\r
37 * @author Laura Werner
\r
41 public class BuddhistCalendar extends GregorianCalendar {
\r
42 // jdk1.4.2 serialver
\r
43 private static final long serialVersionUID = 2583005278132380631L;
\r
45 //-------------------------------------------------------------------------
\r
47 //-------------------------------------------------------------------------
\r
50 * Constant for the Buddhist Era. This is the only allowable <code>ERA</code>
\r
51 * value for the Buddhist calendar.
\r
53 * @see com.ibm.icu.util.Calendar#ERA
\r
56 public static final int BE = 0;
\r
59 * Constructs a <code>BuddhistCalendar</code> using the current time
\r
60 * in the default time zone with the default locale.
\r
63 public BuddhistCalendar() {
\r
68 * Constructs a <code>BuddhistCalendar</code> based on the current time
\r
69 * in the given time zone with the default locale.
\r
71 * @param zone the given time zone.
\r
74 public BuddhistCalendar(TimeZone zone) {
\r
79 * Constructs a <code>BuddhistCalendar</code> based on the current time
\r
80 * in the default time zone with the given locale.
\r
82 * @param aLocale the given locale.
\r
85 public BuddhistCalendar(Locale aLocale) {
\r
90 * Constructs a <code>BuddhistCalendar</code> based on the current time
\r
91 * in the default time zone with the given locale.
\r
93 * @param locale the given ulocale.
\r
96 public BuddhistCalendar(ULocale locale) {
\r
101 * Constructs a <code>BuddhistCalendar</code> based on the current time
\r
102 * in the given time zone with the given locale.
\r
104 * @param zone the given time zone.
\r
106 * @param aLocale the given locale.
\r
109 public BuddhistCalendar(TimeZone zone, Locale aLocale) {
\r
110 super(zone, aLocale);
\r
114 * Constructs a <code>BuddhistCalendar</code> based on the current time
\r
115 * in the given time zone with the given locale.
\r
117 * @param zone the given time zone.
\r
119 * @param locale the given ulocale.
\r
122 public BuddhistCalendar(TimeZone zone, ULocale locale) {
\r
123 super(zone, locale);
\r
127 * Constructs a <code>BuddhistCalendar</code> with the given date set
\r
128 * in the default time zone with the default locale.
\r
130 * @param date The date to which the new calendar is set.
\r
133 public BuddhistCalendar(Date date) {
\r
139 * Constructs a <code>BuddhistCalendar</code> with the given date set
\r
140 * in the default time zone with the default locale.
\r
142 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
\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
147 * @param date The value used to set the calendar's {@link #DATE DATE} time field.
\r
150 public BuddhistCalendar(int year, int month, int date) {
\r
151 super(year, month, date);
\r
155 * Constructs a BuddhistCalendar with the given date
\r
156 * and time set for the default time zone with the default locale.
\r
158 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
\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
163 * @param date The value used to set the calendar's {@link #DATE DATE} time field.
\r
165 * @param hour The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field.
\r
167 * @param minute The value used to set the calendar's {@link #MINUTE MINUTE} time field.
\r
169 * @param second The value used to set the calendar's {@link #SECOND SECOND} time field.
\r
172 public BuddhistCalendar(int year, int month, int date, int hour,
\r
173 int minute, int second)
\r
175 super(year, month, date, hour, minute, second);
\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
185 // Starts in -543 AD, ie 544 BC
\r
186 private static final int BUDDHIST_ERA_START = -543;
\r
188 // Use 1970 as the default value of EXTENDED_YEAR
\r
189 private static final int GREGORIAN_EPOCH = 1970;
\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
198 if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
\r
199 year = internalGet(EXTENDED_YEAR, GREGORIAN_EPOCH);
\r
201 year = internalGet(YEAR, GREGORIAN_EPOCH - BUDDHIST_ERA_START)
\r
202 + BUDDHIST_ERA_START;
\r
207 // Return JD of start of given month/year
\r
211 protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) {
\r
212 return super.handleComputeMonthStart(eyear, month, useMonth);
\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
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
231 protected int handleGetLimit(int field, int limitType) {
\r
232 if (field == ERA) {
\r
235 return super.handleGetLimit(field, limitType);
\r
239 * Return the current Calendar type.
\r
240 * @return type of calendar
\r
243 public String getType() {
\r