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