KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > util > CECalendar


1 /*
2  *******************************************************************************
3  * Copyright (C) 2005-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7 package com.ibm.icu.util;
8
9 import java.util.Date JavaDoc;
10 import java.util.Locale JavaDoc;
11
12 /**
13  * Base class for EthiopicCalendar and CopticCalendar.
14  * @internal
15  */

16 class CECalendar extends Calendar {
17     // jdk1.4.2 serialver
18
private static final long serialVersionUID = -999547623066414271L;
19
20     private static final int LIMITS[][] = {
21         // Minimum Greatest Least Maximum
22
// Minimum Maximum
23
{0, 0, 1, 1 }, // ERA
24
{1, 1, 5828963, 5838270 }, // YEAR
25
{0, 0, 13, 13 }, // MONTH
26
{1, 1, 52, 53 }, // WEEK_OF_YEAR
27
{0, 0, 1, 6 }, // WEEK_OF_MONTH
28
{1, 1, 5, 30 }, // DAY_OF_MONTH
29
{1, 1, 365, 366 }, // DAY_OF_YEAR
30
{/* */}, // DAY_OF_WEEK
31
{-1, -1, 4, 6 }, // DAY_OF_WEEK_IN_MONTH
32
{/* */}, // AM_PM
33
{/* */}, // HOUR
34
{/* */}, // HOUR_OF_DAY
35
{/* */}, // MINUTE
36
{/* */}, // SECOND
37
{/* */}, // MILLISECOND
38
{/* */}, // ZONE_OFFSET
39
{/* */}, // DST_OFFSET
40
{-5838270, -5838270, 5828964, 5838271}, // YEAR_WOY
41
{/* */}, // DOW_LOCAL
42
{-5838269, -5838269, 5828963, 5838270}, // EXTENDED_YEAR
43
{/* */}, // JULIAN_DAY
44
{/* */}, // MILLISECONDS_IN_DAY
45
};
46
47     private static final int[][] ceMONTH_COUNT = {
48         //len len2 st st2
49
{30, 30, 0, 0}, // Meskerem
50
{30, 30, 30, 30}, // Tekemt
51
{30, 30, 60, 60}, // Hedar
52
{30, 30, 90, 90}, // Tahsas
53
{30, 30, 120, 120}, // Ter
54
{30, 30, 150, 150}, // Yekatit
55
{30, 30, 180, 180}, // Megabit
56
{30, 30, 210, 210}, // Miazia
57
{30, 30, 240, 244}, // Genbot
58
{30, 30, 270, 270}, // Sene
59
{30, 30, 300, 300}, // Hamle
60
{30, 30, 330, 330}, // Nehasse
61
{ 5, 6, 360, 360} // Pwagme
62
// len length of month
63
// len2 length of month in a leap year
64
// st days in year before start of month
65
// st2 days in year before month in leap year
66
};
67     
68     // The Coptic and Ethiopic calendars differ only in their epochs.
69
// We handle this by setting the jdOffset to the difference between
70
// the Julian and Coptic or Ethiopic epoch.
71
// This value is set in the class initialization phase of the two
72
// subclasses, CopticCalendar and EthiopicCalendar
73
protected int jdEpochOffset = -1;
74     
75
76     protected int handleGetLimit(int field, int limitType) {
77         return LIMITS[field][limitType];
78     }
79     
80     //-------------------------------------------------------------------------
81
// Constructors...
82
//-------------------------------------------------------------------------
83

84     /**
85      * Constructs a default <code>CECalendar</code> using the current time
86      * in the default time zone with the default locale.
87      */

88     protected CECalendar() {
89         this(TimeZone.getDefault(), ULocale.getDefault());
90     }
91
92     /**
93      * Constructs a <code>CECalendar</code> based on the current time
94      * in the given time zone with the default locale.
95      *
96      * @param zone The time zone for the new calendar.
97      */

98     protected CECalendar(TimeZone zone) {
99         this(zone, ULocale.getDefault());
100     }
101
102     /**
103      * Constructs a <code>CECalendar</code> based on the current time
104      * in the default time zone with the given locale.
105      *
106      * @param aLocale The locale for the new calendar.
107      */

108     protected CECalendar(Locale JavaDoc aLocale) {
109         this(TimeZone.getDefault(), aLocale);
110     }
111
112     /**
113      * Constructs a <code>CECalendar</code> based on the current time
114      * in the default time zone with the given locale.
115      *
116      * @param locale The locale for the new calendar.
117      */

118     protected CECalendar(ULocale locale) {
119         this(TimeZone.getDefault(), locale);
120     }
121
122     /**
123      * Constructs a <code>CECalendar</code> based on the current time
124      * in the given time zone with the given locale.
125      *
126      * @param zone The time zone for the new calendar.
127      *
128      * @param aLocale The locale for the new calendar.
129      */

130     protected CECalendar(TimeZone zone, Locale JavaDoc aLocale) {
131         super(zone, aLocale);
132         setTimeInMillis(System.currentTimeMillis());
133     }
134
135     /**
136      * Constructs a <code>CECalendar</code> based on the current time
137      * in the given time zone with the given locale.
138      *
139      * @param zone The time zone for the new calendar.
140      *
141      * @param locale The locale for the new calendar.
142      */

143     protected CECalendar(TimeZone zone, ULocale locale) {
144         super(zone, locale);
145         setTimeInMillis(System.currentTimeMillis());
146     }
147
148     /**
149      * Constructs a <code>CECalendar</code> with the given date set
150      * in the default time zone with the default locale.
151      *
152      * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
153      *
154      * @param month The value used to set the calendar's {@link #MONTH MONTH} time field.
155      * The value is 0-based. e.g., 0 for Tishri.
156      *
157      * @param date The value used to set the calendar's {@link #DATE DATE} time field.
158      */

159     protected CECalendar(int year, int month, int date) {
160         super(TimeZone.getDefault(), ULocale.getDefault());
161         this.set(year, month, date);
162     }
163
164     /**
165      * Constructs a <code>CECalendar</code> with the given date set
166      * in the default time zone with the default locale.
167      *
168      * @param date The date to which the new calendar is set.
169      */

170     protected CECalendar(Date JavaDoc date) {
171         super(TimeZone.getDefault(), ULocale.getDefault());
172         this.setTime(date);
173     }
174
175     /**
176      * Constructs a <code>CECalendar</code> with the given date
177      * and time set for the default time zone with the default locale.
178      *
179      * @param year The value used to set the calendar's {@link #YEAR YEAR} time field.
180      * @param month The value used to set the calendar's {@link #MONTH MONTH} time field.
181      * The value is 0-based. e.g., 0 for Tishri.
182      * @param date The value used to set the calendar's {@link #DATE DATE} time field.
183      * @param hour The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field.
184      * @param minute The value used to set the calendar's {@link #MINUTE MINUTE} time field.
185      * @param second The value used to set the calendar's {@link #SECOND SECOND} time field.
186      */

187     protected CECalendar(int year, int month, int date, int hour,
188                          int minute, int second)
189     {
190         super(TimeZone.getDefault(), ULocale.getDefault());
191         this.set(year, month, date, hour, minute, second);
192     }
193     
194     
195     //-------------------------------------------------------------------------
196
// Calendar system Converstion methods...
197
//-------------------------------------------------------------------------
198

199     /**
200      * @internal
201      */

202     protected int handleComputeMonthStart(int eyear,
203                                           int emonth,
204                                           boolean useMonth) {
205         return ceToJD(eyear, emonth, 0, jdEpochOffset);
206     }
207
208     /**
209      * @internal
210      */

211     protected int handleGetExtendedYear() {
212         int year;
213         if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
214             year = internalGet(EXTENDED_YEAR, 1); // Default to year 1
215
} else {
216             year = internalGet(YEAR, 1); // Default to year 1
217
}
218         return year;
219     }
220
221     /**
222      * @internal
223      */

