]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/util/CECalendar.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / util / CECalendar.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2005-2009, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.util;\r
8 \r
9 import java.util.Date;\r
10 import java.util.Locale;\r
11 \r
12 /**\r
13  * Base class for EthiopicCalendar and CopticCalendar.\r
14  * @internal\r
15  * @deprecated This API is ICU internal only.\r
16  */\r
17 abstract class CECalendar extends Calendar {\r
18     // jdk1.4.2 serialver\r
19     private static final long serialVersionUID = -999547623066414271L;\r
20 \r
21     private static final int LIMITS[][] = {\r
22         // Minimum  Greatest    Least  Maximum\r
23         //           Minimum  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
33         {/*                                  */}, // AM_PM\r
34         {/*                                  */}, // HOUR\r
35         {/*                                  */}, // HOUR_OF_DAY\r
36         {/*                                  */}, // MINUTE\r
37         {/*                                  */}, // SECOND\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
46     };\r
47 \r
48     //-------------------------------------------------------------------------\r
49     // Constructors...\r
50     //-------------------------------------------------------------------------\r
51 \r
52     /**\r
53      * Constructs a default <code>CECalendar</code> using the current time\r
54      * in the default time zone with the default locale.\r
55      */\r
56     protected CECalendar() {\r
57         this(TimeZone.getDefault(), ULocale.getDefault());\r
58     }\r
59 \r
60     /**\r
61      * Constructs a <code>CECalendar</code> based on the current time\r
62      * in the given time zone with the default locale.\r
63      *\r
64      * @param zone The time zone for the new calendar.\r
65      */\r
66     protected CECalendar(TimeZone zone) {\r
67         this(zone, ULocale.getDefault());\r
68     }\r
69 \r
70     /**\r
71      * Constructs a <code>CECalendar</code> based on the current time\r
72      * in the default time zone with the given locale.\r
73      *\r
74      * @param aLocale The locale for the new calendar.\r
75      */\r
76     protected CECalendar(Locale aLocale) {\r
77         this(TimeZone.getDefault(), aLocale);\r
78     }\r
79 \r
80     /**\r
81      * Constructs a <code>CECalendar</code> based on the current time\r
82      * in the default time zone with the given locale.\r
83      *\r
84      * @param locale The locale for the new calendar.\r
85      */\r
86     protected CECalendar(ULocale locale) {\r
87         this(TimeZone.getDefault(), locale);\r
88     }\r
89 \r
90     /**\r
91      * Constructs a <code>CECalendar</code> based on the current time\r
92      * in the given time zone with the given locale.\r
93      *\r
94      * @param zone The time zone for the new calendar.\r
95      *\r
96      * @param aLocale The locale for the new calendar.\r
97      */\r
98     protected CECalendar(TimeZone zone, Locale aLocale) {\r
99         super(zone, aLocale);\r
100         setTimeInMillis(System.currentTimeMillis());\r
101     }\r
102 \r
103     /**\r
104      * Constructs a <code>CECalendar</code> based on the current time\r
105      * in the given time zone with the given locale.\r
106      *\r
107      * @param zone The time zone for the new calendar.\r
108      *\r
109      * @param locale The locale for the new calendar.\r
110      */\r
111     protected CECalendar(TimeZone zone, ULocale locale) {\r
112         super(zone, locale);\r
113         setTimeInMillis(System.currentTimeMillis());\r
114     }\r
115 \r
116     /**\r
117      * Constructs a <code>CECalendar</code> with the given date set\r
118      * in the default time zone with the default locale.\r
119      *\r
120      * @param year      The value used to set the calendar's {@link #YEAR YEAR} time field.\r
121      *\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
124      *\r
125      * @param date      The value used to set the calendar's {@link #DATE DATE} time field.\r
126      */\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
130     }\r
131 \r
132     /**\r
133      * Constructs a <code>CECalendar</code> with the given date set\r
134      * in the default time zone with the default locale.\r
135      *\r
136      * @param date      The date to which the new calendar is set.\r
137      */\r
138     protected CECalendar(Date date) {\r
139         super(TimeZone.getDefault(), ULocale.getDefault());\r
140         this.setTime(date);\r
141     }\r
142 \r
143     /**\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
146      *\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
154      */\r
155     protected CECalendar(int year, int month, int date, int hour,\r
156                          int minute, int second)\r
157     {\r
158         super(TimeZone.getDefault(), ULocale.getDefault());\r
159         this.set(year, month, date, hour, minute, second);\r
160     }\r
161 \r
162     //-------------------------------------------------------------------------\r
163     // Calendar framework\r
164     //-------------------------------------------------------------------------\r
165 \r
166     /**\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
170      */\r
171     abstract protected int getJDEpochOffset();\r
172 \r
173     /**\r
174      * Return JD of start of given month/extended year\r
175      */\r
176     protected int handleComputeMonthStart(int eyear,\r
177                                           int emonth,\r
178                                           boolean useMonth) {\r
179         return ceToJD(eyear, emonth, 0, getJDEpochOffset());\r
180     }\r
181 \r
182     /**\r
183      * Calculate the limit for a specified type of limit and field\r
184      */\r
185     protected int handleGetLimit(int field, int limitType) {\r
186         return LIMITS[field][limitType];\r
187     }\r
188 \r
189     //-------------------------------------------------------------------------\r
190     // Calendar framework\r
191     //-------------------------------------------------------------------------\r
192 \r
193     /**\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
199      */\r
200     public static int ceToJD(long year, int month, int day, int jdEpochOffset) {\r
201 \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
205 \r
206         // handle month > 12, < 0 (e.g. from add/set)\r
207         if ( month >= 0 ) {\r
208             year += month/13;\r
209             month %= 13;\r
210         } else {\r
211             ++month;\r
212             year += month/13 - 1;\r
213             month = month%13 + 12;\r
214         }\r
215         return (int) (\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
221             );\r
222     }\r
223 \r
224     /**\r
225      * Convert a Julian day to an Coptic/Ethiopic year, month and day\r
226      */\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
230 \r
231         c4 = floorDivide(julianDay - jdEpochOffset, 1461, r4);\r
232 \r
233         // exteded year\r
234         fields[0] = 4 * c4 + (r4[0]/365 - r4[0]/1460); // 4 * <number of 4year cycle> + <years within the last cycle>\r
235 \r
236         int doy = (r4[0] == 1460) ? 365 : (r4[0] % 365); // days in present year\r
237 \r
238         // month\r
239         fields[1] = doy / 30; // 30 -> Coptic/Ethiopic month length up to 12th month\r
240         // day\r
241         fields[2] = (doy % 30) + 1; // 1-based days in a month\r
242     }\r
243 }\r