Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/util/GregorianCalendar.java
38829 views
1
/*
2
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/*
27
* (C) Copyright Taligent, Inc. 1996-1998 - All Rights Reserved
28
* (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
29
*
30
* The original version of this source code and documentation is copyrighted
31
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
32
* materials are provided under terms of a License Agreement between Taligent
33
* and Sun. This technology is protected by multiple US and International
34
* patents. This notice and attribution to Taligent may not be removed.
35
* Taligent is a registered trademark of Taligent, Inc.
36
*
37
*/
38
39
package java.util;
40
41
import java.io.IOException;
42
import java.io.ObjectInputStream;
43
import java.time.Instant;
44
import java.time.ZonedDateTime;
45
import java.time.temporal.ChronoField;
46
import sun.util.calendar.BaseCalendar;
47
import sun.util.calendar.CalendarDate;
48
import sun.util.calendar.CalendarSystem;
49
import sun.util.calendar.CalendarUtils;
50
import sun.util.calendar.Era;
51
import sun.util.calendar.Gregorian;
52
import sun.util.calendar.JulianCalendar;
53
import sun.util.calendar.ZoneInfo;
54
55
/**
56
* <code>GregorianCalendar</code> is a concrete subclass of
57
* <code>Calendar</code> and provides the standard calendar system
58
* used by most of the world.
59
*
60
* <p> <code>GregorianCalendar</code> is a hybrid calendar that
61
* supports both the Julian and Gregorian calendar systems with the
62
* support of a single discontinuity, which corresponds by default to
63
* the Gregorian date when the Gregorian calendar was instituted
64
* (October 15, 1582 in some countries, later in others). The cutover
65
* date may be changed by the caller by calling {@link
66
* #setGregorianChange(Date) setGregorianChange()}.
67
*
68
* <p>
69
* Historically, in those countries which adopted the Gregorian calendar first,
70
* October 4, 1582 (Julian) was thus followed by October 15, 1582 (Gregorian). This calendar models
71
* this correctly. Before the Gregorian cutover, <code>GregorianCalendar</code>
72
* implements the Julian calendar. The only difference between the Gregorian
73
* and the Julian calendar is the leap year rule. The Julian calendar specifies
74
* leap years every four years, whereas the Gregorian calendar omits century
75
* years which are not divisible by 400.
76
*
77
* <p>
78
* <code>GregorianCalendar</code> implements <em>proleptic</em> Gregorian and
79
* Julian calendars. That is, dates are computed by extrapolating the current
80
* rules indefinitely far backward and forward in time. As a result,
81
* <code>GregorianCalendar</code> may be used for all years to generate
82
* meaningful and consistent results. However, dates obtained using
83
* <code>GregorianCalendar</code> are historically accurate only from March 1, 4
84
* AD onward, when modern Julian calendar rules were adopted. Before this date,
85
* leap year rules were applied irregularly, and before 45 BC the Julian
86
* calendar did not even exist.
87
*
88
* <p>
89
* Prior to the institution of the Gregorian calendar, New Year's Day was
90
* March 25. To avoid confusion, this calendar always uses January 1. A manual
91
* adjustment may be made if desired for dates that are prior to the Gregorian
92
* changeover and which fall between January 1 and March 24.
93
*
94
* <h3><a name="week_and_year">Week Of Year and Week Year</a></h3>
95
*
96
* <p>Values calculated for the {@link Calendar#WEEK_OF_YEAR
97
* WEEK_OF_YEAR} field range from 1 to 53. The first week of a
98
* calendar year is the earliest seven day period starting on {@link
99
* Calendar#getFirstDayOfWeek() getFirstDayOfWeek()} that contains at
100
* least {@link Calendar#getMinimalDaysInFirstWeek()
101
* getMinimalDaysInFirstWeek()} days from that year. It thus depends
102
* on the values of {@code getMinimalDaysInFirstWeek()}, {@code
103
* getFirstDayOfWeek()}, and the day of the week of January 1. Weeks
104
* between week 1 of one year and week 1 of the following year
105
* (exclusive) are numbered sequentially from 2 to 52 or 53 (except
106
* for year(s) involved in the Julian-Gregorian transition).
107
*
108
* <p>The {@code getFirstDayOfWeek()} and {@code
109
* getMinimalDaysInFirstWeek()} values are initialized using
110
* locale-dependent resources when constructing a {@code
111
* GregorianCalendar}. <a name="iso8601_compatible_setting">The week
112
* determination is compatible</a> with the ISO 8601 standard when {@code
113
* getFirstDayOfWeek()} is {@code MONDAY} and {@code
114
* getMinimalDaysInFirstWeek()} is 4, which values are used in locales
115
* where the standard is preferred. These values can explicitly be set by
116
* calling {@link Calendar#setFirstDayOfWeek(int) setFirstDayOfWeek()} and
117
* {@link Calendar#setMinimalDaysInFirstWeek(int)
118
* setMinimalDaysInFirstWeek()}.
119
*
120
* <p>A <a name="week_year"><em>week year</em></a> is in sync with a
121
* {@code WEEK_OF_YEAR} cycle. All weeks between the first and last
122
* weeks (inclusive) have the same <em>week year</em> value.
123
* Therefore, the first and last days of a week year may have
124
* different calendar year values.
125
*
126
* <p>For example, January 1, 1998 is a Thursday. If {@code
127
* getFirstDayOfWeek()} is {@code MONDAY} and {@code
128
* getMinimalDaysInFirstWeek()} is 4 (ISO 8601 standard compatible
129
* setting), then week 1 of 1998 starts on December 29, 1997, and ends
130
* on January 4, 1998. The week year is 1998 for the last three days
131
* of calendar year 1997. If, however, {@code getFirstDayOfWeek()} is
132
* {@code SUNDAY}, then week 1 of 1998 starts on January 4, 1998, and
133
* ends on January 10, 1998; the first three days of 1998 then are
134
* part of week 53 of 1997 and their week year is 1997.
135
*
136
* <h4>Week Of Month</h4>
137
*
138
* <p>Values calculated for the <code>WEEK_OF_MONTH</code> field range from 0
139
* to 6. Week 1 of a month (the days with <code>WEEK_OF_MONTH =
140
* 1</code>) is the earliest set of at least
141
* <code>getMinimalDaysInFirstWeek()</code> contiguous days in that month,
142
* ending on the day before <code>getFirstDayOfWeek()</code>. Unlike
143
* week 1 of a year, week 1 of a month may be shorter than 7 days, need
144
* not start on <code>getFirstDayOfWeek()</code>, and will not include days of
145
* the previous month. Days of a month before week 1 have a
146
* <code>WEEK_OF_MONTH</code> of 0.
147
*
148
* <p>For example, if <code>getFirstDayOfWeek()</code> is <code>SUNDAY</code>
149
* and <code>getMinimalDaysInFirstWeek()</code> is 4, then the first week of
150
* January 1998 is Sunday, January 4 through Saturday, January 10. These days
151
* have a <code>WEEK_OF_MONTH</code> of 1. Thursday, January 1 through
152
* Saturday, January 3 have a <code>WEEK_OF_MONTH</code> of 0. If
153
* <code>getMinimalDaysInFirstWeek()</code> is changed to 3, then January 1
154
* through January 3 have a <code>WEEK_OF_MONTH</code> of 1.
155
*
156
* <h4>Default Fields Values</h4>
157
*
158
* <p>The <code>clear</code> method sets calendar field(s)
159
* undefined. <code>GregorianCalendar</code> uses the following
160
* default value for each calendar field if its value is undefined.
161
*
162
* <table cellpadding="0" cellspacing="3" border="0"
163
* summary="GregorianCalendar default field values"
164
* style="text-align: left; width: 66%;">
165
* <tbody>
166
* <tr>
167
* <th style="vertical-align: top; background-color: rgb(204, 204, 255);
168
* text-align: center;">Field<br>
169
* </th>
170
* <th style="vertical-align: top; background-color: rgb(204, 204, 255);
171
* text-align: center;">Default Value<br>
172
* </th>
173
* </tr>
174
* <tr>
175
* <td style="vertical-align: middle;">
176
* <code>ERA<br></code>
177
* </td>
178
* <td style="vertical-align: middle;">
179
* <code>AD<br></code>
180
* </td>
181
* </tr>
182
* <tr>
183
* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
184
* <code>YEAR<br></code>
185
* </td>
186
* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
187
* <code>1970<br></code>
188
* </td>
189
* </tr>
190
* <tr>
191
* <td style="vertical-align: middle;">
192
* <code>MONTH<br></code>
193
* </td>
194
* <td style="vertical-align: middle;">
195
* <code>JANUARY<br></code>
196
* </td>
197
* </tr>
198
* <tr>
199
* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
200
* <code>DAY_OF_MONTH<br></code>
201
* </td>
202
* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
203
* <code>1<br></code>
204
* </td>
205
* </tr>
206
* <tr>
207
* <td style="vertical-align: middle;">
208
* <code>DAY_OF_WEEK<br></code>
209
* </td>
210
* <td style="vertical-align: middle;">
211
* <code>the first day of week<br></code>
212
* </td>
213
* </tr>
214
* <tr>
215
* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
216
* <code>WEEK_OF_MONTH<br></code>
217
* </td>
218
* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
219
* <code>0<br></code>
220
* </td>
221
* </tr>
222
* <tr>
223
* <td style="vertical-align: top;">
224
* <code>DAY_OF_WEEK_IN_MONTH<br></code>
225
* </td>
226
* <td style="vertical-align: top;">
227
* <code>1<br></code>
228
* </td>
229
* </tr>
230
* <tr>
231
* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
232
* <code>AM_PM<br></code>
233
* </td>
234
* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
235
* <code>AM<br></code>
236
* </td>
237
* </tr>
238
* <tr>
239
* <td style="vertical-align: middle;">
240
* <code>HOUR, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND<br></code>
241
* </td>
242
* <td style="vertical-align: middle;">
243
* <code>0<br></code>
244
* </td>
245
* </tr>
246
* </tbody>
247
* </table>
248
* <br>Default values are not applicable for the fields not listed above.
249
*
250
* <p>
251
* <strong>Example:</strong>
252
* <blockquote>
253
* <pre>
254
* // get the supported ids for GMT-08:00 (Pacific Standard Time)
255
* String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
256
* // if no ids were returned, something is wrong. get out.
257
* if (ids.length == 0)
258
* System.exit(0);
259
*
260
* // begin output
261
* System.out.println("Current Time");
262
*
263
* // create a Pacific Standard Time time zone
264
* SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
265
*
266
* // set up rules for Daylight Saving Time
267
* pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
268
* pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
269
*
270
* // create a GregorianCalendar with the Pacific Daylight time zone
271
* // and the current date and time
272
* Calendar calendar = new GregorianCalendar(pdt);
273
* Date trialTime = new Date();
274
* calendar.setTime(trialTime);
275
*
276
* // print out a bunch of interesting things
277
* System.out.println("ERA: " + calendar.get(Calendar.ERA));
278
* System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
279
* System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
280
* System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
281
* System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
282
* System.out.println("DATE: " + calendar.get(Calendar.DATE));
283
* System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
284
* System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
285
* System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
286
* System.out.println("DAY_OF_WEEK_IN_MONTH: "
287
* + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
288
* System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
289
* System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
290
* System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
291
* System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
292
* System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
293
* System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
294
* System.out.println("ZONE_OFFSET: "
295
* + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
296
* System.out.println("DST_OFFSET: "
297
* + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
298
299
* System.out.println("Current Time, with hour reset to 3");
300
* calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
301
* calendar.set(Calendar.HOUR, 3);
302
* System.out.println("ERA: " + calendar.get(Calendar.ERA));
303
* System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
304
* System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
305
* System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
306
* System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
307
* System.out.println("DATE: " + calendar.get(Calendar.DATE));
308
* System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
309
* System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
310
* System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
311
* System.out.println("DAY_OF_WEEK_IN_MONTH: "
312
* + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
313
* System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
314
* System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
315
* System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
316
* System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
317
* System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
318
* System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
319
* System.out.println("ZONE_OFFSET: "
320
* + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
321
* System.out.println("DST_OFFSET: "
322
* + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
323
* </pre>
324
* </blockquote>
325
*
326
* @see TimeZone
327
* @author David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu
328
* @since JDK1.1
329
*/
330
public class GregorianCalendar extends Calendar {
331
/*
332
* Implementation Notes
333
*
334
* The epoch is the number of days or milliseconds from some defined
335
* starting point. The epoch for java.util.Date is used here; that is,
336
* milliseconds from January 1, 1970 (Gregorian), midnight UTC. Other
337
* epochs which are used are January 1, year 1 (Gregorian), which is day 1
338
* of the Gregorian calendar, and December 30, year 0 (Gregorian), which is
339
* day 1 of the Julian calendar.
340
*
341
* We implement the proleptic Julian and Gregorian calendars. This means we
342
* implement the modern definition of the calendar even though the
343
* historical usage differs. For example, if the Gregorian change is set
344
* to new Date(Long.MIN_VALUE), we have a pure Gregorian calendar which
345
* labels dates preceding the invention of the Gregorian calendar in 1582 as
346
* if the calendar existed then.
347
*
348
* Likewise, with the Julian calendar, we assume a consistent
349
* 4-year leap year rule, even though the historical pattern of
350
* leap years is irregular, being every 3 years from 45 BCE
351
* through 9 BCE, then every 4 years from 8 CE onwards, with no
352
* leap years in-between. Thus date computations and functions
353
* such as isLeapYear() are not intended to be historically
354
* accurate.
355
*/
356
357
//////////////////
358
// Class Variables
359
//////////////////
360
361
/**
362
* Value of the <code>ERA</code> field indicating
363
* the period before the common era (before Christ), also known as BCE.
364
* The sequence of years at the transition from <code>BC</code> to <code>AD</code> is
365
* ..., 2 BC, 1 BC, 1 AD, 2 AD,...
366
*
367
* @see #ERA
368
*/
369
public static final int BC = 0;
370
371
/**
372
* Value of the {@link #ERA} field indicating
373
* the period before the common era, the same value as {@link #BC}.
374
*
375
* @see #CE
376
*/
377
static final int BCE = 0;
378
379
/**
380
* Value of the <code>ERA</code> field indicating
381
* the common era (Anno Domini), also known as CE.
382
* The sequence of years at the transition from <code>BC</code> to <code>AD</code> is
383
* ..., 2 BC, 1 BC, 1 AD, 2 AD,...
384
*
385
* @see #ERA
386
*/
387
public static final int AD = 1;
388
389
/**
390
* Value of the {@link #ERA} field indicating
391
* the common era, the same value as {@link #AD}.
392
*
393
* @see #BCE
394
*/
395
static final int CE = 1;
396
397
private static final int EPOCH_OFFSET = 719163; // Fixed date of January 1, 1970 (Gregorian)
398
private static final int EPOCH_YEAR = 1970;
399
400
static final int MONTH_LENGTH[]
401
= {31,28,31,30,31,30,31,31,30,31,30,31}; // 0-based
402
static final int LEAP_MONTH_LENGTH[]
403
= {31,29,31,30,31,30,31,31,30,31,30,31}; // 0-based
404
405
// Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit
406
// into ints, they must be longs in order to prevent arithmetic overflow
407
// when performing (bug 4173516).
408
private static final int ONE_SECOND = 1000;
409
private static final int ONE_MINUTE = 60*ONE_SECOND;
410
private static final int ONE_HOUR = 60*ONE_MINUTE;
411
private static final long ONE_DAY = 24*ONE_HOUR;
412
private static final long ONE_WEEK = 7*ONE_DAY;
413
414
/*
415
* <pre>
416
* Greatest Least
417
* Field name Minimum Minimum Maximum Maximum
418
* ---------- ------- ------- ------- -------
419
* ERA 0 0 1 1
420
* YEAR 1 1 292269054 292278994
421
* MONTH 0 0 11 11
422
* WEEK_OF_YEAR 1 1 52* 53
423
* WEEK_OF_MONTH 0 0 4* 6
424
* DAY_OF_MONTH 1 1 28* 31
425
* DAY_OF_YEAR 1 1 365* 366
426
* DAY_OF_WEEK 1 1 7 7
427
* DAY_OF_WEEK_IN_MONTH -1 -1 4* 6
428
* AM_PM 0 0 1 1
429
* HOUR 0 0 11 11
430
* HOUR_OF_DAY 0 0 23 23
431
* MINUTE 0 0 59 59
432
* SECOND 0 0 59 59
433
* MILLISECOND 0 0 999 999
434
* ZONE_OFFSET -13:00 -13:00 14:00 14:00
435
* DST_OFFSET 0:00 0:00 0:20 2:00
436
* </pre>
437
* *: depends on the Gregorian change date
438
*/
439
static final int MIN_VALUES[] = {
440
BCE, // ERA
441
1, // YEAR
442
JANUARY, // MONTH
443
1, // WEEK_OF_YEAR
444
0, // WEEK_OF_MONTH
445
1, // DAY_OF_MONTH
446
1, // DAY_OF_YEAR
447
SUNDAY, // DAY_OF_WEEK
448
1, // DAY_OF_WEEK_IN_MONTH
449
AM, // AM_PM
450
0, // HOUR
451
0, // HOUR_OF_DAY
452
0, // MINUTE
453
0, // SECOND
454
0, // MILLISECOND
455
-13*ONE_HOUR, // ZONE_OFFSET (UNIX compatibility)
456
0 // DST_OFFSET
457
};
458
static final int LEAST_MAX_VALUES[] = {
459
CE, // ERA
460
292269054, // YEAR
461
DECEMBER, // MONTH
462
52, // WEEK_OF_YEAR
463
4, // WEEK_OF_MONTH
464
28, // DAY_OF_MONTH
465
365, // DAY_OF_YEAR
466
SATURDAY, // DAY_OF_WEEK
467
4, // DAY_OF_WEEK_IN
468
PM, // AM_PM
469
11, // HOUR
470
23, // HOUR_OF_DAY
471
59, // MINUTE
472
59, // SECOND
473
999, // MILLISECOND
474
14*ONE_HOUR, // ZONE_OFFSET
475
20*ONE_MINUTE // DST_OFFSET (historical least maximum)
476
};
477
static final int MAX_VALUES[] = {
478
CE, // ERA
479
292278994, // YEAR
480
DECEMBER, // MONTH
481
53, // WEEK_OF_YEAR
482
6, // WEEK_OF_MONTH
483
31, // DAY_OF_MONTH
484
366, // DAY_OF_YEAR
485
SATURDAY, // DAY_OF_WEEK
486
6, // DAY_OF_WEEK_IN
487
PM, // AM_PM
488
11, // HOUR
489
23, // HOUR_OF_DAY
490
59, // MINUTE
491
59, // SECOND
492
999, // MILLISECOND
493
14*ONE_HOUR, // ZONE_OFFSET
494
2*ONE_HOUR // DST_OFFSET (double summer time)
495
};
496
497
// Proclaim serialization compatibility with JDK 1.1
498
@SuppressWarnings("FieldNameHidesFieldInSuperclass")
499
static final long serialVersionUID = -8125100834729963327L;
500
501
// Reference to the sun.util.calendar.Gregorian instance (singleton).
502
private static final Gregorian gcal =
503
CalendarSystem.getGregorianCalendar();
504
505
// Reference to the JulianCalendar instance (singleton), set as needed. See
506
// getJulianCalendarSystem().
507
private static JulianCalendar jcal;
508
509
// JulianCalendar eras. See getJulianCalendarSystem().
510
private static Era[] jeras;
511
512
// The default value of gregorianCutover.
513
static final long DEFAULT_GREGORIAN_CUTOVER = -12219292800000L;
514
515
/////////////////////
516
// Instance Variables
517
/////////////////////
518
519
/**
520
* The point at which the Gregorian calendar rules are used, measured in
521
* milliseconds from the standard epoch. Default is October 15, 1582
522
* (Gregorian) 00:00:00 UTC or -12219292800000L. For this value, October 4,
523
* 1582 (Julian) is followed by October 15, 1582 (Gregorian). This
524
* corresponds to Julian day number 2299161.
525
* @serial
526
*/
527
private long gregorianCutover = DEFAULT_GREGORIAN_CUTOVER;
528
529
/**
530
* The fixed date of the gregorianCutover.
531
*/
532
private transient long gregorianCutoverDate =
533
(((DEFAULT_GREGORIAN_CUTOVER + 1)/ONE_DAY) - 1) + EPOCH_OFFSET; // == 577736
534
535
/**
536
* The normalized year of the gregorianCutover in Gregorian, with
537
* 0 representing 1 BCE, -1 representing 2 BCE, etc.
538
*/
539
private transient int gregorianCutoverYear = 1582;
540
541
/**
542
* The normalized year of the gregorianCutover in Julian, with 0
543
* representing 1 BCE, -1 representing 2 BCE, etc.
544
*/
545
private transient int gregorianCutoverYearJulian = 1582;
546
547
/**
548
* gdate always has a sun.util.calendar.Gregorian.Date instance to
549
* avoid overhead of creating it. The assumption is that most
550
* applications will need only Gregorian calendar calculations.
551
*/
552
private transient BaseCalendar.Date gdate;
553
554
/**
555
* Reference to either gdate or a JulianCalendar.Date
556
* instance. After calling complete(), this value is guaranteed to
557
* be set.
558
*/
559
private transient BaseCalendar.Date cdate;
560
561
/**
562
* The CalendarSystem used to calculate the date in cdate. After
563
* calling complete(), this value is guaranteed to be set and
564
* consistent with the cdate value.
565
*/
566
private transient BaseCalendar calsys;
567
568
/**
569
* Temporary int[2] to get time zone offsets. zoneOffsets[0] gets
570
* the GMT offset value and zoneOffsets[1] gets the DST saving
571
* value.
572
*/
573
private transient int[] zoneOffsets;
574
575
/**
576
* Temporary storage for saving original fields[] values in
577
* non-lenient mode.
578
*/
579
private transient int[] originalFields;
580
581
///////////////
582
// Constructors
583
///////////////
584
585
/**
586
* Constructs a default <code>GregorianCalendar</code> using the current time
587
* in the default time zone with the default
588
* {@link Locale.Category#FORMAT FORMAT} locale.
589
*/
590
public GregorianCalendar() {
591
this(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
592
setZoneShared(true);
593
}
594
595
/**
596
* Constructs a <code>GregorianCalendar</code> based on the current time
597
* in the given time zone with the default
598
* {@link Locale.Category#FORMAT FORMAT} locale.
599
*
600
* @param zone the given time zone.
601
*/
602
public GregorianCalendar(TimeZone zone) {
603
this(zone, Locale.getDefault(Locale.Category.FORMAT));
604
}
605
606
/**
607
* Constructs a <code>GregorianCalendar</code> based on the current time
608
* in the default time zone with the given locale.
609
*
610
* @param aLocale the given locale.
611
*/
612
public GregorianCalendar(Locale aLocale) {
613
this(TimeZone.getDefaultRef(), aLocale);
614
setZoneShared(true);
615
}
616
617
/**
618
* Constructs a <code>GregorianCalendar</code> based on the current time
619
* in the given time zone with the given locale.
620
*
621
* @param zone the given time zone.
622
* @param aLocale the given locale.
623
*/
624
public GregorianCalendar(TimeZone zone, Locale aLocale) {
625
super(zone, aLocale);
626
gdate = (BaseCalendar.Date) gcal.newCalendarDate(zone);
627
setTimeInMillis(System.currentTimeMillis());
628
}
629
630
/**
631
* Constructs a <code>GregorianCalendar</code> with the given date set
632
* in the default time zone with the default locale.
633
*
634
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
635
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
636
* Month value is 0-based. e.g., 0 for January.
637
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
638
*/
639
public GregorianCalendar(int year, int month, int dayOfMonth) {
640
this(year, month, dayOfMonth, 0, 0, 0, 0);
641
}
642
643
/**
644
* Constructs a <code>GregorianCalendar</code> with the given date
645
* and time set for the default time zone with the default locale.
646
*
647
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
648
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
649
* Month value is 0-based. e.g., 0 for January.
650
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
651
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
652
* in the calendar.
653
* @param minute the value used to set the <code>MINUTE</code> calendar field
654
* in the calendar.
655
*/
656
public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,
657
int minute) {
658
this(year, month, dayOfMonth, hourOfDay, minute, 0, 0);
659
}
660
661
/**
662
* Constructs a GregorianCalendar with the given date
663
* and time set for the default time zone with the default locale.
664
*
665
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
666
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
667
* Month value is 0-based. e.g., 0 for January.
668
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
669
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
670
* in the calendar.
671
* @param minute the value used to set the <code>MINUTE</code> calendar field
672
* in the calendar.
673
* @param second the value used to set the <code>SECOND</code> calendar field
674
* in the calendar.
675
*/
676
public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,
677
int minute, int second) {
678
this(year, month, dayOfMonth, hourOfDay, minute, second, 0);
679
}
680
681
/**
682
* Constructs a <code>GregorianCalendar</code> with the given date
683
* and time set for the default time zone with the default locale.
684
*
685
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
686
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
687
* Month value is 0-based. e.g., 0 for January.
688
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
689
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
690
* in the calendar.
691
* @param minute the value used to set the <code>MINUTE</code> calendar field
692
* in the calendar.
693
* @param second the value used to set the <code>SECOND</code> calendar field
694
* in the calendar.
695
* @param millis the value used to set the <code>MILLISECOND</code> calendar field
696
*/
697
GregorianCalendar(int year, int month, int dayOfMonth,
698
int hourOfDay, int minute, int second, int millis) {
699
super();
700
gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());
701
this.set(YEAR, year);
702
this.set(MONTH, month);
703
this.set(DAY_OF_MONTH, dayOfMonth);
704
705
// Set AM_PM and HOUR here to set their stamp values before
706
// setting HOUR_OF_DAY (6178071).
707
if (hourOfDay >= 12 && hourOfDay <= 23) {
708
// If hourOfDay is a valid PM hour, set the correct PM values
709
// so that it won't throw an exception in case it's set to
710
// non-lenient later.
711
this.internalSet(AM_PM, PM);
712
this.internalSet(HOUR, hourOfDay - 12);
713
} else {
714
// The default value for AM_PM is AM.
715
// We don't care any out of range value here for leniency.
716
this.internalSet(HOUR, hourOfDay);
717
}
718
// The stamp values of AM_PM and HOUR must be COMPUTED. (6440854)
719
setFieldsComputed(HOUR_MASK|AM_PM_MASK);
720
721
this.set(HOUR_OF_DAY, hourOfDay);
722
this.set(MINUTE, minute);
723
this.set(SECOND, second);
724
// should be changed to set() when this constructor is made
725
// public.
726
this.internalSet(MILLISECOND, millis);
727
}
728
729
/**
730
* Constructs an empty GregorianCalendar.
731
*
732
* @param zone the given time zone
733
* @param aLocale the given locale
734
* @param flag the flag requesting an empty instance
735
*/
736
GregorianCalendar(TimeZone zone, Locale locale, boolean flag) {
737
super(zone, locale);
738
gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());
739
}
740
741
/////////////////
742
// Public methods
743
/////////////////
744
745
/**
746
* Sets the <code>GregorianCalendar</code> change date. This is the point when the switch
747
* from Julian dates to Gregorian dates occurred. Default is October 15,
748
* 1582 (Gregorian). Previous to this, dates will be in the Julian calendar.
749
* <p>
750
* To obtain a pure Julian calendar, set the change date to
751
* <code>Date(Long.MAX_VALUE)</code>. To obtain a pure Gregorian calendar,
752
* set the change date to <code>Date(Long.MIN_VALUE)</code>.
753
*
754
* @param date the given Gregorian cutover date.
755
*/
756
public void setGregorianChange(Date date) {
757
long cutoverTime = date.getTime();
758
if (cutoverTime == gregorianCutover) {
759
return;
760
}
761
// Before changing the cutover date, make sure to have the
762
// time of this calendar.
763
complete();
764
setGregorianChange(cutoverTime);
765
}
766
767
private void setGregorianChange(long cutoverTime) {
768
gregorianCutover = cutoverTime;
769
gregorianCutoverDate = CalendarUtils.floorDivide(cutoverTime, ONE_DAY)
770
+ EPOCH_OFFSET;
771
772
// To provide the "pure" Julian calendar as advertised.
773
// Strictly speaking, the last millisecond should be a
774
// Gregorian date. However, the API doc specifies that setting
775
// the cutover date to Long.MAX_VALUE will make this calendar
776
// a pure Julian calendar. (See 4167995)
777
if (cutoverTime == Long.MAX_VALUE) {
778
gregorianCutoverDate++;
779
}
780
781
BaseCalendar.Date d = getGregorianCutoverDate();
782
783
// Set the cutover year (in the Gregorian year numbering)
784
gregorianCutoverYear = d.getYear();
785
786
BaseCalendar julianCal = getJulianCalendarSystem();
787
d = (BaseCalendar.Date) julianCal.newCalendarDate(TimeZone.NO_TIMEZONE);
788
julianCal.getCalendarDateFromFixedDate(d, gregorianCutoverDate - 1);
789
gregorianCutoverYearJulian = d.getNormalizedYear();
790
791
if (time < gregorianCutover) {
792
// The field values are no longer valid under the new
793
// cutover date.
794
setUnnormalized();
795
}
796
}
797
798
/**
799
* Gets the Gregorian Calendar change date. This is the point when the
800
* switch from Julian dates to Gregorian dates occurred. Default is
801
* October 15, 1582 (Gregorian). Previous to this, dates will be in the Julian
802
* calendar.
803
*
804
* @return the Gregorian cutover date for this <code>GregorianCalendar</code> object.
805
*/
806
public final Date getGregorianChange() {
807
return new Date(gregorianCutover);
808
}
809
810
/**
811
* Determines if the given year is a leap year. Returns <code>true</code> if
812
* the given year is a leap year. To specify BC year numbers,
813
* <code>1 - year number</code> must be given. For example, year BC 4 is
814
* specified as -3.
815
*
816
* @param year the given year.
817
* @return <code>true</code> if the given year is a leap year; <code>false</code> otherwise.
818
*/
819
public boolean isLeapYear(int year) {
820
if ((year & 3) != 0) {
821
return false;
822
}
823
824
if (year > gregorianCutoverYear) {
825
return (year%100 != 0) || (year%400 == 0); // Gregorian
826
}
827
if (year < gregorianCutoverYearJulian) {
828
return true; // Julian
829
}
830
boolean gregorian;
831
// If the given year is the Gregorian cutover year, we need to
832
// determine which calendar system to be applied to February in the year.
833
if (gregorianCutoverYear == gregorianCutoverYearJulian) {
834
BaseCalendar.Date d = getCalendarDate(gregorianCutoverDate); // Gregorian
835
gregorian = d.getMonth() < BaseCalendar.MARCH;
836
} else {
837
gregorian = year == gregorianCutoverYear;
838
}
839
return gregorian ? (year%100 != 0) || (year%400 == 0) : true;
840
}
841
842
/**
843
* Returns {@code "gregory"} as the calendar type.
844
*
845
* @return {@code "gregory"}
846
* @since 1.8
847
*/
848
@Override
849
public String getCalendarType() {
850
return "gregory";
851
}
852
853
/**
854
* Compares this <code>GregorianCalendar</code> to the specified
855
* <code>Object</code>. The result is <code>true</code> if and
856
* only if the argument is a <code>GregorianCalendar</code> object
857
* that represents the same time value (millisecond offset from
858
* the <a href="Calendar.html#Epoch">Epoch</a>) under the same
859
* <code>Calendar</code> parameters and Gregorian change date as
860
* this object.
861
*
862
* @param obj the object to compare with.
863
* @return <code>true</code> if this object is equal to <code>obj</code>;
864
* <code>false</code> otherwise.
865
* @see Calendar#compareTo(Calendar)
866
*/
867
@Override
868
public boolean equals(Object obj) {
869
return obj instanceof GregorianCalendar &&
870
super.equals(obj) &&
871
gregorianCutover == ((GregorianCalendar)obj).gregorianCutover;
872
}
873
874
/**
875
* Generates the hash code for this <code>GregorianCalendar</code> object.
876
*/
877
@Override
878
public int hashCode() {
879
return super.hashCode() ^ (int)gregorianCutoverDate;
880
}
881
882
/**
883
* Adds the specified (signed) amount of time to the given calendar field,
884
* based on the calendar's rules.
885
*
886
* <p><em>Add rule 1</em>. The value of <code>field</code>
887
* after the call minus the value of <code>field</code> before the
888
* call is <code>amount</code>, modulo any overflow that has occurred in
889
* <code>field</code>. Overflow occurs when a field value exceeds its
890
* range and, as a result, the next larger field is incremented or
891
* decremented and the field value is adjusted back into its range.</p>
892
*
893
* <p><em>Add rule 2</em>. If a smaller field is expected to be
894
* invariant, but it is impossible for it to be equal to its
895
* prior value because of changes in its minimum or maximum after
896
* <code>field</code> is changed, then its value is adjusted to be as close
897
* as possible to its expected value. A smaller field represents a
898
* smaller unit of time. <code>HOUR</code> is a smaller field than
899
* <code>DAY_OF_MONTH</code>. No adjustment is made to smaller fields
900
* that are not expected to be invariant. The calendar system
901
* determines what fields are expected to be invariant.</p>
902
*
903
* @param field the calendar field.
904
* @param amount the amount of date or time to be added to the field.
905
* @exception IllegalArgumentException if <code>field</code> is
906
* <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,
907
* or if any calendar fields have out-of-range values in
908
* non-lenient mode.
909
*/
910
@Override
911
public void add(int field, int amount) {
912
// If amount == 0, do nothing even the given field is out of
913
// range. This is tested by JCK.
914
if (amount == 0) {
915
return; // Do nothing!
916
}
917
918
if (field < 0 || field >= ZONE_OFFSET) {
919
throw new IllegalArgumentException();
920
}
921
922
// Sync the time and calendar fields.
923
complete();
924
925
if (field == YEAR) {
926
int year = internalGet(YEAR);
927
if (internalGetEra() == CE) {
928
year += amount;
929
if (year > 0) {
930
set(YEAR, year);
931
} else { // year <= 0
932
set(YEAR, 1 - year);
933
// if year == 0, you get 1 BCE.
934
set(ERA, BCE);
935
}
936
}
937
else { // era == BCE
938
year -= amount;
939
if (year > 0) {
940
set(YEAR, year);
941
} else { // year <= 0
942
set(YEAR, 1 - year);
943
// if year == 0, you get 1 CE
944
set(ERA, CE);
945
}
946
}
947
pinDayOfMonth();
948
} else if (field == MONTH) {
949
int month = internalGet(MONTH) + amount;
950
int year = internalGet(YEAR);
951
int y_amount;
952
953
if (month >= 0) {
954
y_amount = month/12;
955
} else {
956
y_amount = (month+1)/12 - 1;
957
}
958
if (y_amount != 0) {
959
if (internalGetEra() == CE) {
960
year += y_amount;
961
if (year > 0) {
962
set(YEAR, year);
963
} else { // year <= 0
964
set(YEAR, 1 - year);
965
// if year == 0, you get 1 BCE
966
set(ERA, BCE);
967
}
968
}
969
else { // era == BCE
970
year -= y_amount;
971
if (year > 0) {
972
set(YEAR, year);
973
} else { // year <= 0
974
set(YEAR, 1 - year);
975
// if year == 0, you get 1 CE
976
set(ERA, CE);
977
}
978
}
979
}
980
981
if (month >= 0) {
982
set(MONTH, month % 12);
983
} else {
984
// month < 0
985
month %= 12;
986
if (month < 0) {
987
month += 12;
988
}
989
set(MONTH, JANUARY + month);
990
}
991
pinDayOfMonth();
992
} else if (field == ERA) {
993
int era = internalGet(ERA) + amount;
994
if (era < 0) {
995
era = 0;
996
}
997
if (era > 1) {
998
era = 1;
999
}
1000
set(ERA, era);
1001
} else {
1002
long delta = amount;
1003
long timeOfDay = 0;
1004
switch (field) {
1005
// Handle the time fields here. Convert the given
1006
// amount to milliseconds and call setTimeInMillis.
1007
case HOUR:
1008
case HOUR_OF_DAY:
1009
delta *= 60 * 60 * 1000; // hours to minutes
1010
break;
1011
1012
case MINUTE:
1013
delta *= 60 * 1000; // minutes to seconds
1014
break;
1015
1016
case SECOND:
1017
delta *= 1000; // seconds to milliseconds
1018
break;
1019
1020
case MILLISECOND:
1021
break;
1022
1023
// Handle week, day and AM_PM fields which involves
1024
// time zone offset change adjustment. Convert the
1025
// given amount to the number of days.
1026
case WEEK_OF_YEAR:
1027
case WEEK_OF_MONTH:
1028
case DAY_OF_WEEK_IN_MONTH:
1029
delta *= 7;
1030
break;
1031
1032
case DAY_OF_MONTH: // synonym of DATE
1033
case DAY_OF_YEAR:
1034
case DAY_OF_WEEK:
1035
break;
1036
1037
case AM_PM:
1038
// Convert the amount to the number of days (delta)
1039
// and +12 or -12 hours (timeOfDay).
1040
delta = amount / 2;
1041
timeOfDay = 12 * (amount % 2);
1042
break;
1043
}
1044
1045
// The time fields don't require time zone offset change
1046
// adjustment.
1047
if (field >= HOUR) {
1048
setTimeInMillis(time + delta);
1049
return;
1050
}
1051
1052
// The rest of the fields (week, day or AM_PM fields)
1053
// require time zone offset (both GMT and DST) change
1054
// adjustment.
1055
1056
// Translate the current time to the fixed date and time
1057
// of the day.
1058
long fd = getCurrentFixedDate();
1059
timeOfDay += internalGet(HOUR_OF_DAY);
1060
timeOfDay *= 60;
1061
timeOfDay += internalGet(MINUTE);
1062
timeOfDay *= 60;
1063
timeOfDay += internalGet(SECOND);
1064
timeOfDay *= 1000;
1065
timeOfDay += internalGet(MILLISECOND);
1066
if (timeOfDay >= ONE_DAY) {
1067
fd++;
1068
timeOfDay -= ONE_DAY;
1069
} else if (timeOfDay < 0) {
1070
fd--;
1071
timeOfDay += ONE_DAY;
1072
}
1073
1074
fd += delta; // fd is the expected fixed date after the calculation
1075
int zoneOffset = internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);
1076
setTimeInMillis((fd - EPOCH_OFFSET) * ONE_DAY + timeOfDay - zoneOffset);
1077
zoneOffset -= internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);
1078
// If the time zone offset has changed, then adjust the difference.
1079
if (zoneOffset != 0) {
1080
setTimeInMillis(time + zoneOffset);
1081
long fd2 = getCurrentFixedDate();
1082
// If the adjustment has changed the date, then take
1083
// the previous one.
1084
if (fd2 != fd) {
1085
setTimeInMillis(time - zoneOffset);
1086
}
1087
}
1088
}
1089
}
1090
1091
/**
1092
* Adds or subtracts (up/down) a single unit of time on the given time
1093
* field without changing larger fields.
1094
* <p>
1095
* <em>Example</em>: Consider a <code>GregorianCalendar</code>
1096
* originally set to December 31, 1999. Calling {@link #roll(int,boolean) roll(Calendar.MONTH, true)}
1097
* sets the calendar to January 31, 1999. The <code>YEAR</code> field is unchanged
1098
* because it is a larger field than <code>MONTH</code>.</p>
1099
*
1100
* @param up indicates if the value of the specified calendar field is to be
1101
* rolled up or rolled down. Use <code>true</code> if rolling up, <code>false</code> otherwise.
1102
* @exception IllegalArgumentException if <code>field</code> is
1103
* <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,
1104
* or if any calendar fields have out-of-range values in
1105
* non-lenient mode.
1106
* @see #add(int,int)
1107
* @see #set(int,int)
1108
*/
1109
@Override
1110
public void roll(int field, boolean up) {
1111
roll(field, up ? +1 : -1);
1112
}
1113
1114
/**
1115
* Adds a signed amount to the specified calendar field without changing larger fields.
1116
* A negative roll amount means to subtract from field without changing
1117
* larger fields. If the specified amount is 0, this method performs nothing.
1118
*
1119
* <p>This method calls {@link #complete()} before adding the
1120
* amount so that all the calendar fields are normalized. If there
1121
* is any calendar field having an out-of-range value in non-lenient mode, then an
1122
* <code>IllegalArgumentException</code> is thrown.
1123
*
1124
* <p>
1125
* <em>Example</em>: Consider a <code>GregorianCalendar</code>
1126
* originally set to August 31, 1999. Calling <code>roll(Calendar.MONTH,
1127
* 8)</code> sets the calendar to April 30, <strong>1999</strong>. Using a
1128
* <code>GregorianCalendar</code>, the <code>DAY_OF_MONTH</code> field cannot
1129
* be 31 in the month April. <code>DAY_OF_MONTH</code> is set to the closest possible
1130
* value, 30. The <code>YEAR</code> field maintains the value of 1999 because it
1131
* is a larger field than <code>MONTH</code>.
1132
* <p>
1133
* <em>Example</em>: Consider a <code>GregorianCalendar</code>
1134
* originally set to Sunday June 6, 1999. Calling
1135
* <code>roll(Calendar.WEEK_OF_MONTH, -1)</code> sets the calendar to
1136
* Tuesday June 1, 1999, whereas calling
1137
* <code>add(Calendar.WEEK_OF_MONTH, -1)</code> sets the calendar to
1138
* Sunday May 30, 1999. This is because the roll rule imposes an
1139
* additional constraint: The <code>MONTH</code> must not change when the
1140
* <code>WEEK_OF_MONTH</code> is rolled. Taken together with add rule 1,
1141
* the resultant date must be between Tuesday June 1 and Saturday June
1142
* 5. According to add rule 2, the <code>DAY_OF_WEEK</code>, an invariant
1143
* when changing the <code>WEEK_OF_MONTH</code>, is set to Tuesday, the
1144
* closest possible value to Sunday (where Sunday is the first day of the
1145
* week).</p>
1146
*
1147
* @param field the calendar field.
1148
* @param amount the signed amount to add to <code>field</code>.
1149
* @exception IllegalArgumentException if <code>field</code> is
1150
* <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,
1151
* or if any calendar fields have out-of-range values in
1152
* non-lenient mode.
1153
* @see #roll(int,boolean)
1154
* @see #add(int,int)
1155
* @see #set(int,int)
1156
* @since 1.2
1157
*/
1158
@Override
1159
public void roll(int field, int amount) {
1160
// If amount == 0, do nothing even the given field is out of
1161
// range. This is tested by JCK.
1162
if (amount == 0) {
1163
return;
1164
}
1165
1166
if (field < 0 || field >= ZONE_OFFSET) {
1167
throw new IllegalArgumentException();
1168
}
1169
1170
// Sync the time and calendar fields.
1171
complete();
1172
1173
int min = getMinimum(field);
1174
int max = getMaximum(field);
1175
1176
switch (field) {
1177
case AM_PM:
1178
case ERA:
1179
case YEAR:
1180
case MINUTE:
1181
case SECOND:
1182
case MILLISECOND:
1183
// These fields are handled simply, since they have fixed minima
1184
// and maxima. The field DAY_OF_MONTH is almost as simple. Other
1185
// fields are complicated, since the range within they must roll
1186
// varies depending on the date.
1187
break;
1188
1189
case HOUR:
1190
case HOUR_OF_DAY:
1191
{
1192
int rolledValue = getRolledValue(internalGet(field), amount, min, max);
1193
int hourOfDay = rolledValue;
1194
if (field == HOUR && internalGet(AM_PM) == PM) {
1195
hourOfDay += 12;
1196
}
1197
1198
// Create the current date/time value to perform wall-clock-based
1199
// roll.
1200
CalendarDate d = calsys.getCalendarDate(time, getZone());
1201
d.setHours(hourOfDay);
1202
time = calsys.getTime(d);
1203
1204
// If we stay on the same wall-clock time, try the next or previous hour.
1205
if (internalGet(HOUR_OF_DAY) == d.getHours()) {
1206
hourOfDay = getRolledValue(rolledValue, amount > 0 ? +1 : -1, min, max);
1207
if (field == HOUR && internalGet(AM_PM) == PM) {
1208
hourOfDay += 12;
1209
}
1210
d.setHours(hourOfDay);
1211
time = calsys.getTime(d);
1212
}
1213
// Get the new hourOfDay value which might have changed due to a DST transition.
1214
hourOfDay = d.getHours();
1215
// Update the hour related fields
1216
internalSet(HOUR_OF_DAY, hourOfDay);
1217
internalSet(AM_PM, hourOfDay / 12);
1218
internalSet(HOUR, hourOfDay % 12);
1219
1220
// Time zone offset and/or daylight saving might have changed.
1221
int zoneOffset = d.getZoneOffset();
1222
int saving = d.getDaylightSaving();
1223
internalSet(ZONE_OFFSET, zoneOffset - saving);
1224
internalSet(DST_OFFSET, saving);
1225
return;
1226
}
1227
1228
case MONTH:
1229
// Rolling the month involves both pinning the final value to [0, 11]
1230
// and adjusting the DAY_OF_MONTH if necessary. We only adjust the
1231
// DAY_OF_MONTH if, after updating the MONTH field, it is illegal.
1232
// E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>.
1233
{
1234
if (!isCutoverYear(cdate.getNormalizedYear())) {
1235
int mon = (internalGet(MONTH) + amount) % 12;
1236
if (mon < 0) {
1237
mon += 12;
1238
}
1239
set(MONTH, mon);
1240
1241
// Keep the day of month in the range. We don't want to spill over
1242
// into the next month; e.g., we don't want jan31 + 1 mo -> feb31 ->
1243
// mar3.
1244
int monthLen = monthLength(mon);
1245
if (internalGet(DAY_OF_MONTH) > monthLen) {
1246
set(DAY_OF_MONTH, monthLen);
1247
}
1248
} else {
1249
// We need to take care of different lengths in
1250
// year and month due to the cutover.
1251
int yearLength = getActualMaximum(MONTH) + 1;
1252
int mon = (internalGet(MONTH) + amount) % yearLength;
1253
if (mon < 0) {
1254
mon += yearLength;
1255
}
1256
set(MONTH, mon);
1257
int monthLen = getActualMaximum(DAY_OF_MONTH);
1258
if (internalGet(DAY_OF_MONTH) > monthLen) {
1259
set(DAY_OF_MONTH, monthLen);
1260
}
1261
}
1262
return;
1263
}
1264
1265
case WEEK_OF_YEAR:
1266
{
1267
int y = cdate.getNormalizedYear();
1268
max = getActualMaximum(WEEK_OF_YEAR);
1269
set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));
1270
int woy = internalGet(WEEK_OF_YEAR);
1271
int value = woy + amount;
1272
if (!isCutoverYear(y)) {
1273
int weekYear = getWeekYear();
1274
if (weekYear == y) {
1275
// If the new value is in between min and max
1276
// (exclusive), then we can use the value.
1277
if (value > min && value < max) {
1278
set(WEEK_OF_YEAR, value);
1279
return;
1280
}
1281
long fd = getCurrentFixedDate();
1282
// Make sure that the min week has the current DAY_OF_WEEK
1283
// in the calendar year
1284
long day1 = fd - (7 * (woy - min));
1285
if (calsys.getYearFromFixedDate(day1) != y) {
1286
min++;
1287
}
1288
1289
// Make sure the same thing for the max week
1290
fd += 7 * (max - internalGet(WEEK_OF_YEAR));
1291
if (calsys.getYearFromFixedDate(fd) != y) {
1292
max--;
1293
}
1294
} else {
1295
// When WEEK_OF_YEAR and YEAR are out of sync,
1296
// adjust woy and amount to stay in the calendar year.
1297
if (weekYear > y) {
1298
if (amount < 0) {
1299
amount++;
1300
}
1301
woy = max;
1302
} else {
1303
if (amount > 0) {
1304
amount -= woy - max;
1305
}
1306
woy = min;
1307
}
1308
}
1309
set(field, getRolledValue(woy, amount, min, max));
1310
return;
1311
}
1312
1313
// Handle cutover here.
1314
long fd = getCurrentFixedDate();
1315
BaseCalendar cal;
1316
if (gregorianCutoverYear == gregorianCutoverYearJulian) {
1317
cal = getCutoverCalendarSystem();
1318
} else if (y == gregorianCutoverYear) {
1319
cal = gcal;
1320
} else {
1321
cal = getJulianCalendarSystem();
1322
}
1323
long day1 = fd - (7 * (woy - min));
1324
// Make sure that the min week has the current DAY_OF_WEEK
1325
if (cal.getYearFromFixedDate(day1) != y) {
1326
min++;
1327
}
1328
1329
// Make sure the same thing for the max week
1330
fd += 7 * (max - woy);
1331
cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
1332
if (cal.getYearFromFixedDate(fd) != y) {
1333
max--;
1334
}
1335
// value: the new WEEK_OF_YEAR which must be converted
1336
// to month and day of month.
1337
value = getRolledValue(woy, amount, min, max) - 1;
1338
BaseCalendar.Date d = getCalendarDate(day1 + value * 7);
1339
set(MONTH, d.getMonth() - 1);
1340
set(DAY_OF_MONTH, d.getDayOfMonth());
1341
return;
1342
}
1343
1344
case WEEK_OF_MONTH:
1345
{
1346
boolean isCutoverYear = isCutoverYear(cdate.getNormalizedYear());
1347
// dow: relative day of week from first day of week
1348
int dow = internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
1349
if (dow < 0) {
1350
dow += 7;
1351
}
1352
1353
long fd = getCurrentFixedDate();
1354
long month1; // fixed date of the first day (usually 1) of the month
1355
int monthLength; // actual month length
1356
if (isCutoverYear) {
1357
month1 = getFixedDateMonth1(cdate, fd);
1358
monthLength = actualMonthLength();
1359
} else {
1360
month1 = fd - internalGet(DAY_OF_MONTH) + 1;
1361
monthLength = calsys.getMonthLength(cdate);
1362
}
1363
1364
// the first day of week of the month.
1365
long monthDay1st = BaseCalendar.getDayOfWeekDateOnOrBefore(month1 + 6,
1366
getFirstDayOfWeek());
1367
// if the week has enough days to form a week, the
1368
// week starts from the previous month.
1369
if ((int)(monthDay1st - month1) >= getMinimalDaysInFirstWeek()) {
1370
monthDay1st -= 7;
1371
}
1372
max = getActualMaximum(field);
1373
1374
// value: the new WEEK_OF_MONTH value
1375
int value = getRolledValue(internalGet(field), amount, 1, max) - 1;
1376
1377
// nfd: fixed date of the rolled date
1378
long nfd = monthDay1st + value * 7 + dow;
1379
1380
// Unlike WEEK_OF_YEAR, we need to change day of week if the
1381
// nfd is out of the month.
1382
if (nfd < month1) {
1383
nfd = month1;
1384
} else if (nfd >= (month1 + monthLength)) {
1385
nfd = month1 + monthLength - 1;
1386
}
1387
int dayOfMonth;
1388
if (isCutoverYear) {
1389
// If we are in the cutover year, convert nfd to
1390
// its calendar date and use dayOfMonth.
1391
BaseCalendar.Date d = getCalendarDate(nfd);
1392
dayOfMonth = d.getDayOfMonth();
1393
} else {
1394
dayOfMonth = (int)(nfd - month1) + 1;
1395
}
1396
set(DAY_OF_MONTH, dayOfMonth);
1397
return;
1398
}
1399
1400
case DAY_OF_MONTH:
1401
{
1402
if (!isCutoverYear(cdate.getNormalizedYear())) {
1403
max = calsys.getMonthLength(cdate);
1404
break;
1405
}
1406
1407
// Cutover year handling
1408
long fd = getCurrentFixedDate();
1409
long month1 = getFixedDateMonth1(cdate, fd);
1410
// It may not be a regular month. Convert the date and range to
1411
// the relative values, perform the roll, and
1412
// convert the result back to the rolled date.
1413
int value = getRolledValue((int)(fd - month1), amount, 0, actualMonthLength() - 1);
1414
BaseCalendar.Date d = getCalendarDate(month1 + value);
1415
assert d.getMonth()-1 == internalGet(MONTH);
1416
set(DAY_OF_MONTH, d.getDayOfMonth());
1417
return;
1418
}
1419
1420
case DAY_OF_YEAR:
1421
{
1422
max = getActualMaximum(field);
1423
if (!isCutoverYear(cdate.getNormalizedYear())) {
1424
break;
1425
}
1426
1427
// Handle cutover here.
1428
long fd = getCurrentFixedDate();
1429
long jan1 = fd - internalGet(DAY_OF_YEAR) + 1;
1430
int value = getRolledValue((int)(fd - jan1) + 1, amount, min, max);
1431
BaseCalendar.Date d = getCalendarDate(jan1 + value - 1);
1432
set(MONTH, d.getMonth() - 1);
1433
set(DAY_OF_MONTH, d.getDayOfMonth());
1434
return;
1435
}
1436
1437
case DAY_OF_WEEK:
1438
{
1439
if (!isCutoverYear(cdate.getNormalizedYear())) {
1440
// If the week of year is in the same year, we can
1441
// just change DAY_OF_WEEK.
1442
int weekOfYear = internalGet(WEEK_OF_YEAR);
1443
if (weekOfYear > 1 && weekOfYear < 52) {
1444
set(WEEK_OF_YEAR, weekOfYear); // update stamp[WEEK_OF_YEAR]
1445
max = SATURDAY;
1446
break;
1447
}
1448
}
1449
1450
// We need to handle it in a different way around year
1451
// boundaries and in the cutover year. Note that
1452
// changing era and year values violates the roll
1453
// rule: not changing larger calendar fields...
1454
amount %= 7;
1455
if (amount == 0) {
1456
return;
1457
}
1458
long fd = getCurrentFixedDate();
1459
long dowFirst = BaseCalendar.getDayOfWeekDateOnOrBefore(fd, getFirstDayOfWeek());
1460
fd += amount;
1461
if (fd < dowFirst) {
1462
fd += 7;
1463
} else if (fd >= dowFirst + 7) {
1464
fd -= 7;
1465
}
1466
BaseCalendar.Date d = getCalendarDate(fd);
1467
set(ERA, (d.getNormalizedYear() <= 0 ? BCE : CE));
1468
set(d.getYear(), d.getMonth() - 1, d.getDayOfMonth());
1469
return;
1470
}
1471
1472
case DAY_OF_WEEK_IN_MONTH:
1473
{
1474
min = 1; // after normalized, min should be 1.
1475
if (!isCutoverYear(cdate.getNormalizedYear())) {
1476
int dom = internalGet(DAY_OF_MONTH);
1477
int monthLength = calsys.getMonthLength(cdate);
1478
int lastDays = monthLength % 7;
1479
max = monthLength / 7;
1480
int x = (dom - 1) % 7;
1481
if (x < lastDays) {
1482
max++;
1483
}
1484
set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));
1485
break;
1486
}
1487
1488
// Cutover year handling
1489
long fd = getCurrentFixedDate();
1490
long month1 = getFixedDateMonth1(cdate, fd);
1491
int monthLength = actualMonthLength();
1492
int lastDays = monthLength % 7;
1493
max = monthLength / 7;
1494
int x = (int)(fd - month1) % 7;
1495
if (x < lastDays) {
1496
max++;
1497
}
1498
int value = getRolledValue(internalGet(field), amount, min, max) - 1;
1499
fd = month1 + value * 7 + x;
1500
BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
1501
BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
1502
cal.getCalendarDateFromFixedDate(d, fd);
1503
set(DAY_OF_MONTH, d.getDayOfMonth());
1504
return;
1505
}
1506
}
1507
1508
set(field, getRolledValue(internalGet(field), amount, min, max));
1509
}
1510
1511
/**
1512
* Returns the minimum value for the given calendar field of this
1513
* <code>GregorianCalendar</code> instance. The minimum value is
1514
* defined as the smallest value returned by the {@link
1515
* Calendar#get(int) get} method for any possible time value,
1516
* taking into consideration the current values of the
1517
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1518
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1519
* {@link #getGregorianChange() getGregorianChange} and
1520
* {@link Calendar#getTimeZone() getTimeZone} methods.
1521
*
1522
* @param field the calendar field.
1523
* @return the minimum value for the given calendar field.
1524
* @see #getMaximum(int)
1525
* @see #getGreatestMinimum(int)
1526
* @see #getLeastMaximum(int)
1527
* @see #getActualMinimum(int)
1528
* @see #getActualMaximum(int)
1529
*/
1530
@Override
1531
public int getMinimum(int field) {
1532
return MIN_VALUES[field];
1533
}
1534
1535
/**
1536
* Returns the maximum value for the given calendar field of this
1537
* <code>GregorianCalendar</code> instance. The maximum value is
1538
* defined as the largest value returned by the {@link
1539
* Calendar#get(int) get} method for any possible time value,
1540
* taking into consideration the current values of the
1541
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1542
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1543
* {@link #getGregorianChange() getGregorianChange} and
1544
* {@link Calendar#getTimeZone() getTimeZone} methods.
1545
*
1546
* @param field the calendar field.
1547
* @return the maximum value for the given calendar field.
1548
* @see #getMinimum(int)
1549
* @see #getGreatestMinimum(int)
1550
* @see #getLeastMaximum(int)
1551
* @see #getActualMinimum(int)
1552
* @see #getActualMaximum(int)
1553
*/
1554
@Override
1555
public int getMaximum(int field) {
1556
switch (field) {
1557
case MONTH:
1558
case DAY_OF_MONTH:
1559
case DAY_OF_YEAR:
1560
case WEEK_OF_YEAR:
1561
case WEEK_OF_MONTH:
1562
case DAY_OF_WEEK_IN_MONTH:
1563
case YEAR:
1564
{
1565
// On or after Gregorian 200-3-1, Julian and Gregorian
1566
// calendar dates are the same or Gregorian dates are
1567
// larger (i.e., there is a "gap") after 300-3-1.
1568
if (gregorianCutoverYear > 200) {
1569
break;
1570
}
1571
// There might be "overlapping" dates.
1572
GregorianCalendar gc = (GregorianCalendar) clone();
1573
gc.setLenient(true);
1574
gc.setTimeInMillis(gregorianCutover);
1575
int v1 = gc.getActualMaximum(field);
1576
gc.setTimeInMillis(gregorianCutover-1);
1577
int v2 = gc.getActualMaximum(field);
1578
return Math.max(MAX_VALUES[field], Math.max(v1, v2));
1579
}
1580
}
1581
return MAX_VALUES[field];
1582
}
1583
1584
/**
1585
* Returns the highest minimum value for the given calendar field
1586
* of this <code>GregorianCalendar</code> instance. The highest
1587
* minimum value is defined as the largest value returned by
1588
* {@link #getActualMinimum(int)} for any possible time value,
1589
* taking into consideration the current values of the
1590
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1591
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1592
* {@link #getGregorianChange() getGregorianChange} and
1593
* {@link Calendar#getTimeZone() getTimeZone} methods.
1594
*
1595
* @param field the calendar field.
1596
* @return the highest minimum value for the given calendar field.
1597
* @see #getMinimum(int)
1598
* @see #getMaximum(int)
1599
* @see #getLeastMaximum(int)
1600
* @see #getActualMinimum(int)
1601
* @see #getActualMaximum(int)
1602
*/
1603
@Override
1604
public int getGreatestMinimum(int field) {
1605
if (field == DAY_OF_MONTH) {
1606
BaseCalendar.Date d = getGregorianCutoverDate();
1607
long mon1 = getFixedDateMonth1(d, gregorianCutoverDate);
1608
d = getCalendarDate(mon1);
1609
return Math.max(MIN_VALUES[field], d.getDayOfMonth());
1610
}
1611
return MIN_VALUES[field];
1612
}
1613
1614
/**
1615
* Returns the lowest maximum value for the given calendar field
1616
* of this <code>GregorianCalendar</code> instance. The lowest
1617
* maximum value is defined as the smallest value returned by
1618
* {@link #getActualMaximum(int)} for any possible time value,
1619
* taking into consideration the current values of the
1620
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1621
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1622
* {@link #getGregorianChange() getGregorianChange} and
1623
* {@link Calendar#getTimeZone() getTimeZone} methods.
1624
*
1625
* @param field the calendar field
1626
* @return the lowest maximum value for the given calendar field.
1627
* @see #getMinimum(int)
1628
* @see #getMaximum(int)
1629
* @see #getGreatestMinimum(int)
1630
* @see #getActualMinimum(int)
1631
* @see #getActualMaximum(int)
1632
*/
1633
@Override
1634
public int getLeastMaximum(int field) {
1635
switch (field) {
1636
case MONTH:
1637
case DAY_OF_MONTH:
1638
case DAY_OF_YEAR:
1639
case WEEK_OF_YEAR:
1640
case WEEK_OF_MONTH:
1641
case DAY_OF_WEEK_IN_MONTH:
1642
case YEAR:
1643
{
1644
GregorianCalendar gc = (GregorianCalendar) clone();
1645
gc.setLenient(true);
1646
gc.setTimeInMillis(gregorianCutover);
1647
int v1 = gc.getActualMaximum(field);
1648
gc.setTimeInMillis(gregorianCutover-1);
1649
int v2 = gc.getActualMaximum(field);
1650
return Math.min(LEAST_MAX_VALUES[field], Math.min(v1, v2));
1651
}
1652
}
1653
return LEAST_MAX_VALUES[field];
1654
}
1655
1656
/**
1657
* Returns the minimum value that this calendar field could have,
1658
* taking into consideration the given time value and the current
1659
* values of the
1660
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1661
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1662
* {@link #getGregorianChange() getGregorianChange} and
1663
* {@link Calendar#getTimeZone() getTimeZone} methods.
1664
*
1665
* <p>For example, if the Gregorian change date is January 10,
1666
* 1970 and the date of this <code>GregorianCalendar</code> is
1667
* January 20, 1970, the actual minimum value of the
1668
* <code>DAY_OF_MONTH</code> field is 10 because the previous date
1669
* of January 10, 1970 is December 27, 1996 (in the Julian
1670
* calendar). Therefore, December 28, 1969 to January 9, 1970
1671
* don't exist.
1672
*
1673
* @param field the calendar field
1674
* @return the minimum of the given field for the time value of
1675
* this <code>GregorianCalendar</code>
1676
* @see #getMinimum(int)
1677
* @see #getMaximum(int)
1678
* @see #getGreatestMinimum(int)
1679
* @see #getLeastMaximum(int)
1680
* @see #getActualMaximum(int)
1681
* @since 1.2
1682
*/
1683
@Override
1684
public int getActualMinimum(int field) {
1685
if (field == DAY_OF_MONTH) {
1686
GregorianCalendar gc = getNormalizedCalendar();
1687
int year = gc.cdate.getNormalizedYear();
1688
if (year == gregorianCutoverYear || year == gregorianCutoverYearJulian) {
1689
long month1 = getFixedDateMonth1(gc.cdate, gc.calsys.getFixedDate(gc.cdate));
1690
BaseCalendar.Date d = getCalendarDate(month1);
1691
return d.getDayOfMonth();
1692
}
1693
}
1694
return getMinimum(field);
1695
}
1696
1697
/**
1698
* Returns the maximum value that this calendar field could have,
1699
* taking into consideration the given time value and the current
1700
* values of the
1701
* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1702
* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1703
* {@link #getGregorianChange() getGregorianChange} and
1704
* {@link Calendar#getTimeZone() getTimeZone} methods.
1705
* For example, if the date of this instance is February 1, 2004,
1706
* the actual maximum value of the <code>DAY_OF_MONTH</code> field
1707
* is 29 because 2004 is a leap year, and if the date of this
1708
* instance is February 1, 2005, it's 28.
1709
*
1710
* <p>This method calculates the maximum value of {@link
1711
* Calendar#WEEK_OF_YEAR WEEK_OF_YEAR} based on the {@link
1712
* Calendar#YEAR YEAR} (calendar year) value, not the <a
1713
* href="#week_year">week year</a>. Call {@link
1714
* #getWeeksInWeekYear()} to get the maximum value of {@code
1715
* WEEK_OF_YEAR} in the week year of this {@code GregorianCalendar}.
1716
*
1717
* @param field the calendar field
1718
* @return the maximum of the given field for the time value of
1719
* this <code>GregorianCalendar</code>
1720
* @see #getMinimum(int)
1721
* @see #getMaximum(int)
1722
* @see #getGreatestMinimum(int)
1723
* @see #getLeastMaximum(int)
1724
* @see #getActualMinimum(int)
1725
* @since 1.2
1726
*/
1727
@Override
1728
public int getActualMaximum(int field) {
1729
final int fieldsForFixedMax = ERA_MASK|DAY_OF_WEEK_MASK|HOUR_MASK|AM_PM_MASK|
1730
HOUR_OF_DAY_MASK|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK|
1731
ZONE_OFFSET_MASK|DST_OFFSET_MASK;
1732
if ((fieldsForFixedMax & (1<<field)) != 0) {
1733
return getMaximum(field);
1734
}
1735
1736
GregorianCalendar gc = getNormalizedCalendar();
1737
BaseCalendar.Date date = gc.cdate;
1738
BaseCalendar cal = gc.calsys;
1739
int normalizedYear = date.getNormalizedYear();
1740
1741
int value = -1;
1742
switch (field) {
1743
case MONTH:
1744
{
1745
if (!gc.isCutoverYear(normalizedYear)) {
1746
value = DECEMBER;
1747
break;
1748
}
1749
1750
// January 1 of the next year may or may not exist.
1751
long nextJan1;
1752
do {
1753
nextJan1 = gcal.getFixedDate(++normalizedYear, BaseCalendar.JANUARY, 1, null);
1754
} while (nextJan1 < gregorianCutoverDate);
1755
BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
1756
cal.getCalendarDateFromFixedDate(d, nextJan1 - 1);
1757
value = d.getMonth() - 1;
1758
}
1759
break;
1760
1761
case DAY_OF_MONTH:
1762
{
1763
value = cal.getMonthLength(date);
1764
if (!gc.isCutoverYear(normalizedYear) || date.getDayOfMonth() == value) {
1765
break;
1766
}
1767
1768
// Handle cutover year.
1769
long fd = gc.getCurrentFixedDate();
1770
if (fd >= gregorianCutoverDate) {
1771
break;
1772
}
1773
int monthLength = gc.actualMonthLength();
1774
long monthEnd = gc.getFixedDateMonth1(gc.cdate, fd) + monthLength - 1;
1775
// Convert the fixed date to its calendar date.
1776
BaseCalendar.Date d = gc.getCalendarDate(monthEnd);
1777
value = d.getDayOfMonth();
1778
}
1779
break;
1780
1781
case DAY_OF_YEAR:
1782
{
1783
if (!gc.isCutoverYear(normalizedYear)) {
1784
value = cal.getYearLength(date);
1785
break;
1786
}
1787
1788
// Handle cutover year.
1789
long jan1;
1790
if (gregorianCutoverYear == gregorianCutoverYearJulian) {
1791
BaseCalendar cocal = gc.getCutoverCalendarSystem();
1792
jan1 = cocal.getFixedDate(normalizedYear, 1, 1, null);
1793
} else if (normalizedYear == gregorianCutoverYearJulian) {
1794
jan1 = cal.getFixedDate(normalizedYear, 1, 1, null);
1795
} else {
1796
jan1 = gregorianCutoverDate;
1797
}
1798
// January 1 of the next year may or may not exist.
1799
long nextJan1 = gcal.getFixedDate(++normalizedYear, 1, 1, null);
1800
if (nextJan1 < gregorianCutoverDate) {
1801
nextJan1 = gregorianCutoverDate;
1802
}
1803
assert jan1 <= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(),
1804
date.getDayOfMonth(), date);
1805
assert nextJan1 >= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(),
1806
date.getDayOfMonth(), date);
1807
value = (int)(nextJan1 - jan1);
1808
}
1809
break;
1810
1811
case WEEK_OF_YEAR:
1812
{
1813
if (!gc.isCutoverYear(normalizedYear)) {
1814
// Get the day of week of January 1 of the year
1815
CalendarDate d = cal.newCalendarDate(TimeZone.NO_TIMEZONE);
1816
d.setDate(date.getYear(), BaseCalendar.JANUARY, 1);
1817
int dayOfWeek = cal.getDayOfWeek(d);
1818
// Normalize the day of week with the firstDayOfWeek value
1819
dayOfWeek -= getFirstDayOfWeek();
1820
if (dayOfWeek < 0) {
1821
dayOfWeek += 7;
1822
}
1823
value = 52;
1824
int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1;
1825
if ((magic == 6) ||
1826
(date.isLeapYear() && (magic == 5 || magic == 12))) {
1827
value++;
1828
}
1829
break;
1830
}
1831
1832
if (gc == this) {
1833
gc = (GregorianCalendar) gc.clone();
1834
}
1835
int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);
1836
gc.set(DAY_OF_YEAR, maxDayOfYear);
1837
value = gc.get(WEEK_OF_YEAR);
1838
if (internalGet(YEAR) != gc.getWeekYear()) {
1839
gc.set(DAY_OF_YEAR, maxDayOfYear - 7);
1840
value = gc.get(WEEK_OF_YEAR);
1841
}
1842
}
1843
break;
1844
1845
case WEEK_OF_MONTH:
1846
{
1847
if (!gc.isCutoverYear(normalizedYear)) {
1848
CalendarDate d = cal.newCalendarDate(null);
1849
d.setDate(date.getYear(), date.getMonth(), 1);
1850
int dayOfWeek = cal.getDayOfWeek(d);
1851
int monthLength = cal.getMonthLength(d);
1852
dayOfWeek -= getFirstDayOfWeek();
1853
if (dayOfWeek < 0) {
1854
dayOfWeek += 7;
1855
}
1856
int nDaysFirstWeek = 7 - dayOfWeek; // # of days in the first week
1857
value = 3;
1858
if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) {
1859
value++;
1860
}
1861
monthLength -= nDaysFirstWeek + 7 * 3;
1862
if (monthLength > 0) {
1863
value++;
1864
if (monthLength > 7) {
1865
value++;
1866
}
1867
}
1868
break;
1869
}
1870
1871
// Cutover year handling
1872
if (gc == this) {
1873
gc = (GregorianCalendar) gc.clone();
1874
}
1875
int y = gc.internalGet(YEAR);
1876
int m = gc.internalGet(MONTH);
1877
do {
1878
value = gc.get(WEEK_OF_MONTH);
1879
gc.add(WEEK_OF_MONTH, +1);
1880
} while (gc.get(YEAR) == y && gc.get(MONTH) == m);
1881
}
1882
break;
1883
1884
case DAY_OF_WEEK_IN_MONTH:
1885
{
1886
// may be in the Gregorian cutover month
1887
int ndays, dow1;
1888
int dow = date.getDayOfWeek();
1889
if (!gc.isCutoverYear(normalizedYear)) {
1890
BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
1891
ndays = cal.getMonthLength(d);
1892
d.setDayOfMonth(1);
1893
cal.normalize(d);
1894
dow1 = d.getDayOfWeek();
1895
} else {
1896
// Let a cloned GregorianCalendar take care of the cutover cases.
1897
if (gc == this) {
1898
gc = (GregorianCalendar) clone();
1899
}
1900
ndays = gc.actualMonthLength();
1901
gc.set(DAY_OF_MONTH, gc.getActualMinimum(DAY_OF_MONTH));
1902
dow1 = gc.get(DAY_OF_WEEK);
1903
}
1904
int x = dow - dow1;
1905
if (x < 0) {
1906
x += 7;
1907
}
1908
ndays -= x;
1909
value = (ndays + 6) / 7;
1910
}
1911
break;
1912
1913
case YEAR:
1914
/* The year computation is no different, in principle, from the
1915
* others, however, the range of possible maxima is large. In
1916
* addition, the way we know we've exceeded the range is different.
1917
* For these reasons, we use the special case code below to handle
1918
* this field.
1919
*
1920
* The actual maxima for YEAR depend on the type of calendar:
1921
*
1922
* Gregorian = May 17, 292275056 BCE - Aug 17, 292278994 CE
1923
* Julian = Dec 2, 292269055 BCE - Jan 3, 292272993 CE
1924
* Hybrid = Dec 2, 292269055 BCE - Aug 17, 292278994 CE
1925
*
1926
* We know we've exceeded the maximum when either the month, date,
1927
* time, or era changes in response to setting the year. We don't
1928
* check for month, date, and time here because the year and era are
1929
* sufficient to detect an invalid year setting. NOTE: If code is
1930
* added to check the month and date in the future for some reason,
1931
* Feb 29 must be allowed to shift to Mar 1 when setting the year.
1932
*/
1933
{
1934
if (gc == this) {
1935
gc = (GregorianCalendar) clone();
1936
}
1937
1938
// Calculate the millisecond offset from the beginning
1939
// of the year of this calendar and adjust the max
1940
// year value if we are beyond the limit in the max
1941
// year.
1942
long current = gc.getYearOffsetInMillis();
1943
1944
if (gc.internalGetEra() == CE) {
1945
gc.setTimeInMillis(Long.MAX_VALUE);
1946
value = gc.get(YEAR);
1947
long maxEnd = gc.getYearOffsetInMillis();
1948
if (current > maxEnd) {
1949
value--;
1950
}
1951
} else {
1952
CalendarSystem mincal = gc.getTimeInMillis() >= gregorianCutover ?
1953
gcal : getJulianCalendarSystem();
1954
CalendarDate d = mincal.getCalendarDate(Long.MIN_VALUE, getZone());
1955
long maxEnd = (cal.getDayOfYear(d) - 1) * 24 + d.getHours();
1956
maxEnd *= 60;
1957
maxEnd += d.getMinutes();
1958
maxEnd *= 60;
1959
maxEnd += d.getSeconds();
1960
maxEnd *= 1000;
1961
maxEnd += d.getMillis();
1962
value = d.getYear();
1963
if (value <= 0) {
1964
assert mincal == gcal;
1965
value = 1 - value;
1966
}
1967
if (current < maxEnd) {
1968
value--;
1969
}
1970
}
1971
}
1972
break;
1973
1974
default:
1975
throw new ArrayIndexOutOfBoundsException(field);
1976
}
1977
return value;
1978
}
1979
1980
/**
1981
* Returns the millisecond offset from the beginning of this
1982
* year. This Calendar object must have been normalized.
1983
*/
1984
private long getYearOffsetInMillis() {
1985
long t = (internalGet(DAY_OF_YEAR) - 1) * 24;
1986
t += internalGet(HOUR_OF_DAY);
1987
t *= 60;
1988
t += internalGet(MINUTE);
1989
t *= 60;
1990
t += internalGet(SECOND);
1991
t *= 1000;
1992
return t + internalGet(MILLISECOND) -
1993
(internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET));
1994
}
1995
1996
@Override
1997
public Object clone()
1998
{
1999
GregorianCalendar other = (GregorianCalendar) super.clone();
2000
2001
other.gdate = (BaseCalendar.Date) gdate.clone();
2002
if (cdate != null) {
2003
if (cdate != gdate) {
2004
other.cdate = (BaseCalendar.Date) cdate.clone();
2005
} else {
2006
other.cdate = other.gdate;
2007
}
2008
}
2009
other.originalFields = null;
2010
other.zoneOffsets = null;
2011
return other;
2012
}
2013
2014
@Override
2015
public TimeZone getTimeZone() {
2016
TimeZone zone = super.getTimeZone();
2017
// To share the zone by CalendarDates
2018
gdate.setZone(zone);
2019
if (cdate != null && cdate != gdate) {
2020
cdate.setZone(zone);
2021
}
2022
return zone;
2023
}
2024
2025
@Override
2026
public void setTimeZone(TimeZone zone) {
2027
super.setTimeZone(zone);
2028
// To share the zone by CalendarDates
2029
gdate.setZone(zone);
2030
if (cdate != null && cdate != gdate) {
2031
cdate.setZone(zone);
2032
}
2033
}
2034
2035
/**
2036
* Returns {@code true} indicating this {@code GregorianCalendar}
2037
* supports week dates.
2038
*
2039
* @return {@code true} (always)
2040
* @see #getWeekYear()
2041
* @see #setWeekDate(int,int,int)
2042
* @see #getWeeksInWeekYear()
2043
* @since 1.7
2044
*/
2045
@Override
2046
public final boolean isWeekDateSupported() {
2047
return true;
2048
}
2049
2050
/**
2051
* Returns the <a href="#week_year">week year</a> represented by this
2052
* {@code GregorianCalendar}. The dates in the weeks between 1 and the
2053
* maximum week number of the week year have the same week year value
2054
* that may be one year before or after the {@link Calendar#YEAR YEAR}
2055
* (calendar year) value.
2056
*
2057
* <p>This method calls {@link Calendar#complete()} before
2058
* calculating the week year.
2059
*
2060
* @return the week year represented by this {@code GregorianCalendar}.
2061
* If the {@link Calendar#ERA ERA} value is {@link #BC}, the year is
2062
* represented by 0 or a negative number: BC 1 is 0, BC 2
2063
* is -1, BC 3 is -2, and so on.
2064
* @throws IllegalArgumentException
2065
* if any of the calendar fields is invalid in non-lenient mode.
2066
* @see #isWeekDateSupported()
2067
* @see #getWeeksInWeekYear()
2068
* @see Calendar#getFirstDayOfWeek()
2069
* @see Calendar#getMinimalDaysInFirstWeek()
2070
* @since 1.7
2071
*/
2072
@Override
2073
public int getWeekYear() {
2074
int year = get(YEAR); // implicitly calls complete()
2075
if (internalGetEra() == BCE) {
2076
year = 1 - year;
2077
}
2078
2079
// Fast path for the Gregorian calendar years that are never
2080
// affected by the Julian-Gregorian transition
2081
if (year > gregorianCutoverYear + 1) {
2082
int weekOfYear = internalGet(WEEK_OF_YEAR);
2083
if (internalGet(MONTH) == JANUARY) {
2084
if (weekOfYear >= 52) {
2085
--year;
2086
}
2087
} else {
2088
if (weekOfYear == 1) {
2089
++year;
2090
}
2091
}
2092
return year;
2093
}
2094
2095
// General (slow) path
2096
int dayOfYear = internalGet(DAY_OF_YEAR);
2097
int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);
2098
int minimalDays = getMinimalDaysInFirstWeek();
2099
2100
// Quickly check the possibility of year adjustments before
2101
// cloning this GregorianCalendar.
2102
if (dayOfYear > minimalDays && dayOfYear < (maxDayOfYear - 6)) {
2103
return year;
2104
}
2105
2106
// Create a clone to work on the calculation
2107
GregorianCalendar cal = (GregorianCalendar) clone();
2108
cal.setLenient(true);
2109
// Use GMT so that intermediate date calculations won't
2110
// affect the time of day fields.
2111
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
2112
// Go to the first day of the year, which is usually January 1.
2113
cal.set(DAY_OF_YEAR, 1);
2114
cal.complete();
2115
2116
// Get the first day of the first day-of-week in the year.
2117
int delta = getFirstDayOfWeek() - cal.get(DAY_OF_WEEK);
2118
if (delta != 0) {
2119
if (delta < 0) {
2120
delta += 7;
2121
}
2122
cal.add(DAY_OF_YEAR, delta);
2123
}
2124
int minDayOfYear = cal.get(DAY_OF_YEAR);
2125
if (dayOfYear < minDayOfYear) {
2126
if (minDayOfYear <= minimalDays) {
2127
--year;
2128
}
2129
} else {
2130
cal.set(YEAR, year + 1);
2131
cal.set(DAY_OF_YEAR, 1);
2132
cal.complete();
2133
int del = getFirstDayOfWeek() - cal.get(DAY_OF_WEEK);
2134
if (del != 0) {
2135
if (del < 0) {
2136
del += 7;
2137
}
2138
cal.add(DAY_OF_YEAR, del);
2139
}
2140
minDayOfYear = cal.get(DAY_OF_YEAR) - 1;
2141
if (minDayOfYear == 0) {
2142
minDayOfYear = 7;
2143
}
2144
if (minDayOfYear >= minimalDays) {
2145
int days = maxDayOfYear - dayOfYear + 1;
2146
if (days <= (7 - minDayOfYear)) {
2147
++year;
2148
}
2149
}
2150
}
2151
return year;
2152
}
2153
2154
/**
2155
* Sets this {@code GregorianCalendar} to the date given by the
2156
* date specifiers - <a href="#week_year">{@code weekYear}</a>,
2157
* {@code weekOfYear}, and {@code dayOfWeek}. {@code weekOfYear}
2158
* follows the <a href="#week_and_year">{@code WEEK_OF_YEAR}
2159
* numbering</a>. The {@code dayOfWeek} value must be one of the
2160
* {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} values: {@link
2161
* Calendar#SUNDAY SUNDAY} to {@link Calendar#SATURDAY SATURDAY}.
2162
*
2163
* <p>Note that the numeric day-of-week representation differs from
2164
* the ISO 8601 standard, and that the {@code weekOfYear}
2165
* numbering is compatible with the standard when {@code
2166
* getFirstDayOfWeek()} is {@code MONDAY} and {@code
2167
* getMinimalDaysInFirstWeek()} is 4.
2168
*
2169
* <p>Unlike the {@code set} method, all of the calendar fields
2170
* and the instant of time value are calculated upon return.
2171
*
2172
* <p>If {@code weekOfYear} is out of the valid week-of-year
2173
* range in {@code weekYear}, the {@code weekYear}
2174
* and {@code weekOfYear} values are adjusted in lenient
2175
* mode, or an {@code IllegalArgumentException} is thrown in
2176
* non-lenient mode.
2177
*
2178
* @param weekYear the week year
2179
* @param weekOfYear the week number based on {@code weekYear}
2180
* @param dayOfWeek the day of week value: one of the constants
2181
* for the {@link #DAY_OF_WEEK DAY_OF_WEEK} field:
2182
* {@link Calendar#SUNDAY SUNDAY}, ...,
2183
* {@link Calendar#SATURDAY SATURDAY}.
2184
* @exception IllegalArgumentException
2185
* if any of the given date specifiers is invalid,
2186
* or if any of the calendar fields are inconsistent
2187
* with the given date specifiers in non-lenient mode
2188
* @see GregorianCalendar#isWeekDateSupported()
2189
* @see Calendar#getFirstDayOfWeek()
2190
* @see Calendar#getMinimalDaysInFirstWeek()
2191
* @since 1.7
2192
*/
2193
@Override
2194
public void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek) {
2195
if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) {
2196
throw new IllegalArgumentException("invalid dayOfWeek: " + dayOfWeek);
2197
}
2198
2199
// To avoid changing the time of day fields by date
2200
// calculations, use a clone with the GMT time zone.
2201
GregorianCalendar gc = (GregorianCalendar) clone();
2202
gc.setLenient(true);
2203
int era = gc.get(ERA);
2204
gc.clear();
2205
gc.setTimeZone(TimeZone.getTimeZone("GMT"));
2206
gc.set(ERA, era);
2207
gc.set(YEAR, weekYear);
2208
gc.set(WEEK_OF_YEAR, 1);
2209
gc.set(DAY_OF_WEEK, getFirstDayOfWeek());
2210
int days = dayOfWeek - getFirstDayOfWeek();
2211
if (days < 0) {
2212
days += 7;
2213
}
2214
days += 7 * (weekOfYear - 1);
2215
if (days != 0) {
2216
gc.add(DAY_OF_YEAR, days);
2217
} else {
2218
gc.complete();
2219
}
2220
2221
if (!isLenient() &&
2222
(gc.getWeekYear() != weekYear
2223
|| gc.internalGet(WEEK_OF_YEAR) != weekOfYear
2224
|| gc.internalGet(DAY_OF_WEEK) != dayOfWeek)) {
2225
throw new IllegalArgumentException();
2226
}
2227
2228
set(ERA, gc.internalGet(ERA));
2229
set(YEAR, gc.internalGet(YEAR));
2230
set(MONTH, gc.internalGet(MONTH));
2231
set(DAY_OF_MONTH, gc.internalGet(DAY_OF_MONTH));
2232
2233
// to avoid throwing an IllegalArgumentException in
2234
// non-lenient, set WEEK_OF_YEAR internally
2235
internalSet(WEEK_OF_YEAR, weekOfYear);
2236
complete();
2237
}
2238
2239
/**
2240
* Returns the number of weeks in the <a href="#week_year">week year</a>
2241
* represented by this {@code GregorianCalendar}.
2242
*
2243
* <p>For example, if this {@code GregorianCalendar}'s date is
2244
* December 31, 2008 with <a href="#iso8601_compatible_setting">the ISO
2245
* 8601 compatible setting</a>, this method will return 53 for the
2246
* period: December 29, 2008 to January 3, 2010 while {@link
2247
* #getActualMaximum(int) getActualMaximum(WEEK_OF_YEAR)} will return
2248
* 52 for the period: December 31, 2007 to December 28, 2008.
2249
*
2250
* @return the number of weeks in the week year.
2251
* @see Calendar#WEEK_OF_YEAR
2252
* @see #getWeekYear()
2253
* @see #getActualMaximum(int)
2254
* @since 1.7
2255
*/
2256
@Override
2257
public int getWeeksInWeekYear() {
2258
GregorianCalendar gc = getNormalizedCalendar();
2259
int weekYear = gc.getWeekYear();
2260
if (weekYear == gc.internalGet(YEAR)) {
2261
return gc.getActualMaximum(WEEK_OF_YEAR);
2262
}
2263
2264
// Use the 2nd week for calculating the max of WEEK_OF_YEAR
2265
if (gc == this) {
2266
gc = (GregorianCalendar) gc.clone();
2267
}
2268
gc.setWeekDate(weekYear, 2, internalGet(DAY_OF_WEEK));
2269
return gc.getActualMaximum(WEEK_OF_YEAR);
2270
}
2271
2272
/////////////////////////////
2273
// Time => Fields computation
2274
/////////////////////////////
2275
2276
/**
2277
* The fixed date corresponding to gdate. If the value is
2278
* Long.MIN_VALUE, the fixed date value is unknown. Currently,
2279
* Julian calendar dates are not cached.
2280
*/
2281
transient private long cachedFixedDate = Long.MIN_VALUE;
2282
2283
/**
2284
* Converts the time value (millisecond offset from the <a
2285
* href="Calendar.html#Epoch">Epoch</a>) to calendar field values.
2286
* The time is <em>not</em>
2287
* recomputed first; to recompute the time, then the fields, call the
2288
* <code>complete</code> method.
2289
*
2290
* @see Calendar#complete
2291
*/
2292
@Override
2293
protected void computeFields() {
2294
int mask;
2295
if (isPartiallyNormalized()) {
2296
// Determine which calendar fields need to be computed.
2297
mask = getSetStateFields();
2298
int fieldMask = ~mask & ALL_FIELDS;
2299
// We have to call computTime in case calsys == null in
2300
// order to set calsys and cdate. (6263644)
2301
if (fieldMask != 0 || calsys == null) {
2302
mask |= computeFields(fieldMask,
2303
mask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK));
2304
assert mask == ALL_FIELDS;
2305
}
2306
} else {
2307
mask = ALL_FIELDS;
2308
computeFields(mask, 0);
2309
}
2310
// After computing all the fields, set the field state to `COMPUTED'.
2311
setFieldsComputed(mask);
2312
}
2313
2314
/**
2315
* This computeFields implements the conversion from UTC
2316
* (millisecond offset from the Epoch) to calendar
2317
* field values. fieldMask specifies which fields to change the
2318
* setting state to COMPUTED, although all fields are set to
2319
* the correct values. This is required to fix 4685354.
2320
*
2321
* @param fieldMask a bit mask to specify which fields to change
2322
* the setting state.
2323
* @param tzMask a bit mask to specify which time zone offset
2324
* fields to be used for time calculations
2325
* @return a new field mask that indicates what field values have
2326
* actually been set.
2327
*/
2328
private int computeFields(int fieldMask, int tzMask) {
2329
int zoneOffset = 0;
2330
TimeZone tz = getZone();
2331
if (zoneOffsets == null) {
2332
zoneOffsets = new int[2];
2333
}
2334
if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {
2335
if (tz instanceof ZoneInfo) {
2336
zoneOffset = ((ZoneInfo)tz).getOffsets(time, zoneOffsets);
2337
} else {
2338
zoneOffset = tz.getOffset(time);
2339
zoneOffsets[0] = tz.getRawOffset();
2340
zoneOffsets[1] = zoneOffset - zoneOffsets[0];
2341
}
2342
}
2343
if (tzMask != 0) {
2344
if (isFieldSet(tzMask, ZONE_OFFSET)) {
2345
zoneOffsets[0] = internalGet(ZONE_OFFSET);
2346
}
2347
if (isFieldSet(tzMask, DST_OFFSET)) {
2348
zoneOffsets[1] = internalGet(DST_OFFSET);
2349
}
2350
zoneOffset = zoneOffsets[0] + zoneOffsets[1];
2351
}
2352
2353
// By computing time and zoneOffset separately, we can take
2354
// the wider range of time+zoneOffset than the previous
2355
// implementation.
2356
long fixedDate = zoneOffset / ONE_DAY;
2357
int timeOfDay = zoneOffset % (int)ONE_DAY;
2358
fixedDate += time / ONE_DAY;
2359
timeOfDay += (int) (time % ONE_DAY);
2360
if (timeOfDay >= ONE_DAY) {
2361
timeOfDay -= ONE_DAY;
2362
++fixedDate;
2363
} else {
2364
while (timeOfDay < 0) {
2365
timeOfDay += ONE_DAY;
2366
--fixedDate;
2367
}
2368
}
2369
fixedDate += EPOCH_OFFSET;
2370
2371
int era = CE;
2372
int year;
2373
if (fixedDate >= gregorianCutoverDate) {
2374
// Handle Gregorian dates.
2375
assert cachedFixedDate == Long.MIN_VALUE || gdate.isNormalized()
2376
: "cache control: not normalized";
2377
assert cachedFixedDate == Long.MIN_VALUE ||
2378
gcal.getFixedDate(gdate.getNormalizedYear(),
2379
gdate.getMonth(),
2380
gdate.getDayOfMonth(), gdate)
2381
== cachedFixedDate
2382
: "cache control: inconsictency" +
2383
", cachedFixedDate=" + cachedFixedDate +
2384
", computed=" +
2385
gcal.getFixedDate(gdate.getNormalizedYear(),
2386
gdate.getMonth(),
2387
gdate.getDayOfMonth(),
2388
gdate) +
2389
", date=" + gdate;
2390
2391
// See if we can use gdate to avoid date calculation.
2392
if (fixedDate != cachedFixedDate) {
2393
gcal.getCalendarDateFromFixedDate(gdate, fixedDate);
2394
cachedFixedDate = fixedDate;
2395
}
2396
2397
year = gdate.getYear();
2398
if (year <= 0) {
2399
year = 1 - year;
2400
era = BCE;
2401
}
2402
calsys = gcal;
2403
cdate = gdate;
2404
assert cdate.getDayOfWeek() > 0 : "dow="+cdate.getDayOfWeek()+", date="+cdate;
2405
} else {
2406
// Handle Julian calendar dates.
2407
calsys = getJulianCalendarSystem();
2408
cdate = (BaseCalendar.Date) jcal.newCalendarDate(getZone());
2409
jcal.getCalendarDateFromFixedDate(cdate, fixedDate);
2410
Era e = cdate.getEra();
2411
if (e == jeras[0]) {
2412
era = BCE;
2413
}
2414
year = cdate.getYear();
2415
}
2416
2417
// Always set the ERA and YEAR values.
2418
internalSet(ERA, era);
2419
internalSet(YEAR, year);
2420
int mask = fieldMask | (ERA_MASK|YEAR_MASK);
2421
2422
int month = cdate.getMonth() - 1; // 0-based
2423
int dayOfMonth = cdate.getDayOfMonth();
2424
2425
// Set the basic date fields.
2426
if ((fieldMask & (MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK))
2427
!= 0) {
2428
internalSet(MONTH, month);
2429
internalSet(DAY_OF_MONTH, dayOfMonth);
2430
internalSet(DAY_OF_WEEK, cdate.getDayOfWeek());
2431
mask |= MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK;
2432
}
2433
2434
if ((fieldMask & (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK
2435
|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK)) != 0) {
2436
if (timeOfDay != 0) {
2437
int hours = timeOfDay / ONE_HOUR;
2438
internalSet(HOUR_OF_DAY, hours);
2439
internalSet(AM_PM, hours / 12); // Assume AM == 0
2440
internalSet(HOUR, hours % 12);
2441
int r = timeOfDay % ONE_HOUR;
2442
internalSet(MINUTE, r / ONE_MINUTE);
2443
r %= ONE_MINUTE;
2444
internalSet(SECOND, r / ONE_SECOND);
2445
internalSet(MILLISECOND, r % ONE_SECOND);
2446
} else {
2447
internalSet(HOUR_OF_DAY, 0);
2448
internalSet(AM_PM, AM);
2449
internalSet(HOUR, 0);
2450
internalSet(MINUTE, 0);
2451
internalSet(SECOND, 0);
2452
internalSet(MILLISECOND, 0);
2453
}
2454
mask |= (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK
2455
|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK);
2456
}
2457
2458
if ((fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) != 0) {
2459
internalSet(ZONE_OFFSET, zoneOffsets[0]);
2460
internalSet(DST_OFFSET, zoneOffsets[1]);
2461
mask |= (ZONE_OFFSET_MASK|DST_OFFSET_MASK);
2462
}
2463
2464
if ((fieldMask & (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK)) != 0) {
2465
int normalizedYear = cdate.getNormalizedYear();
2466
long fixedDateJan1 = calsys.getFixedDate(normalizedYear, 1, 1, cdate);
2467
int dayOfYear = (int)(fixedDate - fixedDateJan1) + 1;
2468
long fixedDateMonth1 = fixedDate - dayOfMonth + 1;
2469
int cutoverGap = 0;
2470
int cutoverYear = (calsys == gcal) ? gregorianCutoverYear : gregorianCutoverYearJulian;
2471
int relativeDayOfMonth = dayOfMonth - 1;
2472
2473
// If we are in the cutover year, we need some special handling.
2474
if (normalizedYear == cutoverYear) {
2475
// Need to take care of the "missing" days.
2476
if (gregorianCutoverYearJulian <= gregorianCutoverYear) {
2477
// We need to find out where we are. The cutover
2478
// gap could even be more than one year. (One
2479
// year difference in ~48667 years.)
2480
fixedDateJan1 = getFixedDateJan1(cdate, fixedDate);
2481
if (fixedDate >= gregorianCutoverDate) {
2482
fixedDateMonth1 = getFixedDateMonth1(cdate, fixedDate);
2483
}
2484
}
2485
int realDayOfYear = (int)(fixedDate - fixedDateJan1) + 1;
2486
cutoverGap = dayOfYear - realDayOfYear;
2487
dayOfYear = realDayOfYear;
2488
relativeDayOfMonth = (int)(fixedDate - fixedDateMonth1);
2489
}
2490
internalSet(DAY_OF_YEAR, dayOfYear);
2491
internalSet(DAY_OF_WEEK_IN_MONTH, relativeDayOfMonth / 7 + 1);
2492
2493
int weekOfYear = getWeekNumber(fixedDateJan1, fixedDate);
2494
2495
// The spec is to calculate WEEK_OF_YEAR in the
2496
// ISO8601-style. This creates problems, though.
2497
if (weekOfYear == 0) {
2498
// If the date belongs to the last week of the
2499
// previous year, use the week number of "12/31" of
2500
// the "previous" year. Again, if the previous year is
2501
// the Gregorian cutover year, we need to take care of
2502
// it. Usually the previous day of January 1 is
2503
// December 31, which is not always true in
2504
// GregorianCalendar.
2505
long fixedDec31 = fixedDateJan1 - 1;
2506
long prevJan1 = fixedDateJan1 - 365;
2507
if (normalizedYear > (cutoverYear + 1)) {
2508
if (CalendarUtils.isGregorianLeapYear(normalizedYear - 1)) {
2509
--prevJan1;
2510
}
2511
} else if (normalizedYear <= gregorianCutoverYearJulian) {
2512
if (CalendarUtils.isJulianLeapYear(normalizedYear - 1)) {
2513
--prevJan1;
2514
}
2515
} else {
2516
BaseCalendar calForJan1 = calsys;
2517
//int prevYear = normalizedYear - 1;
2518
int prevYear = getCalendarDate(fixedDec31).getNormalizedYear();
2519
if (prevYear == gregorianCutoverYear) {
2520
calForJan1 = getCutoverCalendarSystem();
2521
if (calForJan1 == jcal) {
2522
prevJan1 = calForJan1.getFixedDate(prevYear,
2523
BaseCalendar.JANUARY,
2524
1,
2525
null);
2526
} else {
2527
prevJan1 = gregorianCutoverDate;
2528
calForJan1 = gcal;
2529
}
2530
} else if (prevYear <= gregorianCutoverYearJulian) {
2531
calForJan1 = getJulianCalendarSystem();
2532
prevJan1 = calForJan1.getFixedDate(prevYear,
2533
BaseCalendar.JANUARY,
2534
1,
2535
null);
2536
}
2537
}
2538
weekOfYear = getWeekNumber(prevJan1, fixedDec31);
2539
} else {
2540
if (normalizedYear > gregorianCutoverYear ||
2541
normalizedYear < (gregorianCutoverYearJulian - 1)) {
2542
// Regular years
2543
if (weekOfYear >= 52) {
2544
long nextJan1 = fixedDateJan1 + 365;
2545
if (cdate.isLeapYear()) {
2546
nextJan1++;
2547
}
2548
long nextJan1st = BaseCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
2549
getFirstDayOfWeek());
2550
int ndays = (int)(nextJan1st - nextJan1);
2551
if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
2552
// The first days forms a week in which the date is included.
2553
weekOfYear = 1;
2554
}
2555
}
2556
} else {
2557
BaseCalendar calForJan1 = calsys;
2558
int nextYear = normalizedYear + 1;
2559
if (nextYear == (gregorianCutoverYearJulian + 1) &&
2560
nextYear < gregorianCutoverYear) {
2561
// In case the gap is more than one year.
2562
nextYear = gregorianCutoverYear;
2563
}
2564
if (nextYear == gregorianCutoverYear) {
2565
calForJan1 = getCutoverCalendarSystem();
2566
}
2567
2568
long nextJan1;
2569
if (nextYear > gregorianCutoverYear
2570
|| gregorianCutoverYearJulian == gregorianCutoverYear
2571
|| nextYear == gregorianCutoverYearJulian) {
2572
nextJan1 = calForJan1.getFixedDate(nextYear,
2573
BaseCalendar.JANUARY,
2574
1,
2575
null);
2576
} else {
2577
nextJan1 = gregorianCutoverDate;
2578
calForJan1 = gcal;
2579
}
2580
2581
long nextJan1st = BaseCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
2582
getFirstDayOfWeek());
2583
int ndays = (int)(nextJan1st - nextJan1);
2584
if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
2585
// The first days forms a week in which the date is included.
2586
weekOfYear = 1;
2587
}
2588
}
2589
}
2590
internalSet(WEEK_OF_YEAR, weekOfYear);
2591
internalSet(WEEK_OF_MONTH, getWeekNumber(fixedDateMonth1, fixedDate));
2592
mask |= (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK);
2593
}
2594
return mask;
2595
}
2596
2597
/**
2598
* Returns the number of weeks in a period between fixedDay1 and
2599
* fixedDate. The getFirstDayOfWeek-getMinimalDaysInFirstWeek rule
2600
* is applied to calculate the number of weeks.
2601
*
2602
* @param fixedDay1 the fixed date of the first day of the period
2603
* @param fixedDate the fixed date of the last day of the period
2604
* @return the number of weeks of the given period
2605
*/
2606
private int getWeekNumber(long fixedDay1, long fixedDate) {
2607
// We can always use `gcal' since Julian and Gregorian are the
2608
// same thing for this calculation.
2609
long fixedDay1st = Gregorian.getDayOfWeekDateOnOrBefore(fixedDay1 + 6,
2610
getFirstDayOfWeek());
2611
int ndays = (int)(fixedDay1st - fixedDay1);
2612
assert ndays <= 7;
2613
if (ndays >= getMinimalDaysInFirstWeek()) {
2614
fixedDay1st -= 7;
2615
}
2616
int normalizedDayOfPeriod = (int)(fixedDate - fixedDay1st);
2617
if (normalizedDayOfPeriod >= 0) {
2618
return normalizedDayOfPeriod / 7 + 1;
2619
}
2620
return CalendarUtils.floorDivide(normalizedDayOfPeriod, 7) + 1;
2621
}
2622
2623
/**
2624
* Converts calendar field values to the time value (millisecond
2625
* offset from the <a href="Calendar.html#Epoch">Epoch</a>).
2626
*
2627
* @exception IllegalArgumentException if any calendar fields are invalid.
2628
*/
2629
@Override
2630
protected void computeTime() {
2631
// In non-lenient mode, perform brief checking of calendar
2632
// fields which have been set externally. Through this
2633
// checking, the field values are stored in originalFields[]
2634
// to see if any of them are normalized later.
2635
if (!isLenient()) {
2636
if (originalFields == null) {
2637
originalFields = new int[FIELD_COUNT];
2638
}
2639
for (int field = 0; field < FIELD_COUNT; field++) {
2640
int value = internalGet(field);
2641
if (isExternallySet(field)) {
2642
// Quick validation for any out of range values
2643
if (value < getMinimum(field) || value > getMaximum(field)) {
2644
throw new IllegalArgumentException(getFieldName(field));
2645
}
2646
}
2647
originalFields[field] = value;
2648
}
2649
}
2650
2651
// Let the super class determine which calendar fields to be
2652
// used to calculate the time.
2653
int fieldMask = selectFields();
2654
2655
// The year defaults to the epoch start. We don't check
2656
// fieldMask for YEAR because YEAR is a mandatory field to
2657
// determine the date.
2658
int year = isSet(YEAR) ? internalGet(YEAR) : EPOCH_YEAR;
2659
2660
int era = internalGetEra();
2661
if (era == BCE) {
2662
year = 1 - year;
2663
} else if (era != CE) {
2664
// Even in lenient mode we disallow ERA values other than CE & BCE.
2665
// (The same normalization rule as add()/roll() could be
2666
// applied here in lenient mode. But this checking is kept
2667
// unchanged for compatibility as of 1.5.)
2668
throw new IllegalArgumentException("Invalid era");
2669
}
2670
2671
// If year is 0 or negative, we need to set the ERA value later.
2672
if (year <= 0 && !isSet(ERA)) {
2673
fieldMask |= ERA_MASK;
2674
setFieldsComputed(ERA_MASK);
2675
}
2676
2677
// Calculate the time of day. We rely on the convention that
2678
// an UNSET field has 0.
2679
long timeOfDay = 0;
2680
if (isFieldSet(fieldMask, HOUR_OF_DAY)) {
2681
timeOfDay += (long) internalGet(HOUR_OF_DAY);
2682
} else {
2683
timeOfDay += internalGet(HOUR);
2684
// The default value of AM_PM is 0 which designates AM.
2685
if (isFieldSet(fieldMask, AM_PM)) {
2686
timeOfDay += 12 * internalGet(AM_PM);
2687
}
2688
}
2689
timeOfDay *= 60;
2690
timeOfDay += internalGet(MINUTE);
2691
timeOfDay *= 60;
2692
timeOfDay += internalGet(SECOND);
2693
timeOfDay *= 1000;
2694
timeOfDay += internalGet(MILLISECOND);
2695
2696
// Convert the time of day to the number of days and the
2697
// millisecond offset from midnight.
2698
long fixedDate = timeOfDay / ONE_DAY;
2699
timeOfDay %= ONE_DAY;
2700
while (timeOfDay < 0) {
2701
timeOfDay += ONE_DAY;
2702
--fixedDate;
2703
}
2704
2705
// Calculate the fixed date since January 1, 1 (Gregorian).
2706
calculateFixedDate: {
2707
long gfd, jfd;
2708
if (year > gregorianCutoverYear && year > gregorianCutoverYearJulian) {
2709
gfd = fixedDate + getFixedDate(gcal, year, fieldMask);
2710
if (gfd >= gregorianCutoverDate) {
2711
fixedDate = gfd;
2712
break calculateFixedDate;
2713
}
2714
jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);
2715
} else if (year < gregorianCutoverYear && year < gregorianCutoverYearJulian) {
2716
jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);
2717
if (jfd < gregorianCutoverDate) {
2718
fixedDate = jfd;
2719
break calculateFixedDate;
2720
}
2721
gfd = jfd;
2722
} else {
2723
jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);
2724
gfd = fixedDate + getFixedDate(gcal, year, fieldMask);
2725
}
2726
2727
// Now we have to determine which calendar date it is.
2728
2729
// If the date is relative from the beginning of the year
2730
// in the Julian calendar, then use jfd;
2731
if (isFieldSet(fieldMask, DAY_OF_YEAR) || isFieldSet(fieldMask, WEEK_OF_YEAR)) {
2732
if (gregorianCutoverYear == gregorianCutoverYearJulian) {
2733
fixedDate = jfd;
2734
break calculateFixedDate;
2735
} else if (year == gregorianCutoverYear) {
2736
fixedDate = gfd;
2737
break calculateFixedDate;
2738
}
2739
}
2740
2741
if (gfd >= gregorianCutoverDate) {
2742
if (jfd >= gregorianCutoverDate) {
2743
fixedDate = gfd;
2744
} else {
2745
// The date is in an "overlapping" period. No way
2746
// to disambiguate it. Determine it using the
2747
// previous date calculation.
2748
if (calsys == gcal || calsys == null) {
2749
fixedDate = gfd;
2750
} else {
2751
fixedDate = jfd;
2752
}
2753
}
2754
} else {
2755
if (jfd < gregorianCutoverDate) {
2756
fixedDate = jfd;
2757
} else {
2758
// The date is in a "missing" period.
2759
if (!isLenient()) {
2760
throw new IllegalArgumentException("the specified date doesn't exist");
2761
}
2762
// Take the Julian date for compatibility, which
2763
// will produce a Gregorian date.
2764
fixedDate = jfd;
2765
}
2766
}
2767
}
2768
2769
// millis represents local wall-clock time in milliseconds.
2770
long millis = (fixedDate - EPOCH_OFFSET) * ONE_DAY + timeOfDay;
2771
2772
// Compute the time zone offset and DST offset. There are two potential
2773
// ambiguities here. We'll assume a 2:00 am (wall time) switchover time
2774
// for discussion purposes here.
2775
// 1. The transition into DST. Here, a designated time of 2:00 am - 2:59 am
2776
// can be in standard or in DST depending. However, 2:00 am is an invalid
2777
// representation (the representation jumps from 1:59:59 am Std to 3:00:00 am DST).
2778
// We assume standard time.
2779
// 2. The transition out of DST. Here, a designated time of 1:00 am - 1:59 am
2780
// can be in standard or DST. Both are valid representations (the rep
2781
// jumps from 1:59:59 DST to 1:00:00 Std).
2782
// Again, we assume standard time.
2783
// We use the TimeZone object, unless the user has explicitly set the ZONE_OFFSET
2784
// or DST_OFFSET fields; then we use those fields.
2785
TimeZone zone = getZone();
2786
if (zoneOffsets == null) {
2787
zoneOffsets = new int[2];
2788
}
2789
int tzMask = fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK);
2790
if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {
2791
if (zone instanceof ZoneInfo) {
2792
((ZoneInfo)zone).getOffsetsByWall(millis, zoneOffsets);
2793
} else {
2794
int gmtOffset = isFieldSet(fieldMask, ZONE_OFFSET) ?
2795
internalGet(ZONE_OFFSET) : zone.getRawOffset();
2796
zone.getOffsets(millis - gmtOffset, zoneOffsets);
2797
}
2798
}
2799
if (tzMask != 0) {
2800
if (isFieldSet(tzMask, ZONE_OFFSET)) {
2801
zoneOffsets[0] = internalGet(ZONE_OFFSET);
2802
}
2803
if (isFieldSet(tzMask, DST_OFFSET)) {
2804
zoneOffsets[1] = internalGet(DST_OFFSET);
2805
}
2806
}
2807
2808
// Adjust the time zone offset values to get the UTC time.
2809
millis -= zoneOffsets[0] + zoneOffsets[1];
2810
2811
// Set this calendar's time in milliseconds
2812
time = millis;
2813
2814
int mask = computeFields(fieldMask | getSetStateFields(), tzMask);
2815
2816
if (!isLenient()) {
2817
for (int field = 0; field < FIELD_COUNT; field++) {
2818
if (!isExternallySet(field)) {
2819
continue;
2820
}
2821
if (originalFields[field] != internalGet(field)) {
2822
String s = originalFields[field] + " -> " + internalGet(field);
2823
// Restore the original field values
2824
System.arraycopy(originalFields, 0, fields, 0, fields.length);
2825
throw new IllegalArgumentException(getFieldName(field) + ": " + s);
2826
}
2827
}
2828
}
2829
setFieldsNormalized(mask);
2830
}
2831
2832
/**
2833
* Computes the fixed date under either the Gregorian or the
2834
* Julian calendar, using the given year and the specified calendar fields.
2835
*
2836
* @param cal the CalendarSystem to be used for the date calculation
2837
* @param year the normalized year number, with 0 indicating the
2838
* year 1 BCE, -1 indicating 2 BCE, etc.
2839
* @param fieldMask the calendar fields to be used for the date calculation
2840
* @return the fixed date
2841
* @see Calendar#selectFields
2842
*/
2843
private long getFixedDate(BaseCalendar cal, int year, int fieldMask) {
2844
int month = JANUARY;
2845
if (isFieldSet(fieldMask, MONTH)) {
2846
// No need to check if MONTH has been set (no isSet(MONTH)
2847
// call) since its unset value happens to be JANUARY (0).
2848
month = internalGet(MONTH);
2849
2850
// If the month is out of range, adjust it into range
2851
if (month > DECEMBER) {
2852
year += month / 12;
2853
month %= 12;
2854
} else if (month < JANUARY) {
2855
int[] rem = new int[1];
2856
year += CalendarUtils.floorDivide(month, 12, rem);
2857
month = rem[0];
2858
}
2859
}
2860
2861
// Get the fixed date since Jan 1, 1 (Gregorian). We are on
2862
// the first day of either `month' or January in 'year'.
2863
long fixedDate = cal.getFixedDate(year, month + 1, 1,
2864
cal == gcal ? gdate : null);
2865
if (isFieldSet(fieldMask, MONTH)) {
2866
// Month-based calculations
2867
if (isFieldSet(fieldMask, DAY_OF_MONTH)) {
2868
// We are on the first day of the month. Just add the
2869
// offset if DAY_OF_MONTH is set. If the isSet call
2870
// returns false, that means DAY_OF_MONTH has been
2871
// selected just because of the selected
2872
// combination. We don't need to add any since the
2873
// default value is the 1st.
2874
if (isSet(DAY_OF_MONTH)) {
2875
// To avoid underflow with DAY_OF_MONTH-1, add
2876
// DAY_OF_MONTH, then subtract 1.
2877
fixedDate += internalGet(DAY_OF_MONTH);
2878
fixedDate--;
2879
}
2880
} else {
2881
if (isFieldSet(fieldMask, WEEK_OF_MONTH)) {
2882
long firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6,
2883
getFirstDayOfWeek());
2884
// If we have enough days in the first week, then
2885
// move to the previous week.
2886
if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {
2887
firstDayOfWeek -= 7;
2888
}
2889
if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
2890
firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,
2891
internalGet(DAY_OF_WEEK));
2892
}
2893
// In lenient mode, we treat days of the previous
2894
// months as a part of the specified
2895
// WEEK_OF_MONTH. See 4633646.
2896
fixedDate = firstDayOfWeek + 7 * (internalGet(WEEK_OF_MONTH) - 1);
2897
} else {
2898
int dayOfWeek;
2899
if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
2900
dayOfWeek = internalGet(DAY_OF_WEEK);
2901
} else {
2902
dayOfWeek = getFirstDayOfWeek();
2903
}
2904
// We are basing this on the day-of-week-in-month. The only
2905
// trickiness occurs if the day-of-week-in-month is
2906
// negative.
2907
int dowim;
2908
if (isFieldSet(fieldMask, DAY_OF_WEEK_IN_MONTH)) {
2909
dowim = internalGet(DAY_OF_WEEK_IN_MONTH);
2910
} else {
2911
dowim = 1;
2912
}
2913
if (dowim >= 0) {
2914
fixedDate = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + (7 * dowim) - 1,
2915
dayOfWeek);
2916
} else {
2917
// Go to the first day of the next week of
2918
// the specified week boundary.
2919
int lastDate = monthLength(month, year) + (7 * (dowim + 1));
2920
// Then, get the day of week date on or before the last date.
2921
fixedDate = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1,
2922
dayOfWeek);
2923
}
2924
}
2925
}
2926
} else {
2927
if (year == gregorianCutoverYear && cal == gcal
2928
&& fixedDate < gregorianCutoverDate
2929
&& gregorianCutoverYear != gregorianCutoverYearJulian) {
2930
// January 1 of the year doesn't exist. Use
2931
// gregorianCutoverDate as the first day of the
2932
// year.
2933
fixedDate = gregorianCutoverDate;
2934
}
2935
// We are on the first day of the year.
2936
if (isFieldSet(fieldMask, DAY_OF_YEAR)) {
2937
// Add the offset, then subtract 1. (Make sure to avoid underflow.)
2938
fixedDate += internalGet(DAY_OF_YEAR);
2939
fixedDate--;
2940
} else {
2941
long firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6,
2942
getFirstDayOfWeek());
2943
// If we have enough days in the first week, then move
2944
// to the previous week.
2945
if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {
2946
firstDayOfWeek -= 7;
2947
}
2948
if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
2949
int dayOfWeek = internalGet(DAY_OF_WEEK);
2950
if (dayOfWeek != getFirstDayOfWeek()) {
2951
firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,
2952
dayOfWeek);
2953
}
2954
}
2955
fixedDate = firstDayOfWeek + 7 * ((long)internalGet(WEEK_OF_YEAR) - 1);
2956
}
2957
}
2958
2959
return fixedDate;
2960
}
2961
2962
/**
2963
* Returns this object if it's normalized (all fields and time are
2964
* in sync). Otherwise, a cloned object is returned after calling
2965
* complete() in lenient mode.
2966
*/
2967
private GregorianCalendar getNormalizedCalendar() {
2968
GregorianCalendar gc;
2969
if (isFullyNormalized()) {
2970
gc = this;
2971
} else {
2972
// Create a clone and normalize the calendar fields
2973
gc = (GregorianCalendar) this.clone();
2974
gc.setLenient(true);
2975
gc.complete();
2976
}
2977
return gc;
2978
}
2979
2980
/**
2981
* Returns the Julian calendar system instance (singleton). 'jcal'
2982
* and 'jeras' are set upon the return.
2983
*/
2984
private static synchronized BaseCalendar getJulianCalendarSystem() {
2985
if (jcal == null) {
2986
jcal = (JulianCalendar) CalendarSystem.forName("julian");
2987
jeras = jcal.getEras();
2988
}
2989
return jcal;
2990
}
2991
2992
/**
2993
* Returns the calendar system for dates before the cutover date
2994
* in the cutover year. If the cutover date is January 1, the
2995
* method returns Gregorian. Otherwise, Julian.
2996
*/
2997
private BaseCalendar getCutoverCalendarSystem() {
2998
if (gregorianCutoverYearJulian < gregorianCutoverYear) {
2999
return gcal;
3000
}
3001
return getJulianCalendarSystem();
3002
}
3003
3004
/**
3005
* Determines if the specified year (normalized) is the Gregorian
3006
* cutover year. This object must have been normalized.
3007
*/
3008
private boolean isCutoverYear(int normalizedYear) {
3009
int cutoverYear = (calsys == gcal) ? gregorianCutoverYear : gregorianCutoverYearJulian;
3010
return normalizedYear == cutoverYear;
3011
}
3012
3013
/**
3014
* Returns the fixed date of the first day of the year (usually
3015
* January 1) before the specified date.
3016
*
3017
* @param date the date for which the first day of the year is
3018
* calculated. The date has to be in the cut-over year (Gregorian
3019
* or Julian).
3020
* @param fixedDate the fixed date representation of the date
3021
*/
3022
private long getFixedDateJan1(BaseCalendar.Date date, long fixedDate) {
3023
assert date.getNormalizedYear() == gregorianCutoverYear ||
3024
date.getNormalizedYear() == gregorianCutoverYearJulian;
3025
if (gregorianCutoverYear != gregorianCutoverYearJulian) {
3026
if (fixedDate >= gregorianCutoverDate) {
3027
// Dates before the cutover date don't exist
3028
// in the same (Gregorian) year. So, no
3029
// January 1 exists in the year. Use the
3030
// cutover date as the first day of the year.
3031
return gregorianCutoverDate;
3032
}
3033
}
3034
// January 1 of the normalized year should exist.
3035
BaseCalendar juliancal = getJulianCalendarSystem();
3036
return juliancal.getFixedDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1, null);
3037
}
3038
3039
/**
3040
* Returns the fixed date of the first date of the month (usually
3041
* the 1st of the month) before the specified date.
3042
*
3043
* @param date the date for which the first day of the month is
3044
* calculated. The date has to be in the cut-over year (Gregorian
3045
* or Julian).
3046
* @param fixedDate the fixed date representation of the date
3047
*/
3048
private long getFixedDateMonth1(BaseCalendar.Date date, long fixedDate) {
3049
assert date.getNormalizedYear() == gregorianCutoverYear ||
3050
date.getNormalizedYear() == gregorianCutoverYearJulian;
3051
BaseCalendar.Date gCutover = getGregorianCutoverDate();
3052
if (gCutover.getMonth() == BaseCalendar.JANUARY
3053
&& gCutover.getDayOfMonth() == 1) {
3054
// The cutover happened on January 1.
3055
return fixedDate - date.getDayOfMonth() + 1;
3056
}
3057
3058
long fixedDateMonth1;
3059
// The cutover happened sometime during the year.
3060
if (date.getMonth() == gCutover.getMonth()) {
3061
// The cutover happened in the month.
3062
BaseCalendar.Date jLastDate = getLastJulianDate();
3063
if (gregorianCutoverYear == gregorianCutoverYearJulian
3064
&& gCutover.getMonth() == jLastDate.getMonth()) {
3065
// The "gap" fits in the same month.
3066
fixedDateMonth1 = jcal.getFixedDate(date.getNormalizedYear(),
3067
date.getMonth(),
3068
1,
3069
null);
3070
} else {
3071
// Use the cutover date as the first day of the month.
3072
fixedDateMonth1 = gregorianCutoverDate;
3073
}
3074
} else {
3075
// The cutover happened before the month.
3076
fixedDateMonth1 = fixedDate - date.getDayOfMonth() + 1;
3077
}
3078
3079
return fixedDateMonth1;
3080
}
3081
3082
/**
3083
* Returns a CalendarDate produced from the specified fixed date.
3084
*
3085
* @param fd the fixed date
3086
*/
3087
private BaseCalendar.Date getCalendarDate(long fd) {
3088
BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
3089
BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
3090
cal.getCalendarDateFromFixedDate(d, fd);
3091
return d;
3092
}
3093
3094
/**
3095
* Returns the Gregorian cutover date as a BaseCalendar.Date. The
3096
* date is a Gregorian date.
3097
*/
3098
private BaseCalendar.Date getGregorianCutoverDate() {
3099
return getCalendarDate(gregorianCutoverDate);
3100
}
3101
3102
/**
3103
* Returns the day before the Gregorian cutover date as a
3104
* BaseCalendar.Date. The date is a Julian date.
3105
*/
3106
private BaseCalendar.Date getLastJulianDate() {
3107
return getCalendarDate(gregorianCutoverDate - 1);
3108
}
3109
3110
/**
3111
* Returns the length of the specified month in the specified
3112
* year. The year number must be normalized.
3113
*
3114
* @see #isLeapYear(int)
3115
*/
3116
private int monthLength(int month, int year) {
3117
return isLeapYear(year) ? LEAP_MONTH_LENGTH[month] : MONTH_LENGTH[month];
3118
}
3119
3120
/**
3121
* Returns the length of the specified month in the year provided
3122
* by internalGet(YEAR).
3123
*
3124
* @see #isLeapYear(int)
3125
*/
3126
private int monthLength(int month) {
3127
int year = internalGet(YEAR);
3128
if (internalGetEra() == BCE) {
3129
year = 1 - year;
3130
}
3131
return monthLength(month, year);
3132
}
3133
3134
private int actualMonthLength() {
3135
int year = cdate.getNormalizedYear();
3136
if (year != gregorianCutoverYear && year != gregorianCutoverYearJulian) {
3137
return calsys.getMonthLength(cdate);
3138
}
3139
BaseCalendar.Date date = (BaseCalendar.Date) cdate.clone();
3140
long fd = calsys.getFixedDate(date);
3141
long month1 = getFixedDateMonth1(date, fd);
3142
long next1 = month1 + calsys.getMonthLength(date);
3143
if (next1 < gregorianCutoverDate) {
3144
return (int)(next1 - month1);
3145
}
3146
if (cdate != gdate) {
3147
date = (BaseCalendar.Date) gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
3148
}
3149
gcal.getCalendarDateFromFixedDate(date, next1);
3150
next1 = getFixedDateMonth1(date, next1);
3151
return (int)(next1 - month1);
3152
}
3153
3154
/**
3155
* Returns the length (in days) of the specified year. The year
3156
* must be normalized.
3157
*/
3158
private int yearLength(int year) {
3159
return isLeapYear(year) ? 366 : 365;
3160
}
3161
3162
/**
3163
* Returns the length (in days) of the year provided by
3164
* internalGet(YEAR).
3165
*/
3166
private int yearLength() {
3167
int year = internalGet(YEAR);
3168
if (internalGetEra() == BCE) {
3169
year = 1 - year;
3170
}
3171
return yearLength(year);
3172
}
3173
3174
/**
3175
* After adjustments such as add(MONTH), add(YEAR), we don't want the
3176
* month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar
3177
* 3, we want it to go to Feb 28. Adjustments which might run into this
3178
* problem call this method to retain the proper month.
3179
*/
3180
private void pinDayOfMonth() {
3181
int year = internalGet(YEAR);
3182
int monthLen;
3183
if (year > gregorianCutoverYear || year < gregorianCutoverYearJulian) {
3184
monthLen = monthLength(internalGet(MONTH));
3185
} else {
3186
GregorianCalendar gc = getNormalizedCalendar();
3187
monthLen = gc.getActualMaximum(DAY_OF_MONTH);
3188
}
3189
int dom = internalGet(DAY_OF_MONTH);
3190
if (dom > monthLen) {
3191
set(DAY_OF_MONTH, monthLen);
3192
}
3193
}
3194
3195
/**
3196
* Returns the fixed date value of this object. The time value and
3197
* calendar fields must be in synch.
3198
*/
3199
private long getCurrentFixedDate() {
3200
return (calsys == gcal) ? cachedFixedDate : calsys.getFixedDate(cdate);
3201
}
3202
3203
/**
3204
* Returns the new value after 'roll'ing the specified value and amount.
3205
*/
3206
private static int getRolledValue(int value, int amount, int min, int max) {
3207
assert value >= min && value <= max;
3208
int range = max - min + 1;
3209
amount %= range;
3210
int n = value + amount;
3211
if (n > max) {
3212
n -= range;
3213
} else if (n < min) {
3214
n += range;
3215
}
3216
assert n >= min && n <= max;
3217
return n;
3218
}
3219
3220
/**
3221
* Returns the ERA. We need a special method for this because the
3222
* default ERA is CE, but a zero (unset) ERA is BCE.
3223
*/
3224
private int internalGetEra() {
3225
return isSet(ERA) ? internalGet(ERA) : CE;
3226
}
3227
3228
/**
3229
* Updates internal state.
3230
*/
3231
private void readObject(ObjectInputStream stream)
3232
throws IOException, ClassNotFoundException {
3233
stream.defaultReadObject();
3234
if (gdate == null) {
3235
gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());
3236
cachedFixedDate = Long.MIN_VALUE;
3237
}
3238
setGregorianChange(gregorianCutover);
3239
}
3240
3241
/**
3242
* Converts this object to a {@code ZonedDateTime} that represents
3243
* the same point on the time-line as this {@code GregorianCalendar}.
3244
* <p>
3245
* Since this object supports a Julian-Gregorian cutover date and
3246
* {@code ZonedDateTime} does not, it is possible that the resulting year,
3247
* month and day will have different values. The result will represent the
3248
* correct date in the ISO calendar system, which will also be the same value
3249
* for Modified Julian Days.
3250
*
3251
* @return a zoned date-time representing the same point on the time-line
3252
* as this gregorian calendar
3253
* @since 1.8
3254
*/
3255
public ZonedDateTime toZonedDateTime() {
3256
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(getTimeInMillis()),
3257
getTimeZone().toZoneId());
3258
}
3259
3260
/**
3261
* Obtains an instance of {@code GregorianCalendar} with the default locale
3262
* from a {@code ZonedDateTime} object.
3263
* <p>
3264
* Since {@code ZonedDateTime} does not support a Julian-Gregorian cutover
3265
* date and uses ISO calendar system, the return GregorianCalendar is a pure
3266
* Gregorian calendar and uses ISO 8601 standard for week definitions,
3267
* which has {@code MONDAY} as the {@link Calendar#getFirstDayOfWeek()
3268
* FirstDayOfWeek} and {@code 4} as the value of the
3269
* {@link Calendar#getMinimalDaysInFirstWeek() MinimalDaysInFirstWeek}.
3270
* <p>
3271
* {@code ZoneDateTime} can store points on the time-line further in the
3272
* future and further in the past than {@code GregorianCalendar}. In this
3273
* scenario, this method will throw an {@code IllegalArgumentException}
3274
* exception.
3275
*
3276
* @param zdt the zoned date-time object to convert
3277
* @return the gregorian calendar representing the same point on the
3278
* time-line as the zoned date-time provided
3279
* @exception NullPointerException if {@code zdt} is null
3280
* @exception IllegalArgumentException if the zoned date-time is too
3281
* large to represent as a {@code GregorianCalendar}
3282
* @since 1.8
3283
*/
3284
public static GregorianCalendar from(ZonedDateTime zdt) {
3285
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone(zdt.getZone()));
3286
cal.setGregorianChange(new Date(Long.MIN_VALUE));
3287
cal.setFirstDayOfWeek(MONDAY);
3288
cal.setMinimalDaysInFirstWeek(4);
3289
try {
3290
cal.setTimeInMillis(Math.addExact(Math.multiplyExact(zdt.toEpochSecond(), 1000),
3291
zdt.get(ChronoField.MILLI_OF_SECOND)));
3292
} catch (ArithmeticException ex) {
3293
throw new IllegalArgumentException(ex);
3294
}
3295
return cal;
3296
}
3297
}
3298
3299