]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/util/GregorianCalendar.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / util / GregorianCalendar.java
1 /*\r
2  * Copyright (C) 1996-2009, International Business Machines\r
3  * Corporation and others.  All Rights Reserved.\r
4  */\r
5 \r
6 package com.ibm.icu.util;\r
7 \r
8 import java.util.Date;\r
9 import java.util.Locale;\r
10 \r
11 /**\r
12  * <code>GregorianCalendar</code> is a concrete subclass of\r
13  * {@link Calendar}\r
14  * and provides the standard calendar used by most of the world.\r
15  *\r
16  * <p>\r
17  * The standard (Gregorian) calendar has 2 eras, BC and AD.\r
18  *\r
19  * <p>\r
20  * This implementation handles a single discontinuity, which corresponds by\r
21  * default to the date the Gregorian calendar was instituted (October 15, 1582\r
22  * in some countries, later in others).  The cutover date may be changed by the\r
23  * caller by calling <code>setGregorianChange()</code>.\r
24  *\r
25  * <p>\r
26  * Historically, in those countries which adopted the Gregorian calendar first,\r
27  * October 4, 1582 was thus followed by October 15, 1582. This calendar models\r
28  * this correctly.  Before the Gregorian cutover, <code>GregorianCalendar</code>\r
29  * implements the Julian calendar.  The only difference between the Gregorian\r
30  * and the Julian calendar is the leap year rule. The Julian calendar specifies\r
31  * leap years every four years, whereas the Gregorian calendar omits century\r
32  * years which are not divisible by 400.\r
33  *\r
34  * <p>\r
35  * <code>GregorianCalendar</code> implements <em>proleptic</em> Gregorian and\r
36  * Julian calendars. That is, dates are computed by extrapolating the current\r
37  * rules indefinitely far backward and forward in time. As a result,\r
38  * <code>GregorianCalendar</code> may be used for all years to generate\r
39  * meaningful and consistent results. However, dates obtained using\r
40  * <code>GregorianCalendar</code> are historically accurate only from March 1, 4\r
41  * AD onward, when modern Julian calendar rules were adopted.  Before this date,\r
42  * leap year rules were applied irregularly, and before 45 BC the Julian\r
43  * calendar did not even exist.\r
44  *\r
45  * <p>\r
46  * Prior to the institution of the Gregorian calendar, New Year's Day was\r
47  * March 25. To avoid confusion, this calendar always uses January 1. A manual\r
48  * adjustment may be made if desired for dates that are prior to the Gregorian\r
49  * changeover and which fall between January 1 and March 24.\r
50  *\r
51  * <p>Values calculated for the <code>WEEK_OF_YEAR</code> field range from 1 to\r
52  * 53.  Week 1 for a year is the earliest seven day period starting on\r
53  * <code>getFirstDayOfWeek()</code> that contains at least\r
54  * <code>getMinimalDaysInFirstWeek()</code> days from that year.  It thus\r
55  * depends on the values of <code>getMinimalDaysInFirstWeek()</code>,\r
56  * <code>getFirstDayOfWeek()</code>, and the day of the week of January 1.\r
57  * Weeks between week 1 of one year and week 1 of the following year are\r
58  * numbered sequentially from 2 to 52 or 53 (as needed).\r
59 \r
60  * <p>For example, January 1, 1998 was a Thursday.  If\r
61  * <code>getFirstDayOfWeek()</code> is <code>MONDAY</code> and\r
62  * <code>getMinimalDaysInFirstWeek()</code> is 4 (these are the values\r
63  * reflecting ISO 8601 and many national standards), then week 1 of 1998 starts\r
64  * on December 29, 1997, and ends on January 4, 1998.  If, however,\r
65  * <code>getFirstDayOfWeek()</code> is <code>SUNDAY</code>, then week 1 of 1998\r
66  * starts on January 4, 1998, and ends on January 10, 1998; the first three days\r
67  * of 1998 then are part of week 53 of 1997.\r
68  *\r
69  * <p>Values calculated for the <code>WEEK_OF_MONTH</code> field range from 0 or\r
70  * 1 to 4 or 5.  Week 1 of a month (the days with <code>WEEK_OF_MONTH =\r
71  * 1</code>) is the earliest set of at least\r
72  * <code>getMinimalDaysInFirstWeek()</code> contiguous days in that month,\r
73  * ending on the day before <code>getFirstDayOfWeek()</code>.  Unlike\r
74  * week 1 of a year, week 1 of a month may be shorter than 7 days, need\r
75  * not start on <code>getFirstDayOfWeek()</code>, and will not include days of\r
76  * the previous month.  Days of a month before week 1 have a\r
77  * <code>WEEK_OF_MONTH</code> of 0.\r
78  *\r
79  * <p>For example, if <code>getFirstDayOfWeek()</code> is <code>SUNDAY</code>\r
80  * and <code>getMinimalDaysInFirstWeek()</code> is 4, then the first week of\r
81  * January 1998 is Sunday, January 4 through Saturday, January 10.  These days\r
82  * have a <code>WEEK_OF_MONTH</code> of 1.  Thursday, January 1 through\r
83  * Saturday, January 3 have a <code>WEEK_OF_MONTH</code> of 0.  If\r
84  * <code>getMinimalDaysInFirstWeek()</code> is changed to 3, then January 1\r
85  * through January 3 have a <code>WEEK_OF_MONTH</code> of 1.\r
86  *\r
87  * <p>\r
88  * <strong>Example:</strong>\r
89  * <blockquote>\r
90  * <pre>\r
91  * // get the supported ids for GMT-08:00 (Pacific Standard Time)\r
92  * String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);\r
93  * // if no ids were returned, something is wrong. get out.\r
94  * if (ids.length == 0)\r
95  *     System.exit(0);\r
96  *\r
97  *  // begin output\r
98  * System.out.println("Current Time");\r
99  *\r
100  * // create a Pacific Standard Time time zone\r
101  * SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);\r
102  *\r
103  * // set up rules for daylight savings time\r
104  * pdt.setStartRule(Calendar.MARCH, 2, Calendar.SUNDAY, 2 * 60 * 60 * 1000);\r
105  * pdt.setEndRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);\r
106  *\r
107  * // create a GregorianCalendar with the Pacific Daylight time zone\r
108  * // and the current date and time\r
109  * Calendar calendar = new GregorianCalendar(pdt);\r
110  * Date trialTime = new Date();\r
111  * calendar.setTime(trialTime);\r
112  *\r
113  * // print out a bunch of interesting things\r
114  * System.out.println("ERA: " + calendar.get(Calendar.ERA));\r
115  * System.out.println("YEAR: " + calendar.get(Calendar.YEAR));\r
116  * System.out.println("MONTH: " + calendar.get(Calendar.MONTH));\r
117  * System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));\r
118  * System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));\r
119  * System.out.println("DATE: " + calendar.get(Calendar.DATE));\r
120  * System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));\r
121  * System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));\r
122  * System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));\r
123  * System.out.println("DAY_OF_WEEK_IN_MONTH: "\r
124  *                    + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));\r
125  * System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));\r
126  * System.out.println("HOUR: " + calendar.get(Calendar.HOUR));\r
127  * System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));\r
128  * System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));\r
129  * System.out.println("SECOND: " + calendar.get(Calendar.SECOND));\r
130  * System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));\r
131  * System.out.println("ZONE_OFFSET: "\r
132  *                    + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));\r
133  * System.out.println("DST_OFFSET: "\r
134  *                    + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));\r
135 \r
136  * System.out.println("Current Time, with hour reset to 3");\r
137  * calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override\r
138  * calendar.set(Calendar.HOUR, 3);\r
139  * System.out.println("ERA: " + calendar.get(Calendar.ERA));\r
140  * System.out.println("YEAR: " + calendar.get(Calendar.YEAR));\r
141  * System.out.println("MONTH: " + calendar.get(Calendar.MONTH));\r
142  * System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));\r
143  * System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));\r
144  * System.out.println("DATE: " + calendar.get(Calendar.DATE));\r
145  * System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));\r
146  * System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));\r
147  * System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));\r
148  * System.out.println("DAY_OF_WEEK_IN_MONTH: "\r
149  *                    + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));\r
150  * System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));\r
151  * System.out.println("HOUR: " + calendar.get(Calendar.HOUR));\r
152  * System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));\r
153  * System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));\r
154  * System.out.println("SECOND: " + calendar.get(Calendar.SECOND));\r
155  * System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));\r
156  * System.out.println("ZONE_OFFSET: "\r
157  *        + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours\r
158  * System.out.println("DST_OFFSET: "\r
159  *        + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours</pre>\r
160  * </blockquote>\r
161  * <p>\r
162  * GregorianCalendar usually should be instantiated using \r
163  * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code>\r
164  * with the tag <code>"@calendar=gregorian"</code>.</p>\r
165 \r
166  * @see          Calendar\r
167  * @see          TimeZone\r
168  * @author David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu\r
169  * @stable ICU 2.0\r
170  */\r
171 public class GregorianCalendar extends Calendar {\r
172     // jdk1.4.2 serialver\r
173     private static final long serialVersionUID = 9199388694351062137L;\r
174 \r
175     /*\r
176      * Implementation Notes\r
177      *\r
178      * The Julian day number, as used here, is a modified number which has its\r
179      * onset at midnight, rather than noon.\r
180      *\r
181      * The epoch is the number of days or milliseconds from some defined\r
182      * starting point. The epoch for java.util.Date is used here; that is,\r
183      * milliseconds from January 1, 1970 (Gregorian), midnight UTC.  Other\r
184      * epochs which are used are January 1, year 1 (Gregorian), which is day 1\r
185      * of the Gregorian calendar, and December 30, year 0 (Gregorian), which is\r
186      * day 1 of the Julian calendar.\r
187      *\r
188      * We implement the proleptic Julian and Gregorian calendars.  This means we\r
189      * implement the modern definition of the calendar even though the\r
190      * historical usage differs.  For example, if the Gregorian change is set\r
191      * to new Date(Long.MIN_VALUE), we have a pure Gregorian calendar which\r
192      * labels dates preceding the invention of the Gregorian calendar in 1582 as\r
193      * if the calendar existed then.\r
194      *\r
195      * Likewise, with the Julian calendar, we assume a consistent 4-year leap\r
196      * rule, even though the historical pattern of leap years is irregular,\r
197      * being every 3 years from 45 BC through 9 BC, then every 4 years from 8 AD\r
198      * onwards, with no leap years in-between.  Thus date computations and\r
199      * functions such as isLeapYear() are not intended to be historically\r
200      * accurate.\r
201      *\r
202      * Given that milliseconds are a long, day numbers such as Julian day\r
203      * numbers, Gregorian or Julian calendar days, or epoch days, are also\r
204      * longs. Years can fit into an int.\r
205      */\r
206 \r
207 //////////////////\r
208 // Class Variables\r
209 //////////////////\r
210 \r
211     /**\r
212      * Value of the <code>ERA</code> field indicating\r
213      * the period before the common era (before Christ), also known as BCE.\r
214      * The sequence of years at the transition from <code>BC</code> to <code>AD</code> is\r
215      * ..., 2 BC, 1 BC, 1 AD, 2 AD,...\r
216      * @see Calendar#ERA\r
217      * @stable ICU 2.0\r
218      */\r
219     public static final int BC = 0;\r
220 \r
221     /**\r
222      * Value of the <code>ERA</code> field indicating\r
223      * the common era (Anno Domini), also known as CE.\r
224      * The sequence of years at the transition from <code>BC</code> to <code>AD</code> is\r
225      * ..., 2 BC, 1 BC, 1 AD, 2 AD,...\r
226      * @see Calendar#ERA\r
227      * @stable ICU 2.0\r
228      */\r
229     public static final int AD = 1;\r
230 \r
231     private static final int EPOCH_YEAR = 1970;\r
232 \r
233     private static final int[][] MONTH_COUNT = {\r
234         //len len2   st  st2\r
235         {  31,  31,   0,   0 }, // Jan\r
236         {  28,  29,  31,  31 }, // Feb\r
237         {  31,  31,  59,  60 }, // Mar\r
238         {  30,  30,  90,  91 }, // Apr\r
239         {  31,  31, 120, 121 }, // May\r
240         {  30,  30, 151, 152 }, // Jun\r
241         {  31,  31, 181, 182 }, // Jul\r
242         {  31,  31, 212, 213 }, // Aug\r
243         {  30,  30, 243, 244 }, // Sep\r
244         {  31,  31, 273, 274 }, // Oct\r
245         {  30,  30, 304, 305 }, // Nov\r
246         {  31,  31, 334, 335 }  // Dec\r
247         // len  length of month\r
248         // len2 length of month in a leap year\r
249         // st   days in year before start of month\r
250         // st2  days in year before month in leap year\r
251     };\r
252     \r
253     /**\r
254      * Old year limits were least max 292269054, max 292278994.\r
255      */\r
256     private static final int LIMITS[][] = {\r
257         // Minimum  Greatest    Least  Maximum\r
258         //           Minimum  Maximum\r
259         {        0,        0,       1,       1 }, // ERA\r
260         {        1,        1, 5828963, 5838270 }, // YEAR\r
261         {        0,        0,      11,      11 }, // MONTH\r
262         {        1,        1,      52,      53 }, // WEEK_OF_YEAR\r
263         {/*                                  */}, // WEEK_OF_MONTH\r
264         {        1,        1,      28,      31 }, // DAY_OF_MONTH\r
265         {        1,        1,     365,     366 }, // DAY_OF_YEAR\r
266         {/*                                  */}, // DAY_OF_WEEK\r
267         {       -1,       -1,       4,       5 }, // DAY_OF_WEEK_IN_MONTH\r
268         {/*                                  */}, // AM_PM\r
269         {/*                                  */}, // HOUR\r
270         {/*                                  */}, // HOUR_OF_DAY\r
271         {/*                                  */}, // MINUTE\r
272         {/*                                  */}, // SECOND\r
273         {/*                                  */}, // MILLISECOND\r
274         {/*                                  */}, // ZONE_OFFSET\r
275         {/*                                  */}, // DST_OFFSET\r
276         { -5838270, -5838270, 5828964, 5838271 }, // YEAR_WOY\r
277         {/*                                  */}, // DOW_LOCAL\r
278         { -5838269, -5838269, 5828963, 5838270 }, // EXTENDED_YEAR\r
279         {/*                                  */}, // JULIAN_DAY\r
280         {/*                                  */}, // MILLISECONDS_IN_DAY\r
281     };\r
282 \r
283     /**\r
284      * @stable ICU 2.0\r
285      */\r
286     protected int handleGetLimit(int field, int limitType) {\r
287         return LIMITS[field][limitType];\r
288     }\r
289 \r
290 /////////////////////\r
291 // Instance Variables\r
292 /////////////////////\r
293 \r
294     /**\r
295      * The point at which the Gregorian calendar rules are used, measured in\r
296      * milliseconds from the standard epoch.  Default is October 15, 1582\r
297      * (Gregorian) 00:00:00 UTC or -12219292800000L.  For this value, October 4,\r
298      * 1582 (Julian) is followed by October 15, 1582 (Gregorian).  This\r
299      * corresponds to Julian day number 2299161.\r
300      * @serial\r
301      */\r
302     private long gregorianCutover = -12219292800000L;\r
303 \r
304     /**\r
305      * Julian day number of the Gregorian cutover.\r
306      */\r
307     private transient int cutoverJulianDay = 2299161;\r
308     \r
309     /**\r
310      * The year of the gregorianCutover, with 0 representing\r
311      * 1 BC, -1 representing 2 BC, etc.\r
312      */\r
313     private transient int gregorianCutoverYear = 1582;\r
314 \r
315     /**\r
316      * Used by handleComputeJulianDay() and handleComputeMonthStart().\r
317      * @stable ICU 2.0\r
318      */\r
319     transient protected boolean isGregorian;\r
320 \r
321     /**\r
322      * Used by handleComputeJulianDay() and handleComputeMonthStart().\r
323      * @stable ICU 2.0\r
324      */\r
325     transient protected boolean invertGregorian;\r
326 \r
327 ///////////////\r
328 // Constructors\r
329 ///////////////\r
330 \r
331     /**\r
332      * Constructs a default GregorianCalendar using the current time\r
333      * in the default time zone with the default locale.\r
334      * @stable ICU 2.0\r
335      */\r
336     public GregorianCalendar() {\r
337         this(TimeZone.getDefault(), ULocale.getDefault());\r
338     }\r
339 \r
340     /**\r
341      * Constructs a GregorianCalendar based on the current time\r
342      * in the given time zone with the default locale.\r
343      * @param zone the given time zone.\r
344      * @stable ICU 2.0\r
345      */\r
346     public GregorianCalendar(TimeZone zone) {\r
347         this(zone, ULocale.getDefault());\r
348     }\r
349 \r
350     /**\r
351      * Constructs a GregorianCalendar based on the current time\r
352      * in the default time zone with the given locale.\r
353      * @param aLocale the given locale.\r
354      * @stable ICU 2.0\r
355      */\r
356     public GregorianCalendar(Locale aLocale) {\r
357         this(TimeZone.getDefault(), aLocale);\r
358     }\r
359 \r
360     /**\r
361      * Constructs a GregorianCalendar based on the current time\r
362      * in the default time zone with the given locale.\r
363      * @param locale the given ulocale.\r
364      * @stable ICU 3.2\r
365      */\r
366     public GregorianCalendar(ULocale locale) {\r
367         this(TimeZone.getDefault(), locale);\r
368     }\r
369 \r
370     /**\r
371      * Constructs a GregorianCalendar based on the current time\r
372      * in the given time zone with the given locale.\r
373      * @param zone the given time zone.\r
374      * @param aLocale the given locale.\r
375      * @stable ICU 2.0\r
376      */\r
377     public GregorianCalendar(TimeZone zone, Locale aLocale) {\r
378         super(zone, aLocale);\r
379         setTimeInMillis(System.currentTimeMillis());\r
380     }\r
381 \r
382     /**\r
383      * Constructs a GregorianCalendar based on the current time\r
384      * in the given time zone with the given locale.\r
385      * @param zone the given time zone.\r
386      * @param locale the given ulocale.\r
387      * @stable ICU 3.2\r
388      */\r
389     public GregorianCalendar(TimeZone zone, ULocale locale) {\r
390         super(zone, locale);\r
391         setTimeInMillis(System.currentTimeMillis());\r
392     }\r
393 \r
394     /**\r
395      * Constructs a GregorianCalendar with the given date set\r
396      * in the default time zone with the default locale.\r
397      * @param year the value used to set the YEAR time field in the calendar.\r
398      * @param month the value used to set the MONTH time field in the calendar.\r
399      * Month value is 0-based. e.g., 0 for January.\r
400      * @param date the value used to set the DATE time field in the calendar.\r
401      * @stable ICU 2.0\r
402      */\r
403     public GregorianCalendar(int year, int month, int date) {\r
404         super(TimeZone.getDefault(), ULocale.getDefault());\r
405         set(ERA, AD);\r
406         set(YEAR, year);\r
407         set(MONTH, month);\r
408         set(DATE, date);\r
409     }\r
410 \r
411     /**\r
412      * Constructs a GregorianCalendar with the given date\r
413      * and time set for the default time zone with the default locale.\r
414      * @param year the value used to set the YEAR time field in the calendar.\r
415      * @param month the value used to set the MONTH time field in the calendar.\r
416      * Month value is 0-based. e.g., 0 for January.\r
417      * @param date the value used to set the DATE time field in the calendar.\r
418      * @param hour the value used to set the HOUR_OF_DAY time field\r
419      * in the calendar.\r
420      * @param minute the value used to set the MINUTE time field\r
421      * in the calendar.\r
422      * @stable ICU 2.0\r
423      */\r
424     public GregorianCalendar(int year, int month, int date, int hour,\r
425                              int minute) {\r
426         super(TimeZone.getDefault(), ULocale.getDefault());\r
427         set(ERA, AD);\r
428         set(YEAR, year);\r
429         set(MONTH, month);\r
430         set(DATE, date);\r
431         set(HOUR_OF_DAY, hour);\r
432         set(MINUTE, minute);\r
433     }\r
434 \r
435     /**\r
436      * Constructs a GregorianCalendar with the given date\r
437      * and time set for the default time zone with the default locale.\r
438      * @param year the value used to set the YEAR time field in the calendar.\r
439      * @param month the value used to set the MONTH time field in the calendar.\r
440      * Month value is 0-based. e.g., 0 for January.\r
441      * @param date the value used to set the DATE time field in the calendar.\r
442      * @param hour the value used to set the HOUR_OF_DAY time field\r
443      * in the calendar.\r
444      * @param minute the value used to set the MINUTE time field\r
445      * in the calendar.\r
446      * @param second the value used to set the SECOND time field\r
447      * in the calendar.\r
448      * @stable ICU 2.0\r
449      */\r
450     public GregorianCalendar(int year, int month, int date, int hour,\r
451                              int minute, int second) {\r
452         super(TimeZone.getDefault(), ULocale.getDefault());\r
453         set(ERA, AD);\r
454         set(YEAR, year);\r
455         set(MONTH, month);\r
456         set(DATE, date);\r
457         set(HOUR_OF_DAY, hour);\r
458         set(MINUTE, minute);\r
459         set(SECOND, second);\r
460     }\r
461 \r
462 /////////////////\r
463 // Public methods\r
464 /////////////////\r
465 \r
466     /**\r
467      * Sets the GregorianCalendar change date. This is the point when the switch\r
468      * from Julian dates to Gregorian dates occurred. Default is October 15,\r
469      * 1582. Previous to this, dates will be in the Julian calendar.\r
470      * <p>\r
471      * To obtain a pure Julian calendar, set the change date to\r
472      * <code>Date(Long.MAX_VALUE)</code>.  To obtain a pure Gregorian calendar,\r
473      * set the change date to <code>Date(Long.MIN_VALUE)</code>.\r
474      *\r
475      * @param date the given Gregorian cutover date.\r
476      * @stable ICU 2.0\r
477      */\r
478     public void setGregorianChange(Date date) {\r
479         gregorianCutover = date.getTime();\r
480 \r
481         // If the cutover has an extreme value, then create a pure\r
482         // Gregorian or pure Julian calendar by giving the cutover year and\r
483         // JD extreme values.\r
484         if (gregorianCutover <= MIN_MILLIS) {\r
485             gregorianCutoverYear = cutoverJulianDay = Integer.MIN_VALUE;\r
486         } else if (gregorianCutover >= MAX_MILLIS) {\r
487             gregorianCutoverYear = cutoverJulianDay = Integer.MAX_VALUE;\r
488         } else {\r
489             // Precompute two internal variables which we use to do the actual\r
490             // cutover computations.  These are the Julian day of the cutover\r
491             // and the cutover year.\r
492             cutoverJulianDay = (int) floorDivide(gregorianCutover, ONE_DAY);\r
493             \r
494             // Convert cutover millis to extended year\r
495             GregorianCalendar cal = new GregorianCalendar(getTimeZone());\r
496             cal.setTime(date);\r
497             gregorianCutoverYear = cal.get(EXTENDED_YEAR);\r
498         }\r
499     }\r
500 \r
501     /**\r
502      * Gets the Gregorian Calendar change date.  This is the point when the\r
503      * switch from Julian dates to Gregorian dates occurred. Default is\r
504      * October 15, 1582. Previous to this, dates will be in the Julian\r
505      * calendar.\r
506      * @return the Gregorian cutover date for this calendar.\r
507      * @stable ICU 2.0\r
508      */\r
509     public final Date getGregorianChange() {\r
510         return new Date(gregorianCutover);\r
511     }\r
512 \r
513     /**\r
514      * Determines if the given year is a leap year. Returns true if the\r
515      * given year is a leap year.\r
516      * @param year the given year.\r
517      * @return true if the given year is a leap year; false otherwise.\r
518      * @stable ICU 2.0\r
519      */\r
520     public boolean isLeapYear(int year) {\r
521         return year >= gregorianCutoverYear ?\r
522             ((year%4 == 0) && ((year%100 != 0) || (year%400 == 0))) : // Gregorian\r
523             (year%4 == 0); // Julian\r
524     }\r
525 \r
526     /**\r
527      * Returns true if the given Calendar object is equivalent to this\r
528      * one.  Calendar override.\r
529      *\r
530      * @param other the Calendar to be compared with this Calendar   \r
531      * @stable ICU 2.4\r
532      */\r
533     public boolean isEquivalentTo(Calendar other) {\r
534         return super.isEquivalentTo(other) &&\r
535             gregorianCutover == ((GregorianCalendar)other).gregorianCutover;\r
536     }\r
537 \r
538     /**\r
539      * Override hashCode.\r
540      * Generates the hash code for the GregorianCalendar object\r
541      * @stable ICU 2.0\r
542      */\r
543     public int hashCode() {\r
544         return super.hashCode() ^ (int)gregorianCutover;\r
545     }\r
546 \r
547     /**\r
548      * Roll a field by a signed amount.\r
549      * @stable ICU 2.0\r
550      */\r
551     public void roll(int field, int amount) {\r
552 \r
553         switch (field) {\r
554         case WEEK_OF_YEAR:\r
555             {\r
556                 // Unlike WEEK_OF_MONTH, WEEK_OF_YEAR never shifts the day of the\r
557                 // week.  Also, rolling the week of the year can have seemingly\r
558                 // strange effects simply because the year of the week of year\r
559                 // may be different from the calendar year.  For example, the\r
560                 // date Dec 28, 1997 is the first day of week 1 of 1998 (if\r
561                 // weeks start on Sunday and the minimal days in first week is\r
562                 // <= 3).\r
563                 int woy = get(WEEK_OF_YEAR);\r
564                 // Get the ISO year, which matches the week of year.  This\r
565                 // may be one year before or after the calendar year.\r
566                 int isoYear = get(YEAR_WOY);\r
567                 int isoDoy = internalGet(DAY_OF_YEAR);\r
568                 if (internalGet(MONTH) == Calendar.JANUARY) {\r
569                     if (woy >= 52) {\r
570                         isoDoy += handleGetYearLength(isoYear);\r
571                     }\r
572                 } else {\r
573                     if (woy == 1) {\r
574                         isoDoy -= handleGetYearLength(isoYear - 1);\r
575                     }\r
576                 }\r
577                 woy += amount;\r
578                 // Do fast checks to avoid unnecessary computation:\r
579                 if (woy < 1 || woy > 52) {\r
580                     // Determine the last week of the ISO year.\r
581                     // We do this using the standard formula we use\r
582                     // everywhere in this file.  If we can see that the\r
583                     // days at the end of the year are going to fall into\r
584                     // week 1 of the next year, we drop the last week by\r
585                     // subtracting 7 from the last day of the year.\r
586                     int lastDoy = handleGetYearLength(isoYear);\r
587                     int lastRelDow = (lastDoy - isoDoy + internalGet(DAY_OF_WEEK) -\r
588                                       getFirstDayOfWeek()) % 7;\r
589                     if (lastRelDow < 0) lastRelDow += 7;\r
590                     if ((6 - lastRelDow) >= getMinimalDaysInFirstWeek()) lastDoy -= 7;\r
591                     int lastWoy = weekNumber(lastDoy, lastRelDow + 1);\r
592                     woy = ((woy + lastWoy - 1) % lastWoy) + 1;\r
593                 }\r
594                 set(WEEK_OF_YEAR, woy);\r
595                 set(YEAR, isoYear); // Why not YEAR_WOY? - Alan 11/6/00\r
596                 return;\r
597             }\r
598 \r
599         default:\r
600             super.roll(field, amount);\r
601             return;\r
602         }\r
603     }\r
604 \r
605     /**\r
606      * Return the minimum value that this field could have, given the current date.\r
607      * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().\r
608      * @stable ICU 2.0\r
609      */\r
610     public int getActualMinimum(int field) {\r
611         return getMinimum(field);\r
612     }\r
613 \r
614     /**\r
615      * Return the maximum value that this field could have, given the current date.\r
616      * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual\r
617      * maximum would be 28; for "Feb 3, 1996" it s 29.  Similarly for a Hebrew calendar,\r
618      * for some years the actual maximum for MONTH is 12, and for others 13.\r
619      * @stable ICU 2.0\r
620      */\r
621     public int getActualMaximum(int field) {\r
622         /* It is a known limitation that the code here (and in getActualMinimum)\r
623          * won't behave properly at the extreme limits of GregorianCalendar's\r
624          * representable range (except for the code that handles the YEAR\r
625          * field).  That's because the ends of the representable range are at\r
626          * odd spots in the year.  For calendars with the default Gregorian\r
627          * cutover, these limits are Sun Dec 02 16:47:04 GMT 292269055 BC to Sun\r
628          * Aug 17 07:12:55 GMT 292278994 AD, somewhat different for non-GMT\r
629          * zones.  As a result, if the calendar is set to Aug 1 292278994 AD,\r
630          * the actual maximum of DAY_OF_MONTH is 17, not 30.  If the date is Mar\r
631          * 31 in that year, the actual maximum month might be Jul, whereas is\r
632          * the date is Mar 15, the actual maximum might be Aug -- depending on\r
633          * the precise semantics that are desired.  Similar considerations\r
634          * affect all fields.  Nonetheless, this effect is sufficiently arcane\r
635          * that we permit it, rather than complicating the code to handle such\r
636          * intricacies. - liu 8/20/98\r
637 \r
638          * UPDATE: No longer true, since we have pulled in the limit values on\r
639          * the year. - Liu 11/6/00 */\r
640 \r
641         switch (field) {\r
642 \r
643         case YEAR:\r
644             /* The year computation is no different, in principle, from the\r
645              * others, however, the range of possible maxima is large.  In\r
646              * addition, the way we know we've exceeded the range is different.\r
647              * For these reasons, we use the special case code below to handle\r
648              * this field.\r
649              *\r
650              * The actual maxima for YEAR depend on the type of calendar:\r
651              *\r
652              *     Gregorian = May 17, 292275056 BC - Aug 17, 292278994 AD\r
653              *     Julian    = Dec  2, 292269055 BC - Jan  3, 292272993 AD\r
654              *     Hybrid    = Dec  2, 292269055 BC - Aug 17, 292278994 AD\r
655              *\r
656              * We know we've exceeded the maximum when either the month, date,\r
657              * time, or era changes in response to setting the year.  We don't\r
658              * check for month, date, and time here because the year and era are\r
659              * sufficient to detect an invalid year setting.  NOTE: If code is\r
660              * added to check the month and date in the future for some reason,\r
661              * Feb 29 must be allowed to shift to Mar 1 when setting the year.\r
662              */\r
663             {\r
664                 Calendar cal = (Calendar) clone();\r
665                 cal.setLenient(true);\r
666                 \r
667                 int era = cal.get(ERA);\r
668                 Date d = cal.getTime();\r
669 \r
670                 /* Perform a binary search, with the invariant that lowGood is a\r
671                  * valid year, and highBad is an out of range year.\r
672                  */\r
673                 int lowGood = LIMITS[YEAR][1];\r
674                 int highBad = LIMITS[YEAR][2]+1;\r
675                 while ((lowGood + 1) < highBad) {\r
676                     int y = (lowGood + highBad) / 2;\r
677                     cal.set(YEAR, y);\r
678                     if (cal.get(YEAR) == y && cal.get(ERA) == era) {\r
679                         lowGood = y;\r
680                     } else {\r
681                         highBad = y;\r
682                         cal.setTime(d); // Restore original fields\r
683                     }\r
684                 }\r
685                 \r
686                 return lowGood;\r
687             }\r
688 \r
689         default:\r
690             return super.getActualMaximum(field);\r
691         }\r
692     }\r
693 \r
694 //////////////////////\r
695 // Proposed public API\r
696 //////////////////////\r
697 \r
698     /**\r
699      * Return true if the current time for this Calendar is in Daylignt\r
700      * Savings Time.\r
701      */\r
702     boolean inDaylightTime() {\r
703         if (!getTimeZone().useDaylightTime()) return false;\r
704         complete(); // Force update of DST_OFFSET field\r
705         return internalGet(DST_OFFSET) != 0;\r
706     }\r
707 \r
708 \r
709 /////////////////////\r
710 // Calendar framework\r
711 /////////////////////\r
712 \r
713     /**\r
714      * @stable ICU 2.0\r
715      */\r
716     protected int handleGetMonthLength(int extendedYear, int month) {\r
717         // If the month is out of range, adjust it into range, and\r
718         // modify the extended year value accordingly.\r
719         if (month < 0 || month > 11) {\r
720             int[] rem = new int[1];\r
721             extendedYear += floorDivide(month, 12, rem);\r
722             month = rem[0];\r
723         }\r
724 \r
725         return MONTH_COUNT[month][isLeapYear(extendedYear)?1:0];\r
726     }\r
727 \r
728     /**\r
729      * @stable ICU 2.0\r
730      */\r
731     protected int handleGetYearLength(int eyear) {\r
732         return isLeapYear(eyear) ? 366 : 365;\r
733     }\r
734 \r
735 /////////////////////////////\r
736 // Time => Fields computation\r
737 /////////////////////////////\r
738 \r
739     /**\r
740      * Override Calendar to compute several fields specific to the hybrid\r
741      * Gregorian-Julian calendar system.  These are:\r
742      *\r
743      * <ul><li>ERA\r
744      * <li>YEAR\r
745      * <li>MONTH\r
746      * <li>DAY_OF_MONTH\r
747      * <li>DAY_OF_YEAR\r
748      * <li>EXTENDED_YEAR</ul>\r
749      * @stable ICU 2.0\r
750      */\r
751     protected void handleComputeFields(int julianDay) {\r
752         int eyear, month, dayOfMonth, dayOfYear;\r
753 \r
754         if (julianDay >= cutoverJulianDay) {\r
755             month = getGregorianMonth();\r
756             dayOfMonth = getGregorianDayOfMonth();\r
757             dayOfYear = getGregorianDayOfYear();\r
758             eyear = getGregorianYear();\r
759         } else {\r
760             // The Julian epoch day (not the same as Julian Day)\r
761             // is zero on Saturday December 30, 0 (Gregorian).\r
762             long julianEpochDay = julianDay - (JAN_1_1_JULIAN_DAY - 2);\r
763             eyear = (int) floorDivide(4*julianEpochDay + 1464, 1461);\r
764             \r
765             // Compute the Julian calendar day number for January 1, eyear\r
766             long january1 = 365*(eyear-1) + floorDivide(eyear-1, 4);\r
767             dayOfYear = (int)(julianEpochDay - january1); // 0-based\r
768             \r
769             // Julian leap years occurred historically every 4 years starting\r
770             // with 8 AD.  Before 8 AD the spacing is irregular; every 3 years\r
771             // from 45 BC to 9 BC, and then none until 8 AD.  However, we don't\r
772             // implement this historical detail; instead, we implement the\r
773             // computatinally cleaner proleptic calendar, which assumes\r
774             // consistent 4-year cycles throughout time.\r
775             boolean isLeap = ((eyear&0x3) == 0); // equiv. to (eyear%4 == 0)\r
776             \r
777             // Common Julian/Gregorian calculation\r
778             int correction = 0;\r
779             int march1 = isLeap ? 60 : 59; // zero-based DOY for March 1\r
780             if (dayOfYear >= march1) {\r
781                 correction = isLeap ? 1 : 2;\r
782             }\r
783             month = (12 * (dayOfYear + correction) + 6) / 367; // zero-based month\r
784             dayOfMonth = dayOfYear - MONTH_COUNT[month][isLeap?3:2] + 1; // one-based DOM\r
785             ++dayOfYear;\r
786         }\r
787         internalSet(MONTH, month);\r
788         internalSet(DAY_OF_MONTH, dayOfMonth);\r
789         internalSet(DAY_OF_YEAR, dayOfYear);\r
790         internalSet(EXTENDED_YEAR, eyear);\r
791         int era = AD;\r
792         if (eyear < 1) {\r
793             era = BC;\r
794             eyear = 1 - eyear;\r
795         }\r
796         internalSet(ERA, era);\r
797         internalSet(YEAR, eyear);\r
798     }\r
799 \r
800 /////////////////////////////\r
801 // Fields => Time computation\r
802 /////////////////////////////\r
803 \r
804     /**\r
805      * @stable ICU 2.0\r
806      */\r
807     protected int handleGetExtendedYear() {\r
808         int year;\r
809         if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {\r
810             year = internalGet(EXTENDED_YEAR, EPOCH_YEAR);\r
811         } else {\r
812             // The year defaults to the epoch start, the era to AD\r
813             int era = internalGet(ERA, AD);\r
814             if (era == BC) {\r
815                 year = 1 - internalGet(YEAR, 1); // Convert to extended year\r
816             } else {\r
817                 year = internalGet(YEAR, EPOCH_YEAR);\r
818             }\r
819         }\r
820         return year;\r
821     }\r
822 \r
823     /**\r
824      * @stable ICU 2.0\r
825      */\r
826     protected int handleComputeJulianDay(int bestField) {\r
827 \r
828         invertGregorian = false;\r
829 \r
830         int jd = super.handleComputeJulianDay(bestField);\r
831 \r
832         // The following check handles portions of the cutover year BEFORE the\r
833         // cutover itself happens.\r
834         if (isGregorian != (jd >= cutoverJulianDay)) {\r
835             invertGregorian = true;\r
836             jd = super.handleComputeJulianDay(bestField);\r
837         }\r
838         \r
839         return jd;\r
840     }\r
841 \r
842     /**\r
843      * Return JD of start of given month/year\r
844      * @stable ICU 2.0\r
845      */\r
846     protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) {\r
847 \r
848         // If the month is out of range, adjust it into range, and\r
849         // modify the extended year value accordingly.\r
850         if (month < 0 || month > 11) {\r
851             int[] rem = new int[1];\r
852             eyear += floorDivide(month, 12, rem);\r
853             month = rem[0];\r
854         }\r
855 \r
856         boolean isLeap = eyear%4 == 0;\r
857         int y = eyear - 1;\r
858         int julianDay = 365*y + floorDivide(y, 4) + (JAN_1_1_JULIAN_DAY - 3);\r
859 \r
860         isGregorian = (eyear >= gregorianCutoverYear);\r
861         if (invertGregorian) {\r
862             isGregorian = !isGregorian;\r
863         }\r
864         if (isGregorian) {\r
865             isLeap = isLeap && ((eyear%100 != 0) || (eyear%400 == 0));\r
866             // Add 2 because Gregorian calendar starts 2 days after\r
867             // Julian calendar\r
868             julianDay += floorDivide(y, 400) - floorDivide(y, 100) + 2;\r
869         }\r
870 \r
871         // At this point julianDay indicates the day BEFORE the first\r
872         // day of January 1, <eyear> of either the Julian or Gregorian\r
873         // calendar.\r
874 \r
875         if (month != 0) {\r
876             julianDay += MONTH_COUNT[month][isLeap?3:2];\r
877         }\r
878 \r
879         return julianDay;\r
880     }\r
881 \r
882     /**\r
883      * Return the current Calendar type.\r
884      * @return type of calendar\r
885      * @stable ICU 3.8\r
886      */\r
887     public String getType() {\r
888         return "gregorian";\r
889     }\r
890 \r
891     /*\r
892     private static CalendarFactory factory;\r
893     public static CalendarFactory factory() {\r
894         if (factory == null) {\r
895             factory = new CalendarFactory() {\r
896                 public Calendar create(TimeZone tz, ULocale loc) {\r
897                     return new GregorianCalendar(tz, loc);\r
898                 }\r
899 \r
900                 public String factoryName() {\r
901                     return "Gregorian";\r
902                 }\r
903             };\r
904         }\r
905         return factory;\r
906     }\r
907     */\r
908 }\r