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