2 * Copyright (C) 1996-2010, International Business Machines
\r
3 * Corporation and others. All Rights Reserved.
\r
6 package com.ibm.icu.util;
\r
8 import java.util.Date;
\r
9 import java.util.Locale;
\r
12 * {@icuenhanced java.util.GregorianCalendar}.{@icu _usage_}
\r
14 * <p><code>GregorianCalendar</code> is a concrete subclass of
\r
16 * and provides the standard calendar used by most of the world.
\r
18 * <p>The standard (Gregorian) calendar has 2 eras, BC and AD.
\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
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
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
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
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
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
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
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
85 * <strong>Example:</strong>
\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
95 * System.out.println("Current Time");
\r
97 * // create a Pacific Standard Time time zone
\r
98 * SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
\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
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
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
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
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
165 * @author David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu
\r
168 public class GregorianCalendar extends Calendar {
\r
169 // jdk1.4.2 serialver
\r
170 private static final long serialVersionUID = 9199388694351062137L;
\r
173 * Implementation Notes
\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
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
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
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
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
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
216 public static final int BC = 0;
\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
226 public static final int AD = 1;
\r
228 private static final int EPOCH_YEAR = 1970;
\r
230 private static final int[][] MONTH_COUNT = {
\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
251 * Old year limits were least max 292269054, max 292278994.
\r
253 private static final int LIMITS[][] = {
\r
254 // Minimum Greatest Least 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
267 {/* */}, // HOUR_OF_DAY
\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
283 protected int handleGetLimit(int field, int limitType) {
\r
284 return LIMITS[field][limitType];
\r
287 /////////////////////
\r
288 // Instance Variables
\r
289 /////////////////////
\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
299 private long gregorianCutover = -12219292800000L;
\r
302 * Julian day number of the Gregorian cutover.
\r
304 private transient int cutoverJulianDay = 2299161;
\r
307 * The year of the gregorianCutover, with 0 representing
\r
308 * 1 BC, -1 representing 2 BC, etc.
\r
310 private transient int gregorianCutoverYear = 1582;
\r
313 * Used by handleComputeJulianDay() and handleComputeMonthStart().
\r
316 transient protected boolean isGregorian;
\r
319 * Used by handleComputeJulianDay() and handleComputeMonthStart().
\r
322 transient protected boolean invertGregorian;
\r
329 * Constructs a default GregorianCalendar using the current time
\r
330 * in the default time zone with the default locale.
\r
333 public GregorianCalendar() {
\r
334 this(TimeZone.getDefault(), ULocale.getDefault());
\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
343 public GregorianCalendar(TimeZone zone) {
\r
344 this(zone, ULocale.getDefault());
\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
353 public GregorianCalendar(Locale aLocale) {
\r
354 this(TimeZone.getDefault(), aLocale);
\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
363 public GregorianCalendar(ULocale locale) {
\r
364 this(TimeZone.getDefault(), locale);
\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
374 public GregorianCalendar(TimeZone zone, Locale aLocale) {
\r
375 super(zone, aLocale);
\r
376 setTimeInMillis(System.currentTimeMillis());
\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
386 public GregorianCalendar(TimeZone zone, ULocale locale) {
\r
387 super(zone, locale);
\r
388 setTimeInMillis(System.currentTimeMillis());
\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
400 public GregorianCalendar(int year, int month, int date) {
\r
401 super(TimeZone.getDefault(), ULocale.getDefault());
\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
417 * @param minute the value used to set the MINUTE time field
\r
421 public GregorianCalendar(int year, int month, int date, int hour,
\r
423 super(TimeZone.getDefault(), ULocale.getDefault());
\r
428 set(HOUR_OF_DAY, hour);
\r
429 set(MINUTE, minute);
\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
441 * @param minute the value used to set the MINUTE time field
\r
443 * @param second the value used to set the SECOND time field
\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
454 set(HOUR_OF_DAY, hour);
\r
455 set(MINUTE, minute);
\r
456 set(SECOND, second);
\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
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
472 * @param date the given Gregorian cutover date.
\r
475 public void setGregorianChange(Date date) {
\r
476 gregorianCutover = date.getTime();
\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
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
491 // Convert cutover millis to extended year
\r
492 GregorianCalendar cal = new GregorianCalendar(getTimeZone());
\r
494 gregorianCutoverYear = cal.get(EXTENDED_YEAR);
\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
503 * @return the Gregorian cutover date for this calendar.
\r
506 public final Date getGregorianChange() {
\r
507 return new Date(gregorianCutover);
\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
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
524 * Returns true if the given Calendar object is equivalent to this
\r
525 * one. Calendar override.
\r
527 * @param other the Calendar to be compared with this Calendar
\r
530 public boolean isEquivalentTo(Calendar other) {
\r
531 return super.isEquivalentTo(other) &&
\r
532 gregorianCutover == ((GregorianCalendar)other).gregorianCutover;
\r
536 * Override hashCode.
\r
537 * Generates the hash code for the GregorianCalendar object
\r
540 public int hashCode() {
\r
541 return super.hashCode() ^ (int)gregorianCutover;
\r
545 * Roll a field by a signed amount.
\r
548 public void roll(int field, int amount) {
\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
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
567 isoDoy += handleGetYearLength(isoYear);
\r
571 isoDoy -= handleGetYearLength(isoYear - 1);
\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
591 set(WEEK_OF_YEAR, woy);
\r
592 set(YEAR, isoYear); // Why not YEAR_WOY? - Alan 11/6/00
\r
597 super.roll(field, amount);
\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
607 public int getActualMinimum(int field) {
\r
608 return getMinimum(field);
\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
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
635 * UPDATE: No longer true, since we have pulled in the limit values on
\r
636 * the year. - Liu 11/6/00 */
\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
647 * The actual maxima for YEAR depend on the type of calendar:
\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
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
661 Calendar cal = (Calendar) clone();
\r
662 cal.setLenient(true);
\r
664 int era = cal.get(ERA);
\r
665 Date d = cal.getTime();
\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
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
675 if (cal.get(YEAR) == y && cal.get(ERA) == era) {
\r
679 cal.setTime(d); // Restore original fields
\r
687 return super.getActualMaximum(field);
\r
691 //////////////////////
\r
692 // Proposed public API
\r
693 //////////////////////
\r
696 * Return true if the current time for this Calendar is in Daylignt
\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
706 /////////////////////
\r
707 // Calendar framework
\r
708 /////////////////////
\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
722 return MONTH_COUNT[month][isLeapYear(extendedYear)?1:0];
\r
728 protected int handleGetYearLength(int eyear) {
\r
729 return isLeapYear(eyear) ? 366 : 365;
\r
732 /////////////////////////////
\r
733 // Time => Fields computation
\r
734 /////////////////////////////
\r
737 * Override Calendar to compute several fields specific to the hybrid
\r
738 * Gregorian-Julian calendar system. These are:
\r
745 * <li>EXTENDED_YEAR</ul>
\r
748 protected void handleComputeFields(int julianDay) {
\r
749 int eyear, month, dayOfMonth, dayOfYear;
\r
751 if (julianDay >= cutoverJulianDay) {
\r
752 month = getGregorianMonth();
\r
753 dayOfMonth = getGregorianDayOfMonth();
\r
754 dayOfYear = getGregorianDayOfYear();
\r
755 eyear = getGregorianYear();
\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
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
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
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
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
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
793 internalSet(ERA, era);
\r
794 internalSet(YEAR, eyear);
\r
797 /////////////////////////////
\r
798 // Fields => Time computation
\r
799 /////////////////////////////
\r
804 protected int handleGetExtendedYear() {
\r
806 if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
\r
807 year = internalGet(EXTENDED_YEAR, EPOCH_YEAR);
\r
809 // The year defaults to the epoch start, the era to AD
\r
810 int era = internalGet(ERA, AD);
\r
812 year = 1 - internalGet(YEAR, 1); // Convert to extended year
\r
814 year = internalGet(YEAR, EPOCH_YEAR);
\r
823 protected int handleComputeJulianDay(int bestField) {
\r
825 invertGregorian = false;
\r
827 int jd = super.handleComputeJulianDay(bestField);
\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
840 * Return JD of start of given month/year
\r
843 protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) {
\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
853 boolean isLeap = eyear%4 == 0;
\r
855 int julianDay = 365*y + floorDivide(y, 4) + (JAN_1_1_JULIAN_DAY - 3);
\r
857 isGregorian = (eyear >= gregorianCutoverYear);
\r
858 if (invertGregorian) {
\r
859 isGregorian = !isGregorian;
\r
862 isLeap = isLeap && ((eyear%100 != 0) || (eyear%400 == 0));
\r
863 // Add 2 because Gregorian calendar starts 2 days after
\r
865 julianDay += floorDivide(y, 400) - floorDivide(y, 100) + 2;
\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
873 julianDay += MONTH_COUNT[month][isLeap?3:2];
\r
880 * Return the current Calendar type.
\r
881 * @return type of calendar
\r
884 public String getType() {
\r
885 return "gregorian";
\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
897 public String factoryName() {
\r
898 return "Gregorian";
\r