224     protected void handleComputeFields(int julianDay) {
225         Integer JavaDoc[] date = getDateFromJD(julianDay, jdEpochOffset);
226         int _year = date[0].intValue();
227         int _month = date[1].intValue();
228         int _day = date[2].intValue();
229         int ceyear = 0;
230
231         // Do we want to use EthiopicCalendar.AA, .AM here?
232
int era = GregorianCalendar.AD;
233         if (_year < 0) { // dlf: this is what the test says to do
234
era = GregorianCalendar.BC;
235             ceyear = 1 - _year;
236         } else {
237             ceyear = _year;
238         }
239
240         internalSet(MONTH, _month);
241         internalSet(DAY_OF_MONTH, _day);
242         internalSet(DAY_OF_YEAR, (30 * _month) + _day);
243         internalSet(EXTENDED_YEAR, ceyear);
244         internalSet(ERA, era);
245         internalSet(YEAR, _year);
246     }
247
248     /**
249      * @internal
250      */

251     public static int ceToJD(long year, int month, int date, int jdEpochOffset) {
252
253         // Julian<->Ethiopic algorithms from:
254
// "Calendars in Ethiopia", Berhanu Beyene, Manfred Kudlek, International Conference
255
// of Ethiopian Studies XV, Hamburg, 2003
256

257         return (int) (
258             (jdEpochOffset+365) // difference from Julian epoch to 1,1,1
259
+ 365 * (year - 1) // number of days from years
260
+ quotient(year, 4) // extra day of leap year
261
+ 30 * (month + 1) // number of days from months
262
+ date // number of days for present month
263
- 31 // slack?
264
);
265     }
266
267     /**
268      * @internal
269      * @provisional This API might change or be removed in a future release.
270      */

271     public static Integer JavaDoc[] getDateFromJD(int julianDay, int jdEpochOffset) {
272         // 1461 is the number of days in 4 years
273
long r4 = mod(julianDay - jdEpochOffset, 1461); // number of days within a 4 year period
274
long n = mod(r4, 365) + 365 * quotient(r4, 1460); // days in present year
275

276         long aprime = 4 // number of years in the leap year cycle
277
* quotient(julianDay - jdEpochOffset, 1461) // number of 4 year periods between epochs?
278
+ quotient(r4, 365) // number of regular years?
279
- quotient(r4, 1460) // number of 4 year periods?
280
- 1;
281
282         int _year = (int) (aprime + 1);
283         int _month = (int) (quotient(n, 30));
284         int _day = mod(n, 30) + 1;
285
286         return new Integer JavaDoc[]{ new Integer JavaDoc(_year), new Integer JavaDoc(_month), new Integer JavaDoc(_day) };
287     }
288  
289     /**
290      * These utility functions can be replaced by equivalent
291      * functions from ICU if available.
292      */

293     static int mod(long i, int j) {
294         return (int) (i - (long) j * quotient(i, j));
295     }
296     
297     static int quotient(long i, int j) {
298         return (int) Math.floor((double) i / j);
299     }
300 }
301
Popular Tags