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