1 /* java.util.GregorianCalendar
2 Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
45 * This class represents the Gregorian calendar, that is used in most
46 * countries all over the world. It does also handle the Julian calendar
47 * for dates smaller than the date of the change to the Gregorian calendar.
48 * The Gregorian calendar differs from the Julian calendar by a different
49 * leap year rule (no leap year every 100 years, except if year is divisible
53 * This change date is different from country to country, and can be changed with
54 * <code>setGregorianChange</code>. The first countries to adopt the Gregorian
55 * calendar did so on the 15th of October, 1582. This date followed October
56 * the 4th, 1582 in the Julian calendar system. The non-existant days that were
57 * omitted when the change took place are interpreted as Gregorian dates.
60 * Prior to the changeover date, New Year's Day occurred on the 25th of March.
61 * However, this class always takes New Year's Day as being the 1st of January.
62 * Client code should manually adapt the year value, if required, for dates
63 * between January the 1st and March the 24th in years prior to the changeover.
66 * Any date infinitely forwards or backwards in time can be represented by
67 * this class. A <em>proleptic</em> calendar system is used, which allows
68 * future dates to be created via the existing rules. This allows meaningful
69 * and consistent dates to be produced for all years. However, dates are only
70 * historically accurate following March the 1st, 4AD when the Julian calendar
71 * system was adopted. Prior to this, leap year rules were applied erraticly.
74 * There are two eras available for the Gregorian calendar, namely BC and AD.
77 * Weeks are defined as a period of seven days, beginning on the first day
78 * of the week, as returned by <code>getFirstDayOfWeek()</code>, and ending
79 * on the day prior to this.
82 * The weeks of the year are numbered from 1 to a possible 53. The first week
83 * of the year is defined as the first week that contains at least the minimum
84 * number of days of the first week in the new year (retrieved via
85 * <code>getMinimalDaysInFirstWeek()</code>). All weeks after this are numbered
89 * For example, take the year 2004. It began on a Thursday. The first week
90 * of 2004 depends both on where a week begins and how long it must minimally
91 * last. Let's say that the week begins on a Monday and must have a minimum
92 * of 5 days. In this case, the first week begins on Monday, the 5th of January.
93 * The first 4 days (Thursday to Sunday) are not eligible, as they are too few
94 * to make up the minimum number of days of the first week which must be in
95 * the new year. If the minimum was lowered to 4 days, then the first week
96 * would instead begin on Monday, the 29th of December, 2003. This first week
97 * has 4 of its days in the new year, and is now eligible.
100 * The weeks of the month are numbered from 0 to a possible 6. The first week
101 * of the month (numbered 1) is a set of days, prior to the first day of the week,
102 * which number at least the minimum number of days in a week. Unlike the first
103 * week of the year, the first week of the month only uses days from that particular
104 * month. As a consequence, it may have a variable number of days (from the minimum
105 * number required up to a full week of 7) and it need not start on the first day of
106 * the week. It must, however, be following by the first day of the week, as this
107 * marks the beginning of week 2. Any days of the month which occur prior to the
108 * first week (because the first day of the week occurs before the minimum number
109 * of days is met) are seen as week 0.
112 * Again, we will take the example of the year 2004 to demonstrate this. September
113 * 2004 begins on a Wednesday. Taking our first day of the week as Monday, and the
114 * minimum length of the first week as 6, we find that week 1 runs from Monday,
115 * the 6th of September to Sunday the 12th. Prior to the 6th, there are only
116 * 5 days (Wednesday through to Sunday). This is too small a number to meet the
117 * minimum, so these are classed as being days in week 0. Week 2 begins on the
118 * 13th, and so on. This changes if we reduce the minimum to 5. In this case,
119 * week 1 is a truncated week from Wednesday the 1st to Sunday the 5th, and week
120 * 0 doesn't exist. The first seven day week is week 2, starting on the 6th.
123 * On using the <code>clear()</code> method, the Gregorian calendar returns
124 * to its default value of the 1st of January, 1970 AD 00:00:00 (the epoch).
125 * The day of the week is set to the correct day for that particular time.
126 * The day is also the first of the month, and the date is in week 0.
131 * @see Calendar#getFirstDayOfWeek()
132 * @see Calendar#getMinimalDaysInFirstWeek()
134 public class GregorianCalendar
extends Calendar
137 * Constant representing the era BC (Before Christ).
139 public static final int BC
= 0;
142 * Constant representing the era AD (Anno Domini).
144 public static final int AD
= 1;
147 * The point at which the Gregorian calendar rules were used.
148 * This is locale dependent; the default for most catholic
149 * countries is midnight (UTC) on October 5, 1582 (Julian),
150 * or October 15, 1582 (Gregorian).
152 * @serial the changeover point from the Julian calendar
153 * system to the Gregorian.
155 private long gregorianCutover
;
158 * For compatability with Sun's JDK.
160 static final long serialVersionUID
= -8125100834729963327L;
163 * The name of the resource bundle. Used only by getBundle()
165 private static final String bundleName
= "gnu.java.locale.Calendar";
168 * Days in the epoch. Relative Jan 1, year '0' which is not a leap year.
169 * (although there is no year zero, this does not matter.)
170 * This is consistent with the formula:
171 * = (year-1)*365L + ((year-1) >> 2)
173 * Plus the gregorian correction:
174 * Math.floor((year-1) / 400.) - Math.floor((year-1) / 100.);
175 * For a correct julian date, the correction is -2 instead.
177 * The gregorian cutover in 1582 was 10 days, so by calculating the
178 * correction from year zero, we have 15 non-leap days (even centuries)
179 * minus 3 leap days (year 400,800,1200) = 12. Subtracting two corrects
180 * this to the correct number 10.
182 private static final int EPOCH_DAYS
= 719162;
185 * Constructs a new GregorianCalender representing the current
186 * time, using the default time zone and the default locale.
188 public GregorianCalendar()
190 this(TimeZone
.getDefault(), Locale
.getDefault());
194 * Constructs a new GregorianCalender representing the current
195 * time, using the specified time zone and the default locale.
197 * @param zone a time zone.
199 public GregorianCalendar(TimeZone zone
)
201 this(zone
, Locale
.getDefault());
205 * Constructs a new GregorianCalender representing the current
206 * time, using the default time zone and the specified locale.
208 * @param locale a locale.
210 public GregorianCalendar(Locale locale
)
212 this(TimeZone
.getDefault(), locale
);
216 * Constructs a new GregorianCalender representing the current
217 * time with the given time zone and the given locale.
219 * @param zone a time zone.
220 * @param locale a locale.
222 public GregorianCalendar(TimeZone zone
, Locale locale
)
224 this(zone
, locale
, false);
225 setTimeInMillis(System
.currentTimeMillis());
230 * Common constructor that all constructors should call.
231 * @param zone a time zone.
232 * @param locale a locale.
233 * @param unused unused parameter to make the signature differ from
234 * the public constructor (TimeZone, Locale).
236 private GregorianCalendar(TimeZone zone
, Locale locale
, boolean unused
)
239 ResourceBundle rb
= ResourceBundle
.getBundle(bundleName
, locale
,
241 .getSystemClassLoader());
242 gregorianCutover
= ((Date
) rb
.getObject("gregorianCutOver")).getTime();
246 * Constructs a new GregorianCalendar representing midnight on the
247 * given date with the default time zone and locale.
248 * @param year corresponds to the YEAR time field.
249 * @param month corresponds to the MONTH time field.
250 * @param day corresponds to the DAY time field.
252 public GregorianCalendar(int year
, int month
, int day
)
254 this(TimeZone
.getDefault(), Locale
.getDefault(), false);
255 set(year
, month
, day
);
259 * Constructs a new GregorianCalendar representing midnight on the
260 * given date with the default time zone and locale.
262 * @param year corresponds to the YEAR time field.
263 * @param month corresponds to the MONTH time field.
264 * @param day corresponds to the DAY time field.
265 * @param hour corresponds to the HOUR_OF_DAY time field.
266 * @param minute corresponds to the MINUTE time field.
268 public GregorianCalendar(int year
, int month
, int day
, int hour
, int minute
)
270 this(TimeZone
.getDefault(), Locale
.getDefault(), false);
271 set(year
, month
, day
, hour
, minute
);
275 * Constructs a new GregorianCalendar representing midnight on the
276 * given date with the default time zone and locale.
279 * @param year corresponds to the YEAR time field.
280 * @param month corresponds to the MONTH time field.
281 * @param day corresponds to the DAY time field.
282 * @param hour corresponds to the HOUR_OF_DAY time field.
283 * @param minute corresponds to the MINUTE time field.
284 * @param second corresponds to the SECOND time field.
286 public GregorianCalendar(int year
, int month
, int day
, int hour
, int minute
,
289 this(TimeZone
.getDefault(), Locale
.getDefault(), false);
290 set(year
, month
, day
, hour
, minute
, second
);
294 * Sets the date of the switch from Julian dates to Gregorian dates.
295 * You can use <code>new Date(Long.MAX_VALUE)</code> to use a pure
296 * Julian calendar, or <code>Long.MIN_VALUE</code> for a pure Gregorian
299 * @param date the date of the change.
301 public void setGregorianChange(Date date
)
303 gregorianCutover
= date
.getTime();
307 * Gets the date of the switch from Julian dates to Gregorian dates.
309 * @return the date of the change.
311 public final Date
getGregorianChange()
313 return new Date(gregorianCutover
);
318 * Determines if the given year is a leap year. The result is
319 * undefined if the Gregorian change took place in 1800, so that
320 * the end of February is skipped, and that year is specified.
324 * To specify a year in the BC era, use a negative value calculated
325 * as 1 - y, where y is the required year in BC. So, 1 BC is 0,
326 * 2 BC is -1, 3 BC is -2, etc.
329 * @param year a year (use a negative value for BC).
330 * @return true, if the given year is a leap year, false otherwise.
332 public boolean isLeapYear(int year
)
334 // Only years divisible by 4 can be leap years
338 // Is the leap-day a Julian date? Then it's a leap year
339 if (! isGregorian(year
, 31 + 29 - 1))
342 // Apply gregorian rules otherwise
343 return ((year
% 100) != 0 || (year
% 400) == 0);
347 * Retrieves the day of the week corresponding to the specified
348 * day of the specified year.
350 * @param year the year in which the dayOfYear occurs.
351 * @param dayOfYear the day of the year (an integer between 0 and
354 private int getWeekDay(int year
, int dayOfYear
)
356 boolean greg
= isGregorian(year
, dayOfYear
);
357 int day
= (int) getLinearDay(year
, dayOfYear
, greg
);
359 // The epoch was a thursday.
360 int weekday
= (day
+ THURSDAY
) % 7;
367 * Returns the day of the week for the first day of a given month (0..11)
369 private int getFirstDayOfMonth(int year
, int month
)
371 int[] dayCount
= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
375 year
+= (month
/ 12);
381 year
+= (int) month
/ 12;
390 int dayOfYear
= dayCount
[month
] + 1;
392 if (isLeapYear(year
))
395 boolean greg
= isGregorian(year
, dayOfYear
);
396 int day
= (int) getLinearDay(year
, dayOfYear
, greg
);
398 // The epoch was a thursday.
399 int weekday
= (day
+ THURSDAY
) % 7;
406 * Takes a year, and a (zero based) day of year and determines
407 * if it is gregorian or not.
409 private boolean isGregorian(int year
, int dayOfYear
)
411 int relativeDay
= (year
- 1) * 365 + ((year
- 1) >> 2) + dayOfYear
412 - EPOCH_DAYS
; // gregorian days from 1 to epoch.
413 int gregFactor
= (int) Math
.floor((double) (year
- 1) / 400.)
414 - (int) Math
.floor((double) (year
- 1) / 100.);
416 return ((relativeDay
+ gregFactor
) * 60L * 60L * 24L * 1000L >= gregorianCutover
);
420 * Check set fields for validity, without leniency.
422 * @throws IllegalArgumentException if a field is invalid
424 private void nonLeniencyCheck() throws IllegalArgumentException
426 int[] month_days
= { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
427 int year
= fields
[YEAR
];
428 int month
= fields
[MONTH
];
429 int leap
= isLeapYear(year
) ?
1 : 0;
431 if (isSet
[ERA
] && fields
[ERA
] != AD
&& fields
[ERA
] != BC
)
432 throw new IllegalArgumentException("Illegal ERA.");
433 if (isSet
[YEAR
] && fields
[YEAR
] < 1)
434 throw new IllegalArgumentException("Illegal YEAR.");
435 if (isSet
[MONTH
] && (month
< 0 || month
> 11))
436 throw new IllegalArgumentException("Illegal MONTH.");
437 if (isSet
[WEEK_OF_YEAR
])
439 int daysInYear
= 365 + leap
;
440 daysInYear
+= (getFirstDayOfMonth(year
, 0) - 1); // pad first week
441 int last
= getFirstDayOfMonth(year
, 11) + 4;
444 daysInYear
+= 7 - last
;
445 int weeks
= daysInYear
/ 7;
446 if (fields
[WEEK_OF_YEAR
] < 1 || fields
[WEEK_OF_YEAR
] > weeks
)
447 throw new IllegalArgumentException("Illegal WEEK_OF_YEAR.");
450 if (isSet
[WEEK_OF_MONTH
])
452 int weeks
= (month
== 1 && leap
== 0) ?
4 : 5;
453 if (fields
[WEEK_OF_MONTH
] < 1 || fields
[WEEK_OF_MONTH
] > weeks
)
454 throw new IllegalArgumentException("Illegal WEEK_OF_MONTH.");
457 if (isSet
[DAY_OF_MONTH
])
458 if (fields
[DAY_OF_MONTH
] < 1
459 || fields
[DAY_OF_MONTH
] > month_days
[month
]
460 + ((month
== 1) ? leap
: 0))
461 throw new IllegalArgumentException("Illegal DAY_OF_MONTH.");
463 if (isSet
[DAY_OF_YEAR
]
464 && (fields
[DAY_OF_YEAR
] < 1 || fields
[DAY_OF_YEAR
] > 365 + leap
))
465 throw new IllegalArgumentException("Illegal DAY_OF_YEAR.");
467 if (isSet
[DAY_OF_WEEK
]
468 && (fields
[DAY_OF_WEEK
] < 1 || fields
[DAY_OF_WEEK
] > 7))
469 throw new IllegalArgumentException("Illegal DAY_OF_WEEK.");
471 if (isSet
[DAY_OF_WEEK_IN_MONTH
])
473 int weeks
= (month
== 1 && leap
== 0) ?
4 : 5;
474 if (fields
[DAY_OF_WEEK_IN_MONTH
] < -weeks
475 || fields
[DAY_OF_WEEK_IN_MONTH
] > weeks
)
476 throw new IllegalArgumentException("Illegal DAY_OF_WEEK_IN_MONTH.");
479 if (isSet
[AM_PM
] && fields
[AM_PM
] != AM
&& fields
[AM_PM
] != PM
)
480 throw new IllegalArgumentException("Illegal AM_PM.");
481 if (isSet
[HOUR
] && (fields
[HOUR
] < 0 || fields
[HOUR
] > 12))
482 throw new IllegalArgumentException("Illegal HOUR.");
483 if (isSet
[HOUR_OF_DAY
]
484 && (fields
[HOUR_OF_DAY
] < 0 || fields
[HOUR_OF_DAY
] > 23))
485 throw new IllegalArgumentException("Illegal HOUR_OF_DAY.");
486 if (isSet
[MINUTE
] && (fields
[MINUTE
] < 0 || fields
[MINUTE
] > 59))
487 throw new IllegalArgumentException("Illegal MINUTE.");
488 if (isSet
[SECOND
] && (fields
[SECOND
] < 0 || fields
[SECOND
] > 59))
489 throw new IllegalArgumentException("Illegal SECOND.");
490 if (isSet
[MILLISECOND
]
491 && (fields
[MILLISECOND
] < 0 || fields
[MILLISECOND
] > 999))
492 throw new IllegalArgumentException("Illegal MILLISECOND.");
493 if (isSet
[ZONE_OFFSET
]
494 && (fields
[ZONE_OFFSET
] < -12 * 60 * 60 * 1000L
495 || fields
[ZONE_OFFSET
] > 12 * 60 * 60 * 1000L))
496 throw new IllegalArgumentException("Illegal ZONE_OFFSET.");
497 if (isSet
[DST_OFFSET
]
498 && (fields
[DST_OFFSET
] < -12 * 60 * 60 * 1000L
499 || fields
[DST_OFFSET
] > 12 * 60 * 60 * 1000L))
500 throw new IllegalArgumentException("Illegal DST_OFFSET.");
504 * Converts the time field values (<code>fields</code>) to
505 * milliseconds since the epoch UTC (<code>time</code>).
507 * @throws IllegalArgumentException if any calendar fields
510 protected synchronized void computeTime()
513 int era
= fields
[ERA
];
514 int year
= fields
[YEAR
];
515 int month
= fields
[MONTH
];
516 int day
= fields
[DAY_OF_MONTH
];
518 int minute
= fields
[MINUTE
];
519 int second
= fields
[SECOND
];
520 int millis
= fields
[MILLISECOND
];
521 int[] month_days
= { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
522 int[] dayCount
= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
528 if (! isSet
[MONTH
] && (! isSet
[DAY_OF_WEEK
] || isSet
[WEEK_OF_YEAR
]))
530 // 5: YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
531 if (isSet
[WEEK_OF_YEAR
])
533 int first
= getFirstDayOfMonth(year
, 0);
535 int daysInFirstWeek
= getFirstDayOfWeek() - first
;
536 if (daysInFirstWeek
<= 0)
537 daysInFirstWeek
+= 7;
539 if (daysInFirstWeek
< getMinimalDaysInFirstWeek())
540 offs
+= daysInFirstWeek
;
542 offs
-= 7 - daysInFirstWeek
;
544 day
= offs
+ 7 * (fields
[WEEK_OF_YEAR
] - 1);
545 offs
= fields
[DAY_OF_WEEK
] - getFirstDayOfWeek();
553 // 4: YEAR + DAY_OF_YEAR
555 day
= fields
[DAY_OF_YEAR
];
560 if (isSet
[DAY_OF_WEEK
])
562 int first
= getFirstDayOfMonth(year
, month
);
564 // 3: YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
565 if (isSet
[DAY_OF_WEEK_IN_MONTH
])
567 int offs
= fields
[DAY_OF_WEEK
] - first
;
570 day
= 1 + 7 * (fields
[DAY_OF_WEEK_IN_MONTH
] - 1);
574 { // 2: YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
576 int daysInFirstWeek
= getFirstDayOfWeek() - first
;
577 if (daysInFirstWeek
<= 0)
578 daysInFirstWeek
+= 7;
580 if (daysInFirstWeek
< getMinimalDaysInFirstWeek())
581 offs
+= daysInFirstWeek
;
583 offs
-= 7 - daysInFirstWeek
;
585 day
= offs
+ 7 * (fields
[WEEK_OF_MONTH
] - 1);
586 offs
= fields
[DAY_OF_WEEK
] - getFirstDayOfWeek();
593 // 1: YEAR + MONTH + DAY_OF_MONTH
595 if (era
== BC
&& year
> 0)
598 // rest of code assumes day/month/year set
599 // should negative BC years be AD?
600 // get the hour (but no check for validity)
604 if (fields
[AM_PM
] == PM
)
605 if (hour
!= 12) /* not Noon */
607 /* Fix the problem of the status of 12:00 AM (midnight). */
608 if (fields
[AM_PM
] == AM
&& hour
== 12)
612 hour
= fields
[HOUR_OF_DAY
];
614 // Read the era,year,month,day fields and convert as appropriate.
615 // Calculate number of milliseconds into the day
616 // This takes care of both h, m, s, ms over/underflows.
617 long allMillis
= (((hour
* 60L) + minute
) * 60L + second
) * 1000L + millis
;
618 day
+= allMillis
/ (24 * 60 * 60 * 1000L);
619 millisInDay
= (int) (allMillis
% (24 * 60 * 60 * 1000L));
623 year
+= (int) month
/ 12;
633 year
+= (month
/ 12);
637 month_days
[1] = isLeapYear(year
) ?
29 : 28;
644 month_days
[1] = isLeapYear(year
) ?
29 : 28;
646 month
= (month
+ 11) % 12;
647 day
+= month_days
[month
];
649 while (day
> month_days
[month
])
651 day
-= (month_days
[month
]);
652 month
= (month
+ 1) % 12;
656 month_days
[1] = isLeapYear(year
) ?
29 : 28;
660 // ok, by here we have valid day,month,year,era and millisinday
661 int dayOfYear
= dayCount
[month
] + day
- 1; // (day starts on 1)
662 if (isLeapYear(year
) && month
> 1)
665 int relativeDay
= (year
- 1) * 365 + ((year
- 1) >> 2) + dayOfYear
666 - EPOCH_DAYS
; // gregorian days from 1 to epoch.
667 int gregFactor
= (int) Math
.floor((double) (year
- 1) / 400.)
668 - (int) Math
.floor((double) (year
- 1) / 100.);
670 if ((relativeDay
+ gregFactor
) * 60L * 60L * 24L * 1000L >= gregorianCutover
)
671 relativeDay
+= gregFactor
;
675 time
= relativeDay
* (24 * 60 * 60 * 1000L) + millisInDay
;
677 // the epoch was a Thursday.
678 int weekday
= (int) (relativeDay
+ THURSDAY
) % 7;
681 fields
[DAY_OF_WEEK
] = weekday
;
683 // Time zone corrections.
684 TimeZone zone
= getTimeZone();
685 int rawOffset
= isSet
[ZONE_OFFSET
] ? fields
[ZONE_OFFSET
]
686 : zone
.getRawOffset();
688 int dstOffset
= isSet
[DST_OFFSET
] ? fields
[DST_OFFSET
]
689 : (zone
.getOffset((year
< 0) ? BC
: AD
,
690 (year
< 0) ?
1 - year
694 - zone
.getRawOffset());
696 time
-= rawOffset
+ dstOffset
;
702 * Get the linear day in days since the epoch, using the
703 * Julian or Gregorian calendar as specified. If you specify a
704 * nonpositive year it is interpreted as BC as following: 0 is 1
705 * BC, -1 is 2 BC and so on.
707 * @param year the year of the date.
708 * @param dayOfYear the day of year of the date; 1 based.
709 * @param gregorian <code>true</code>, if we should use the Gregorian rules.
710 * @return the days since the epoch, may be negative.
712 public long getLinearDay(int year
, int dayOfYear
, boolean gregorian
)
714 // The 13 is the number of days, that were omitted in the Gregorian
715 // Calender until the epoch.
716 // We shift right by 2 instead of dividing by 4, to get correct
717 // results for negative years (and this is even more efficient).
718 long julianDay
= (year
- 1) * 365L + ((year
- 1) >> 2) + (dayOfYear
- 1)
719 - EPOCH_DAYS
; // gregorian days from 1 to epoch.
723 // subtract the days that are missing in gregorian calendar
724 // with respect to julian calendar.
726 // Okay, here we rely on the fact that the gregorian
727 // calendar was introduced in the AD era. This doesn't work
728 // with negative years.
730 // The additional leap year factor accounts for the fact that
731 // a leap day is not seen on Jan 1 of the leap year.
732 int gregOffset
= (int) Math
.floor((double) (year
- 1) / 400.)
733 - (int) Math
.floor((double) (year
- 1) / 100.);
735 return julianDay
+ gregOffset
;
743 * Converts the given linear day into era, year, month,
744 * day_of_year, day_of_month, day_of_week, and writes the result
745 * into the fields array.
747 * @param day the linear day.
748 * @param gregorian true, if we should use Gregorian rules.
750 private void calculateDay(int[] fields
, long day
, boolean gregorian
)
752 // the epoch was a Thursday.
753 int weekday
= (int) (day
+ THURSDAY
) % 7;
756 fields
[DAY_OF_WEEK
] = weekday
;
758 // get a first approximation of the year. This may be one
762 ?
((day
- 100L) * 400L) / (365L * 400L + 100L - 4L
763 + 1L) : ((day
- 100L) * 4L) / (365L * 4L + 1L));
767 long firstDayOfYear
= getLinearDay(year
, 1, gregorian
);
769 // Now look in which year day really lies.
770 if (day
< firstDayOfYear
)
773 firstDayOfYear
= getLinearDay(year
, 1, gregorian
);
776 day
-= firstDayOfYear
- 1; // day of year, one based.
778 fields
[DAY_OF_YEAR
] = (int) day
;
782 fields
[YEAR
] = 1 - year
;
790 int leapday
= isLeapYear(year
) ?
1 : 0;
791 if (day
<= 31 + 28 + leapday
)
793 fields
[MONTH
] = (int) day
/ 32; // 31->JANUARY, 32->FEBRUARY
794 fields
[DAY_OF_MONTH
] = (int) day
- 31 * fields
[MONTH
];
798 // A few more magic formulas
799 int scaledDay
= ((int) day
- leapday
) * 5 + 8;
800 fields
[MONTH
] = scaledDay
/ (31 + 30 + 31 + 30 + 31);
801 fields
[DAY_OF_MONTH
] = (scaledDay
% (31 + 30 + 31 + 30 + 31)) / 5 + 1;
806 * Converts the milliseconds since the epoch UTC
807 * (<code>time</code>) to time fields
808 * (<code>fields</code>).
810 protected synchronized void computeFields()
812 boolean gregorian
= (time
>= gregorianCutover
);
814 TimeZone zone
= getTimeZone();
815 fields
[ZONE_OFFSET
] = zone
.getRawOffset();
816 long localTime
= time
+ fields
[ZONE_OFFSET
];
818 long day
= localTime
/ (24 * 60 * 60 * 1000L);
819 int millisInDay
= (int) (localTime
% (24 * 60 * 60 * 1000L));
823 millisInDay
+= (24 * 60 * 60 * 1000);
827 calculateDay(fields
, day
, gregorian
);
828 fields
[DST_OFFSET
] = zone
.getOffset(fields
[ERA
], fields
[YEAR
],
829 fields
[MONTH
], fields
[DAY_OF_MONTH
],
830 fields
[DAY_OF_WEEK
], millisInDay
)
831 - fields
[ZONE_OFFSET
];
833 millisInDay
+= fields
[DST_OFFSET
];
834 if (millisInDay
>= 24 * 60 * 60 * 1000)
836 millisInDay
-= 24 * 60 * 60 * 1000;
837 calculateDay(fields
, ++day
, gregorian
);
840 fields
[DAY_OF_WEEK_IN_MONTH
] = (fields
[DAY_OF_MONTH
] + 6) / 7;
842 // which day of the week are we (0..6), relative to getFirstDayOfWeek
843 int relativeWeekday
= (7 + fields
[DAY_OF_WEEK
] - getFirstDayOfWeek()) % 7;
845 fields
[WEEK_OF_MONTH
] = (fields
[DAY_OF_MONTH
] - relativeWeekday
+ 12) / 7;
847 int weekOfYear
= (fields
[DAY_OF_YEAR
] - relativeWeekday
+ 6) / 7;
849 // Do the Correction: getMinimalDaysInFirstWeek() is always in the
851 int minDays
= getMinimalDaysInFirstWeek();
852 int firstWeekday
= (7 + getWeekDay(fields
[YEAR
], minDays
)
853 - getFirstDayOfWeek()) % 7;
854 if (minDays
- firstWeekday
< 1)
856 fields
[WEEK_OF_YEAR
] = weekOfYear
;
858 int hourOfDay
= millisInDay
/ (60 * 60 * 1000);
859 fields
[AM_PM
] = (hourOfDay
< 12) ? AM
: PM
;
860 int hour
= hourOfDay
% 12;
861 fields
[HOUR
] = (hour
== 0) ?
12 : hour
;
862 fields
[HOUR_OF_DAY
] = hourOfDay
;
863 millisInDay
%= (60 * 60 * 1000);
864 fields
[MINUTE
] = millisInDay
/ (60 * 1000);
865 millisInDay
%= (60 * 1000);
866 fields
[SECOND
] = millisInDay
/ (1000);
867 fields
[MILLISECOND
] = millisInDay
% 1000;
869 areFieldsSet
= isSet
[ERA
] = isSet
[YEAR
] = isSet
[MONTH
] = isSet
[WEEK_OF_YEAR
] = isSet
[WEEK_OF_MONTH
] = isSet
[DAY_OF_MONTH
] = isSet
[DAY_OF_YEAR
] = isSet
[DAY_OF_WEEK
] = isSet
[DAY_OF_WEEK_IN_MONTH
] = isSet
[AM_PM
] = isSet
[HOUR
] = isSet
[HOUR_OF_DAY
] = isSet
[MINUTE
] = isSet
[SECOND
] = isSet
[MILLISECOND
] = isSet
[ZONE_OFFSET
] = isSet
[DST_OFFSET
] = true;
873 * Compares the given calendar with this. An object, o, is
874 * equivalent to this if it is also a <code>GregorianCalendar</code>
875 * with the same time since the epoch under the same conditions
876 * (same change date and same time zone).
878 * @param o the object to that we should compare.
879 * @return true, if the given object is a calendar, that represents
880 * the same time (but doesn't necessarily have the same fields).
881 * @throws IllegalArgumentException if one of the fields
882 * <code>ZONE_OFFSET</code> or <code>DST_OFFSET</code> is
883 * specified, if an unknown field is specified or if one
884 * of the calendar fields receives an illegal value when
885 * leniancy is not enabled.
887 public boolean equals(Object o
)
889 if (! (o
instanceof GregorianCalendar
))
892 GregorianCalendar cal
= (GregorianCalendar
) o
;
893 return (cal
.getTimeInMillis() == getTimeInMillis());
897 * Adds the specified amount of time to the given time field. The
898 * amount may be negative to subtract the time. If the field overflows
899 * it does what you expect: Jan, 25 + 10 Days is Feb, 4.
900 * @param field one of the time field constants.
901 * @param amount the amount of time to add.
902 * @exception IllegalArgumentException if <code>field</code> is
903 * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or invalid; or
904 * if <code>amount</code> contains an out-of-range value and the calendar
905 * is not in lenient mode.
907 public void add(int field
, int amount
)
913 fields
[YEAR
] += amount
;
918 int months
= fields
[MONTH
] + amount
;
919 fields
[YEAR
] += months
/ 12;
920 fields
[MONTH
] = months
% 12;
921 if (fields
[MONTH
] < 0)
926 int maxDay
= getActualMaximum(DAY_OF_MONTH
);
927 if (fields
[DAY_OF_MONTH
] > maxDay
)
929 fields
[DAY_OF_MONTH
] = maxDay
;
931 set(YEAR
, fields
[YEAR
]);
932 set(MONTH
, fields
[MONTH
]);
939 time
+= amount
* (24 * 60 * 60 * 1000L);
940 areFieldsSet
= false;
944 case DAY_OF_WEEK_IN_MONTH
:
947 time
+= amount
* (7 * 24 * 60 * 60 * 1000L);
948 areFieldsSet
= false;
953 time
+= amount
* (12 * 60 * 60 * 1000L);
954 areFieldsSet
= false;
960 time
+= amount
* (60 * 60 * 1000L);
961 areFieldsSet
= false;
966 time
+= amount
* (60 * 1000L);
967 areFieldsSet
= false;
972 time
+= amount
* (1000L);
973 areFieldsSet
= false;
979 areFieldsSet
= false;
982 case DST_OFFSET
:default:
983 throw new IllegalArgumentException("Invalid or unknown field");
988 * Rolls the specified time field up or down. This means add one
989 * to the specified field, but don't change the other fields. If
990 * the maximum for this field is reached, start over with the
993 * <strong>Note:</strong> There may be situation, where the other
994 * fields must be changed, e.g rolling the month on May, 31.
995 * The date June, 31 is automatically converted to July, 1.
996 * This requires lenient settings.
998 * @param field the time field. One of the time field constants.
999 * @param up the direction, true for up, false for down.
1000 * @throws IllegalArgumentException if one of the fields
1001 * <code>ZONE_OFFSET</code> or <code>DST_OFFSET</code> is
1002 * specified, if an unknown field is specified or if one
1003 * of the calendar fields receives an illegal value when
1004 * leniancy is not enabled.
1006 public void roll(int field
, boolean up
)
1008 roll(field
, up ?
1 : -1);
1012 * Checks that the fields are still within their legal bounds,
1013 * following use of the <code>roll()</code> method.
1015 * @param field the field to check.
1016 * @param delta multipler for alterations to the <code>time</code>.
1017 * @see #roll(int, boolean)
1018 * @see #roll(int, int)
1020 private void cleanUpAfterRoll(int field
, int delta
)
1027 // check that day of month is still in correct range
1028 if (fields
[DAY_OF_MONTH
] > getActualMaximum(DAY_OF_MONTH
))
1029 fields
[DAY_OF_MONTH
] = getActualMaximum(DAY_OF_MONTH
);
1031 isSet
[WEEK_OF_MONTH
] = false;
1032 isSet
[DAY_OF_WEEK
] = false;
1033 isSet
[DAY_OF_WEEK_IN_MONTH
] = false;
1034 isSet
[DAY_OF_YEAR
] = false;
1035 isSet
[WEEK_OF_YEAR
] = false;
1038 isSet
[WEEK_OF_MONTH
] = false;
1039 isSet
[DAY_OF_WEEK
] = false;
1040 isSet
[DAY_OF_WEEK_IN_MONTH
] = false;
1041 isSet
[DAY_OF_YEAR
] = false;
1042 isSet
[WEEK_OF_YEAR
] = false;
1043 time
+= delta
* (24 * 60 * 60 * 1000L);
1046 isSet
[DAY_OF_MONTH
] = false;
1047 isSet
[DAY_OF_WEEK_IN_MONTH
] = false;
1048 isSet
[DAY_OF_YEAR
] = false;
1049 isSet
[WEEK_OF_YEAR
] = false;
1050 time
+= delta
* (7 * 24 * 60 * 60 * 1000L);
1052 case DAY_OF_WEEK_IN_MONTH
:
1053 isSet
[DAY_OF_MONTH
] = false;
1054 isSet
[WEEK_OF_MONTH
] = false;
1055 isSet
[DAY_OF_YEAR
] = false;
1056 isSet
[WEEK_OF_YEAR
] = false;
1057 time
+= delta
* (7 * 24 * 60 * 60 * 1000L);
1060 isSet
[MONTH
] = false;
1061 isSet
[DAY_OF_MONTH
] = false;
1062 isSet
[WEEK_OF_MONTH
] = false;
1063 isSet
[DAY_OF_WEEK_IN_MONTH
] = false;
1064 isSet
[DAY_OF_WEEK
] = false;
1065 isSet
[WEEK_OF_YEAR
] = false;
1066 time
+= delta
* (24 * 60 * 60 * 1000L);
1069 isSet
[MONTH
] = false;
1070 isSet
[DAY_OF_MONTH
] = false;
1071 isSet
[WEEK_OF_MONTH
] = false;
1072 isSet
[DAY_OF_WEEK_IN_MONTH
] = false;
1073 isSet
[DAY_OF_YEAR
] = false;
1074 time
+= delta
* (7 * 24 * 60 * 60 * 1000L);
1077 isSet
[HOUR_OF_DAY
] = false;
1078 time
+= delta
* (12 * 60 * 60 * 1000L);
1081 isSet
[HOUR_OF_DAY
] = false;
1082 time
+= delta
* (60 * 60 * 1000L);
1085 isSet
[HOUR
] = false;
1086 isSet
[AM_PM
] = false;
1087 time
+= delta
* (60 * 60 * 1000L);
1090 time
+= delta
* (60 * 1000L);
1093 time
+= delta
* (1000L);
1102 * Rolls the specified time field by the given amount. This means
1103 * add amount to the specified field, but don't change the other
1104 * fields. If the maximum for this field is reached, start over
1105 * with the minimum value and vice versa for negative amounts.
1107 * <strong>Note:</strong> There may be situation, where the other
1108 * fields must be changed, e.g rolling the month on May, 31.
1109 * The date June, 31 is automatically corrected to June, 30.
1111 * @param field the time field. One of the time field constants.
1112 * @param amount the amount by which we should roll.
1113 * @throws IllegalArgumentException if one of the fields
1114 * <code>ZONE_OFFSET</code> or <code>DST_OFFSET</code> is
1115 * specified, if an unknown field is specified or if one
1116 * of the calendar fields receives an illegal value when
1117 * leniancy is not enabled.
1119 public void roll(int field
, int amount
)
1124 // day of week is special: it rolls automatically
1129 throw new IllegalArgumentException("Can't roll time zone");
1132 int min
= getActualMinimum(field
);
1133 int range
= getActualMaximum(field
) - min
+ 1;
1134 int oldval
= fields
[field
];
1135 int newval
= (oldval
- min
+ range
+ amount
) % range
+ min
;
1138 fields
[field
] = newval
;
1139 cleanUpAfterRoll(field
, newval
- oldval
);
1143 * The minimum values for the calendar fields.
1145 private static final int[] minimums
=
1147 BC
, 1, 0, 0, 1, 1, 1, SUNDAY
, 1, AM
,
1148 1, 0, 0, 0, 0, -(12 * 60 * 60 * 1000),
1153 * The maximum values for the calendar fields.
1155 private static final int[] maximums
=
1157 AD
, 5000000, 11, 53, 5, 31, 366,
1158 SATURDAY
, 5, PM
, 12, 23, 59, 59, 999,
1159 +(12 * 60 * 60 * 1000),
1160 (12 * 60 * 60 * 1000)
1164 * Gets the smallest value that is allowed for the specified field.
1166 * @param field one of the time field constants.
1167 * @return the smallest value for the specified field.
1169 public int getMinimum(int field
)
1171 return minimums
[field
];
1175 * Gets the biggest value that is allowed for the specified field.
1177 * @param field one of the time field constants.
1178 * @return the biggest value.
1180 public int getMaximum(int field
)
1182 return maximums
[field
];
1186 * Gets the greatest minimum value that is allowed for the specified field.
1187 * This is the largest value returned by the <code>getActualMinimum(int)</code>
1190 * @param field the time field. One of the time field constants.
1191 * @return the greatest minimum value.
1192 * @see #getActualMinimum(int)
1194 public int getGreatestMinimum(int field
)
1196 if (field
== WEEK_OF_YEAR
)
1198 return minimums
[field
];
1202 * Gets the smallest maximum value that is allowed for the
1203 * specified field. This is the smallest value returned
1204 * by the <code>getActualMaximum(int)</code>. For example,
1205 * this is 28 for DAY_OF_MONTH (as all months have at least
1208 * @param field the time field. One of the time field constants.
1209 * @return the least maximum value.
1210 * @see #getActualMaximum(int)
1213 public int getLeastMaximum(int field
)
1223 case DAY_OF_WEEK_IN_MONTH
:
1227 return maximums
[field
];
1232 * Gets the actual minimum value that is allowed for the specified field.
1233 * This value is dependent on the values of the other fields. Note that
1234 * this calls <code>complete()</code> if not enough fields are set. This
1235 * can have ugly side effects. The value given depends on the current
1236 * time used by this instance.
1238 * @param field the time field. One of the time field constants.
1239 * @return the actual minimum value.
1242 public int getActualMinimum(int field
)
1244 if (field
== WEEK_OF_YEAR
)
1246 int min
= getMinimalDaysInFirstWeek();
1249 if (! areFieldsSet
|| ! isSet
[ERA
] || ! isSet
[YEAR
])
1252 int year
= fields
[ERA
] == AD ? fields
[YEAR
] : 1 - fields
[YEAR
];
1253 int weekday
= getWeekDay(year
, min
);
1254 if ((7 + weekday
- getFirstDayOfWeek()) % 7 >= min
- 1)
1258 return minimums
[field
];
1262 * Gets the actual maximum value that is allowed for the specified field.
1263 * This value is dependent on the values of the other fields. Note that
1264 * this calls <code>complete()</code> if not enough fields are set. This
1265 * can have ugly side effects. The value given depends on the current time
1266 * used by this instance; thus, leap years have a maximum day of month value of
1267 * 29, rather than 28.
1269 * @param field the time field. One of the time field constants.
1270 * @return the actual maximum value.
1272 public int getActualMaximum(int field
)
1278 if (! areFieldsSet
|| ! isSet
[ERA
] || ! isSet
[YEAR
])
1281 // This is wrong for the year that contains the gregorian change.
1282 // I.e it gives the weeks in the julian year or in the gregorian
1283 // year in that case.
1284 int year
= fields
[ERA
] == AD ? fields
[YEAR
] : 1 - fields
[YEAR
];
1285 int lastDay
= isLeapYear(year
) ?
366 : 365;
1286 int weekday
= getWeekDay(year
, lastDay
);
1287 int week
= (lastDay
+ 6 - (7 + weekday
- getFirstDayOfWeek()) % 7) / 7;
1289 int minimalDays
= getMinimalDaysInFirstWeek();
1290 int firstWeekday
= getWeekDay(year
, minimalDays
);
1292 * Is there a set of days at the beginning of the year, before the
1293 * first day of the week, equal to or greater than the minimum number
1294 * of days required in the first week?
1296 if (minimalDays
- (7 + firstWeekday
- getFirstDayOfWeek()) % 7 < 1)
1297 return week
+ 1; /* Add week 1: firstWeekday through to firstDayOfWeek */
1301 if (! areFieldsSet
|| ! isSet
[MONTH
])
1303 int month
= fields
[MONTH
];
1305 // If you change this, you should also change
1306 // SimpleTimeZone.getDaysInMonth();
1307 if (month
== FEBRUARY
)
1309 if (! isSet
[YEAR
] || ! isSet
[ERA
])
1311 int year
= fields
[ERA
] == AD ? fields
[YEAR
] : 1 - fields
[YEAR
];
1312 return isLeapYear(year
) ?
29 : 28;
1314 else if (month
< AUGUST
)
1315 return 31 - (month
& 1);
1317 return 30 + (month
& 1);
1321 if (! areFieldsSet
|| ! isSet
[ERA
] || ! isSet
[YEAR
])
1323 int year
= fields
[ERA
] == AD ? fields
[YEAR
] : 1 - fields
[YEAR
];
1324 return isLeapYear(year
) ?
366 : 365;
1326 case DAY_OF_WEEK_IN_MONTH
:
1328 // This is wrong for the month that contains the gregorian change.
1329 int daysInMonth
= getActualMaximum(DAY_OF_MONTH
);
1331 // That's black magic, I know
1332 return (daysInMonth
- (fields
[DAY_OF_MONTH
] - 1) % 7 + 6) / 7;
1336 int daysInMonth
= getActualMaximum(DAY_OF_MONTH
);
1337 int weekday
= (daysInMonth
- fields
[DAY_OF_MONTH
]
1338 + fields
[DAY_OF_WEEK
] - SUNDAY
) % 7 + SUNDAY
;
1339 return (daysInMonth
+ 6 - (7 + weekday
- getFirstDayOfWeek()) % 7) / 7;
1342 return maximums
[field
];