2 *******************************************************************************
\r
3 * Copyright (C) 2005-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.util;
\r
9 import java.util.Date;
\r
10 import java.util.Locale;
\r
13 * Base class for EthiopicCalendar and CopticCalendar.
\r
15 * @deprecated This API is ICU internal only.
\r
17 abstract class CECalendar extends Calendar {
\r
18 // jdk1.4.2 serialver
\r
19 private static final long serialVersionUID = -999547623066414271L;
\r
21 private static final int LIMITS[][] = {
\r
22 // Minimum Greatest Least Maximum
\r
24 { 0, 0, 1, 1 }, // ERA
\r
25 { 1, 1, 5000000, 5000000 }, // YEAR
\r
26 { 0, 0, 12, 12 }, // MONTH
\r
27 { 1, 1, 52, 53 }, // WEEK_OF_YEAR
\r
28 {/* */}, // WEEK_OF_MONTH
\r
29 { 1, 1, 5, 30 }, // DAY_OF_MONTH
\r
30 { 1, 1, 365, 366 }, // DAY_OF_YEAR
\r
31 {/* */}, // DAY_OF_WEEK
\r
32 { -1, -1, 1, 5 }, // DAY_OF_WEEK_IN_MONTH
\r
35 {/* */}, // HOUR_OF_DAY
\r
38 {/* */}, // MILLISECOND
\r
39 {/* */}, // ZONE_OFFSET
\r
40 {/* */}, // DST_OFFSET
\r
41 { -5000000, -5000000, 5000000, 5000000 }, // YEAR_WOY
\r
42 {/* */}, // DOW_LOCAL
\r
43 { -5000000, -5000000, 5000000, 5000000 }, // EXTENDED_YEAR
\r
44 {/* */}, // JULIAN_DAY
\r
45 {/* */}, // MILLISECONDS_IN_DAY
\r
48 //-------------------------------------------------------------------------
\r
50 //-------------------------------------------------------------------------
\r
53 * Constructs a default <code>CECalendar</code> using the current time
\r
54 * in the default time zone with the default locale.
\r
56 protected CECalendar() {
\r
57 this(TimeZone.getDefault(), ULocale.getDefault());
\r
61 * Constructs a <code>CECalendar</code> based on the current time
\r
62 * in the given time zone with the default locale.
\r
64 * @param zone The time zone for the new calendar.
\r
66 protected CECalendar(TimeZone zone) {
\r
67 this(zone, ULocale.getDefault());
\r
71 * Constructs a <code>CECalendar</code> based on the current time
\r
72 * in the default time zone with the given locale.
\r
74 * @param aLocale The locale for the new calendar.
\r
76 protected CECalendar(Locale aLocale) {
\r
77 this(TimeZone.getDefault(), aLocale);
\r
81 * Constructs a <code>CECalendar</code> based on the current time
\r
82 * in the default time zone with the given locale.
\r
84 * @param locale The locale for the new calendar.
\r
86 protected CECalendar(ULocale locale) {
\r
87 this(TimeZone.getDefault(), locale);
\r
91 * Constructs a <code>CECalendar</code> based on the current time
\r
92 * in the given time zone with the given locale.
\r
94 * @param zone The time zone for the new calendar.
\r
96 * @param aLocale The locale for the new calendar.
\r
98 protected CECalendar(TimeZone zone, Locale aLocale) {
\r
99 super(zone, aLocale);
\r
100 setTimeInMillis(System.currentTimeMillis());
\r
104 * Constructs a <code>CECalendar</code> based on the current time
\r
105 * in the given time zone with the given locale.
\r
107 * @param zone The time zone for the new calendar.
\r
109 * @param locale The locale for the new calendar.
\r
111 protected CECalendar(TimeZone zone, ULocale locale) {
\r
112 super(zone, locale);
\r
113 setTimeInMillis(System.currentTimeMillis());
\r
117 * Constructs a <code>CECalendar</code> with the given date set
\r
118 * in the default time zone with the default locale.
\r
120 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
\r
122 * @param month The value used to set the calendar's {@link #MONTH MONTH} time field.
\r
123 * The value is 0-based. e.g., 0 for Tishri.
\r
125 * @param date The value used to set the calendar's {@link #DATE DATE} time field.
\r
127 protected CECalendar(int year, int month, int date) {
\r
128 super(TimeZone.getDefault(), ULocale.getDefault());
\r
129 this.set(year, month, date);
\r
133 * Constructs a <code>CECalendar</code> with the given date set
\r
134 * in the default time zone with the default locale.
\r
136 * @param date The date to which the new calendar is set.
\r
138 protected CECalendar(Date date) {
\r
139 super(TimeZone.getDefault(), ULocale.getDefault());
\r
140 this.setTime(date);
\r
144 * Constructs a <code>CECalendar</code> with the given date
\r
145 * and time set for the default time zone with the default locale.
\r
147 * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
\r
148 * @param month The value used to set the calendar's {@link #MONTH MONTH} time field.
\r
149 * The value is 0-based. e.g., 0 for Tishri.
\r
150 * @param date The value used to set the calendar's {@link #DATE DATE} time field.
\r
151 * @param hour The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field.
\r
152 * @param minute The value used to set the calendar's {@link #MINUTE MINUTE} time field.
\r
153 * @param second The value used to set the calendar's {@link #SECOND SECOND} time field.
\r
155 protected CECalendar(int year, int month, int date, int hour,
\r
156 int minute, int second)
\r
158 super(TimeZone.getDefault(), ULocale.getDefault());
\r
159 this.set(year, month, date, hour, minute, second);
\r
162 //-------------------------------------------------------------------------
\r
163 // Calendar framework
\r
164 //-------------------------------------------------------------------------
\r
167 * The Coptic and Ethiopic calendars differ only in their epochs.
\r
168 * This method must be implemented by CECalendar subclasses to
\r
169 * return the date offset from Julian.
\r
171 abstract protected int getJDEpochOffset();
\r
174 * Return JD of start of given month/extended year
\r
176 protected int handleComputeMonthStart(int eyear,
\r
178 boolean useMonth) {
\r
179 return ceToJD(eyear, emonth, 0, getJDEpochOffset());
\r
183 * Calculate the limit for a specified type of limit and field
\r
185 protected int handleGetLimit(int field, int limitType) {
\r
186 return LIMITS[field][limitType];
\r
189 //-------------------------------------------------------------------------
\r
190 // Calendar framework
\r
191 //-------------------------------------------------------------------------
\r
194 * Convert an Coptic/Ethiopic year, month and day to a Julian day
\r
195 * @param year the extended year
\r
196 * @param month the month
\r
197 * @param day the day
\r
198 * @return Julian day
\r
200 public static int ceToJD(long year, int month, int day, int jdEpochOffset) {
\r
202 // Julian<->Ethiopic algorithms from:
\r
203 // "Calendars in Ethiopia", Berhanu Beyene, Manfred Kudlek, International Conference
\r
204 // of Ethiopian Studies XV, Hamburg, 2003
\r
206 // handle month > 12, < 0 (e.g. from add/set)
\r
207 if ( month >= 0 ) {
\r
212 year += month/13 - 1;
\r
213 month = month%13 + 12;
\r
216 jdEpochOffset // difference from Julian epoch to 1,1,1
\r
217 + 365 * year // number of days from years
\r
218 + floorDivide(year, 4) // extra day of leap year
\r
219 + 30 * month // number of days from months (months are 0-based)
\r
220 + day - 1 // number of days for present month (1 based)
\r
225 * Convert a Julian day to an Coptic/Ethiopic year, month and day
\r
227 public static void jdToCE(int julianDay, int jdEpochOffset, int[] fields) {
\r
228 int c4; // number of 4 year cycle (1461 days)
\r
229 int[] r4 = new int[1]; // remainder of 4 year cycle, always positive
\r
231 c4 = floorDivide(julianDay - jdEpochOffset, 1461, r4);
\r
234 fields[0] = 4 * c4 + (r4[0]/365 - r4[0]/1460); // 4 * <number of 4year cycle> + <years within the last cycle>
\r
236 int doy = (r4[0] == 1460) ? 365 : (r4[0] % 365); // days in present year
\r
239 fields[1] = doy / 30; // 30 -> Coptic/Ethiopic month length up to 12th month
\r
241 fields[2] = (doy % 30) + 1; // 1-based days in a month
\r