author | naoto |
Tue, 31 Aug 2010 11:27:10 -0700 | |
changeset 6489 | 9e7015635425 |
parent 5506 | 202f599c92aa |
child 6491 | cd1bcc5057f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1996, 2007, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* (C) Copyright Taligent, Inc. 1996-1998 - All Rights Reserved |
|
28 |
* (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved |
|
29 |
* |
|
30 |
* The original version of this source code and documentation is copyrighted |
|
31 |
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These |
|
32 |
* materials are provided under terms of a License Agreement between Taligent |
|
33 |
* and Sun. This technology is protected by multiple US and International |
|
34 |
* patents. This notice and attribution to Taligent may not be removed. |
|
35 |
* Taligent is a registered trademark of Taligent, Inc. |
|
36 |
* |
|
37 |
*/ |
|
38 |
||
39 |
package java.util; |
|
40 |
||
41 |
import java.io.IOException; |
|
42 |
import java.io.ObjectInputStream; |
|
43 |
import java.io.ObjectOutputStream; |
|
2599
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
44 |
import java.io.OptionalDataException; |
2 | 45 |
import java.io.Serializable; |
2599
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
46 |
import java.security.AccessControlContext; |
2 | 47 |
import java.security.AccessController; |
2599
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
48 |
import java.security.PermissionCollection; |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
49 |
import java.security.PrivilegedActionException; |
2 | 50 |
import java.security.PrivilegedExceptionAction; |
2599
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
51 |
import java.security.ProtectionDomain; |
2 | 52 |
import java.text.DateFormat; |
53 |
import java.text.DateFormatSymbols; |
|
54 |
import sun.util.BuddhistCalendar; |
|
55 |
import sun.util.calendar.ZoneInfo; |
|
56 |
import sun.util.resources.LocaleData; |
|
57 |
||
58 |
/** |
|
59 |
* The <code>Calendar</code> class is an abstract class that provides methods |
|
60 |
* for converting between a specific instant in time and a set of {@link |
|
61 |
* #fields calendar fields} such as <code>YEAR</code>, <code>MONTH</code>, |
|
62 |
* <code>DAY_OF_MONTH</code>, <code>HOUR</code>, and so on, and for |
|
63 |
* manipulating the calendar fields, such as getting the date of the next |
|
64 |
* week. An instant in time can be represented by a millisecond value that is |
|
65 |
* an offset from the <a name="Epoch"><em>Epoch</em></a>, January 1, 1970 |
|
66 |
* 00:00:00.000 GMT (Gregorian). |
|
67 |
* |
|
68 |
* <p>The class also provides additional fields and methods for |
|
69 |
* implementing a concrete calendar system outside the package. Those |
|
70 |
* fields and methods are defined as <code>protected</code>. |
|
71 |
* |
|
72 |
* <p> |
|
73 |
* Like other locale-sensitive classes, <code>Calendar</code> provides a |
|
74 |
* class method, <code>getInstance</code>, for getting a generally useful |
|
75 |
* object of this type. <code>Calendar</code>'s <code>getInstance</code> method |
|
76 |
* returns a <code>Calendar</code> object whose |
|
77 |
* calendar fields have been initialized with the current date and time: |
|
78 |
* <blockquote> |
|
79 |
* <pre> |
|
80 |
* Calendar rightNow = Calendar.getInstance(); |
|
81 |
* </pre> |
|
82 |
* </blockquote> |
|
83 |
* |
|
84 |
* <p>A <code>Calendar</code> object can produce all the calendar field values |
|
85 |
* needed to implement the date-time formatting for a particular language and |
|
86 |
* calendar style (for example, Japanese-Gregorian, Japanese-Traditional). |
|
87 |
* <code>Calendar</code> defines the range of values returned by |
|
88 |
* certain calendar fields, as well as their meaning. For example, |
|
89 |
* the first month of the calendar system has value <code>MONTH == |
|
90 |
* JANUARY</code> for all calendars. Other values are defined by the |
|
91 |
* concrete subclass, such as <code>ERA</code>. See individual field |
|
92 |
* documentation and subclass documentation for details. |
|
93 |
* |
|
94 |
* <h4>Getting and Setting Calendar Field Values</h4> |
|
95 |
* |
|
96 |
* <p>The calendar field values can be set by calling the <code>set</code> |
|
97 |
* methods. Any field values set in a <code>Calendar</code> will not be |
|
98 |
* interpreted until it needs to calculate its time value (milliseconds from |
|
99 |
* the Epoch) or values of the calendar fields. Calling the |
|
100 |
* <code>get</code>, <code>getTimeInMillis</code>, <code>getTime</code>, |
|
101 |
* <code>add</code> and <code>roll</code> involves such calculation. |
|
102 |
* |
|
103 |
* <h4>Leniency</h4> |
|
104 |
* |
|
105 |
* <p><code>Calendar</code> has two modes for interpreting the calendar |
|
106 |
* fields, <em>lenient</em> and <em>non-lenient</em>. When a |
|
107 |
* <code>Calendar</code> is in lenient mode, it accepts a wider range of |
|
108 |
* calendar field values than it produces. When a <code>Calendar</code> |
|
109 |
* recomputes calendar field values for return by <code>get()</code>, all of |
|
110 |
* the calendar fields are normalized. For example, a lenient |
|
111 |
* <code>GregorianCalendar</code> interprets <code>MONTH == JANUARY</code>, |
|
112 |
* <code>DAY_OF_MONTH == 32</code> as February 1. |
|
113 |
||
114 |
* <p>When a <code>Calendar</code> is in non-lenient mode, it throws an |
|
115 |
* exception if there is any inconsistency in its calendar fields. For |
|
116 |
* example, a <code>GregorianCalendar</code> always produces |
|
117 |
* <code>DAY_OF_MONTH</code> values between 1 and the length of the month. A |
|
118 |
* non-lenient <code>GregorianCalendar</code> throws an exception upon |
|
119 |
* calculating its time or calendar field values if any out-of-range field |
|
120 |
* value has been set. |
|
121 |
* |
|
122 |
* <h4>First Week</h4> |
|
123 |
* |
|
124 |
* <code>Calendar</code> defines a locale-specific seven day week using two |
|
125 |
* parameters: the first day of the week and the minimal days in first week |
|
126 |
* (from 1 to 7). These numbers are taken from the locale resource data when a |
|
127 |
* <code>Calendar</code> is constructed. They may also be specified explicitly |
|
128 |
* through the methods for setting their values. |
|
129 |
* |
|
130 |
* <p>When setting or getting the <code>WEEK_OF_MONTH</code> or |
|
131 |
* <code>WEEK_OF_YEAR</code> fields, <code>Calendar</code> must determine the |
|
132 |
* first week of the month or year as a reference point. The first week of a |
|
133 |
* month or year is defined as the earliest seven day period beginning on |
|
134 |
* <code>getFirstDayOfWeek()</code> and containing at least |
|
135 |
* <code>getMinimalDaysInFirstWeek()</code> days of that month or year. Weeks |
|
136 |
* numbered ..., -1, 0 precede the first week; weeks numbered 2, 3,... follow |
|
137 |
* it. Note that the normalized numbering returned by <code>get()</code> may be |
|
138 |
* different. For example, a specific <code>Calendar</code> subclass may |
|
139 |
* designate the week before week 1 of a year as week <code><i>n</i></code> of |
|
140 |
* the previous year. |
|
141 |
* |
|
142 |
* <h4>Calendar Fields Resolution</h4> |
|
143 |
* |
|
144 |
* When computing a date and time from the calendar fields, there |
|
145 |
* may be insufficient information for the computation (such as only |
|
146 |
* year and month with no day of month), or there may be inconsistent |
|
147 |
* information (such as Tuesday, July 15, 1996 (Gregorian) -- July 15, |
|
148 |
* 1996 is actually a Monday). <code>Calendar</code> will resolve |
|
149 |
* calendar field values to determine the date and time in the |
|
150 |
* following way. |
|
151 |
* |
|
152 |
* <p>If there is any conflict in calendar field values, |
|
153 |
* <code>Calendar</code> gives priorities to calendar fields that have been set |
|
154 |
* more recently. The following are the default combinations of the |
|
155 |
* calendar fields. The most recent combination, as determined by the |
|
156 |
* most recently set single field, will be used. |
|
157 |
* |
|
158 |
* <p><a name="date_resolution">For the date fields</a>: |
|
159 |
* <blockquote> |
|
160 |
* <pre> |
|
161 |
* YEAR + MONTH + DAY_OF_MONTH |
|
162 |
* YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK |
|
163 |
* YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK |
|
164 |
* YEAR + DAY_OF_YEAR |
|
165 |
* YEAR + DAY_OF_WEEK + WEEK_OF_YEAR |
|
166 |
* </pre></blockquote> |
|
167 |
* |
|
168 |
* <a name="time_resolution">For the time of day fields</a>: |
|
169 |
* <blockquote> |
|
170 |
* <pre> |
|
171 |
* HOUR_OF_DAY |
|
172 |
* AM_PM + HOUR |
|
173 |
* </pre></blockquote> |
|
174 |
* |
|
175 |
* <p>If there are any calendar fields whose values haven't been set in the selected |
|
176 |
* field combination, <code>Calendar</code> uses their default values. The default |
|
177 |
* value of each field may vary by concrete calendar systems. For example, in |
|
178 |
* <code>GregorianCalendar</code>, the default of a field is the same as that |
|
179 |
* of the start of the Epoch: i.e., <code>YEAR = 1970</code>, <code>MONTH = |
|
180 |
* JANUARY</code>, <code>DAY_OF_MONTH = 1</code>, etc. |
|
181 |
* |
|
182 |
* <p> |
|
183 |
* <strong>Note:</strong> There are certain possible ambiguities in |
|
184 |
* interpretation of certain singular times, which are resolved in the |
|
185 |
* following ways: |
|
186 |
* <ol> |
|
187 |
* <li> 23:59 is the last minute of the day and 00:00 is the first |
|
188 |
* minute of the next day. Thus, 23:59 on Dec 31, 1999 < 00:00 on |
|
189 |
* Jan 1, 2000 < 00:01 on Jan 1, 2000. |
|
190 |
* |
|
191 |
* <li> Although historically not precise, midnight also belongs to "am", |
|
192 |
* and noon belongs to "pm", so on the same day, |
|
193 |
* 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm |
|
194 |
* </ol> |
|
195 |
* |
|
196 |
* <p> |
|
197 |
* The date or time format strings are not part of the definition of a |
|
198 |
* calendar, as those must be modifiable or overridable by the user at |
|
199 |
* runtime. Use {@link DateFormat} |
|
200 |
* to format dates. |
|
201 |
* |
|
202 |
* <h4>Field Manipulation</h4> |
|
203 |
* |
|
204 |
* The calendar fields can be changed using three methods: |
|
205 |
* <code>set()</code>, <code>add()</code>, and <code>roll()</code>.</p> |
|
206 |
* |
|
207 |
* <p><strong><code>set(f, value)</code></strong> changes calendar field |
|
208 |
* <code>f</code> to <code>value</code>. In addition, it sets an |
|
209 |
* internal member variable to indicate that calendar field <code>f</code> has |
|
210 |
* been changed. Although calendar field <code>f</code> is changed immediately, |
|
211 |
* the calendar's time value in milliseconds is not recomputed until the next call to |
|
212 |
* <code>get()</code>, <code>getTime()</code>, <code>getTimeInMillis()</code>, |
|
213 |
* <code>add()</code>, or <code>roll()</code> is made. Thus, multiple calls to |
|
214 |
* <code>set()</code> do not trigger multiple, unnecessary |
|
215 |
* computations. As a result of changing a calendar field using |
|
216 |
* <code>set()</code>, other calendar fields may also change, depending on the |
|
217 |
* calendar field, the calendar field value, and the calendar system. In addition, |
|
218 |
* <code>get(f)</code> will not necessarily return <code>value</code> set by |
|
219 |
* the call to the <code>set</code> method |
|
220 |
* after the calendar fields have been recomputed. The specifics are determined by |
|
221 |
* the concrete calendar class.</p> |
|
222 |
* |
|
223 |
* <p><em>Example</em>: Consider a <code>GregorianCalendar</code> |
|
224 |
* originally set to August 31, 1999. Calling <code>set(Calendar.MONTH, |
|
225 |
* Calendar.SEPTEMBER)</code> sets the date to September 31, |
|
226 |
* 1999. This is a temporary internal representation that resolves to |
|
227 |
* October 1, 1999 if <code>getTime()</code>is then called. However, a |
|
228 |
* call to <code>set(Calendar.DAY_OF_MONTH, 30)</code> before the call to |
|
229 |
* <code>getTime()</code> sets the date to September 30, 1999, since |
|
230 |
* no recomputation occurs after <code>set()</code> itself.</p> |
|
231 |
* |
|
232 |
* <p><strong><code>add(f, delta)</code></strong> adds <code>delta</code> |
|
233 |
* to field <code>f</code>. This is equivalent to calling <code>set(f, |
|
234 |
* get(f) + delta)</code> with two adjustments:</p> |
|
235 |
* |
|
236 |
* <blockquote> |
|
237 |
* <p><strong>Add rule 1</strong>. The value of field <code>f</code> |
|
238 |
* after the call minus the value of field <code>f</code> before the |
|
239 |
* call is <code>delta</code>, modulo any overflow that has occurred in |
|
240 |
* field <code>f</code>. Overflow occurs when a field value exceeds its |
|
241 |
* range and, as a result, the next larger field is incremented or |
|
242 |
* decremented and the field value is adjusted back into its range.</p> |
|
243 |
* |
|
244 |
* <p><strong>Add rule 2</strong>. If a smaller field is expected to be |
|
245 |
* invariant, but it is impossible for it to be equal to its |
|
246 |
* prior value because of changes in its minimum or maximum after field |
|
247 |
* <code>f</code> is changed or other constraints, such as time zone |
|
248 |
* offset changes, then its value is adjusted to be as close |
|
249 |
* as possible to its expected value. A smaller field represents a |
|
250 |
* smaller unit of time. <code>HOUR</code> is a smaller field than |
|
251 |
* <code>DAY_OF_MONTH</code>. No adjustment is made to smaller fields |
|
252 |
* that are not expected to be invariant. The calendar system |
|
253 |
* determines what fields are expected to be invariant.</p> |
|
254 |
* </blockquote> |
|
255 |
* |
|
256 |
* <p>In addition, unlike <code>set()</code>, <code>add()</code> forces |
|
257 |
* an immediate recomputation of the calendar's milliseconds and all |
|
258 |
* fields.</p> |
|
259 |
* |
|
260 |
* <p><em>Example</em>: Consider a <code>GregorianCalendar</code> |
|
261 |
* originally set to August 31, 1999. Calling <code>add(Calendar.MONTH, |
|
262 |
* 13)</code> sets the calendar to September 30, 2000. <strong>Add rule |
|
263 |
* 1</strong> sets the <code>MONTH</code> field to September, since |
|
264 |
* adding 13 months to August gives September of the next year. Since |
|
265 |
* <code>DAY_OF_MONTH</code> cannot be 31 in September in a |
|
266 |
* <code>GregorianCalendar</code>, <strong>add rule 2</strong> sets the |
|
267 |
* <code>DAY_OF_MONTH</code> to 30, the closest possible value. Although |
|
268 |
* it is a smaller field, <code>DAY_OF_WEEK</code> is not adjusted by |
|
269 |
* rule 2, since it is expected to change when the month changes in a |
|
270 |
* <code>GregorianCalendar</code>.</p> |
|
271 |
* |
|
272 |
* <p><strong><code>roll(f, delta)</code></strong> adds |
|
273 |
* <code>delta</code> to field <code>f</code> without changing larger |
|
274 |
* fields. This is equivalent to calling <code>add(f, delta)</code> with |
|
275 |
* the following adjustment:</p> |
|
276 |
* |
|
277 |
* <blockquote> |
|
278 |
* <p><strong>Roll rule</strong>. Larger fields are unchanged after the |
|
279 |
* call. A larger field represents a larger unit of |
|
280 |
* time. <code>DAY_OF_MONTH</code> is a larger field than |
|
281 |
* <code>HOUR</code>.</p> |
|
282 |
* </blockquote> |
|
283 |
* |
|
284 |
* <p><em>Example</em>: See {@link java.util.GregorianCalendar#roll(int, int)}. |
|
285 |
* |
|
286 |
* <p><strong>Usage model</strong>. To motivate the behavior of |
|
287 |
* <code>add()</code> and <code>roll()</code>, consider a user interface |
|
288 |
* component with increment and decrement buttons for the month, day, and |
|
289 |
* year, and an underlying <code>GregorianCalendar</code>. If the |
|
290 |
* interface reads January 31, 1999 and the user presses the month |
|
291 |
* increment button, what should it read? If the underlying |
|
292 |
* implementation uses <code>set()</code>, it might read March 3, 1999. A |
|
293 |
* better result would be February 28, 1999. Furthermore, if the user |
|
294 |
* presses the month increment button again, it should read March 31, |
|
295 |
* 1999, not March 28, 1999. By saving the original date and using either |
|
296 |
* <code>add()</code> or <code>roll()</code>, depending on whether larger |
|
297 |
* fields should be affected, the user interface can behave as most users |
|
298 |
* will intuitively expect.</p> |
|
299 |
* |
|
300 |
* @see java.lang.System#currentTimeMillis() |
|
301 |
* @see Date |
|
302 |
* @see GregorianCalendar |
|
303 |
* @see TimeZone |
|
304 |
* @see java.text.DateFormat |
|
305 |
* @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu |
|
306 |
* @since JDK1.1 |
|
307 |
*/ |
|
308 |
public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar> { |
|
309 |
||
310 |
// Data flow in Calendar |
|
311 |
// --------------------- |
|
312 |
||
313 |
// The current time is represented in two ways by Calendar: as UTC |
|
314 |
// milliseconds from the epoch (1 January 1970 0:00 UTC), and as local |
|
315 |
// fields such as MONTH, HOUR, AM_PM, etc. It is possible to compute the |
|
316 |
// millis from the fields, and vice versa. The data needed to do this |
|
317 |
// conversion is encapsulated by a TimeZone object owned by the Calendar. |
|
318 |
// The data provided by the TimeZone object may also be overridden if the |
|
319 |
// user sets the ZONE_OFFSET and/or DST_OFFSET fields directly. The class |
|
320 |
// keeps track of what information was most recently set by the caller, and |
|
321 |
// uses that to compute any other information as needed. |
|
322 |
||
323 |
// If the user sets the fields using set(), the data flow is as follows. |
|
324 |
// This is implemented by the Calendar subclass's computeTime() method. |
|
325 |
// During this process, certain fields may be ignored. The disambiguation |
|
326 |
// algorithm for resolving which fields to pay attention to is described |
|
327 |
// in the class documentation. |
|
328 |
||
329 |
// local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.) |
|
330 |
// | |
|
331 |
// | Using Calendar-specific algorithm |
|
332 |
// V |
|
333 |
// local standard millis |
|
334 |
// | |
|
335 |
// | Using TimeZone or user-set ZONE_OFFSET / DST_OFFSET |
|
336 |
// V |
|
337 |
// UTC millis (in time data member) |
|
338 |
||
339 |
// If the user sets the UTC millis using setTime() or setTimeInMillis(), |
|
340 |
// the data flow is as follows. This is implemented by the Calendar |
|
341 |
// subclass's computeFields() method. |
|
342 |
||
343 |
// UTC millis (in time data member) |
|
344 |
// | |
|
345 |
// | Using TimeZone getOffset() |
|
346 |
// V |
|
347 |
// local standard millis |
|
348 |
// | |
|
349 |
// | Using Calendar-specific algorithm |
|
350 |
// V |
|
351 |
// local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.) |
|
352 |
||
353 |
// In general, a round trip from fields, through local and UTC millis, and |
|
354 |
// back out to fields is made when necessary. This is implemented by the |
|
355 |
// complete() method. Resolving a partial set of fields into a UTC millis |
|
356 |
// value allows all remaining fields to be generated from that value. If |
|
357 |
// the Calendar is lenient, the fields are also renormalized to standard |
|
358 |
// ranges when they are regenerated. |
|
359 |
||
360 |
/** |
|
361 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
362 |
* era, e.g., AD or BC in the Julian calendar. This is a calendar-specific |
|
363 |
* value; see subclass documentation. |
|
364 |
* |
|
365 |
* @see GregorianCalendar#AD |
|
366 |
* @see GregorianCalendar#BC |
|
367 |
*/ |
|
368 |
public final static int ERA = 0; |
|
369 |
||
370 |
/** |
|
371 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
372 |
* year. This is a calendar-specific value; see subclass documentation. |
|
373 |
*/ |
|
374 |
public final static int YEAR = 1; |
|
375 |
||
376 |
/** |
|
377 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
378 |
* month. This is a calendar-specific value. The first month of |
|
379 |
* the year in the Gregorian and Julian calendars is |
|
380 |
* <code>JANUARY</code> which is 0; the last depends on the number |
|
381 |
* of months in a year. |
|
382 |
* |
|
383 |
* @see #JANUARY |
|
384 |
* @see #FEBRUARY |
|
385 |
* @see #MARCH |
|
386 |
* @see #APRIL |
|
387 |
* @see #MAY |
|
388 |
* @see #JUNE |
|
389 |
* @see #JULY |
|
390 |
* @see #AUGUST |
|
391 |
* @see #SEPTEMBER |
|
392 |
* @see #OCTOBER |
|
393 |
* @see #NOVEMBER |
|
394 |
* @see #DECEMBER |
|
395 |
* @see #UNDECIMBER |
|
396 |
*/ |
|
397 |
public final static int MONTH = 2; |
|
398 |
||
399 |
/** |
|
400 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
401 |
* week number within the current year. The first week of the year, as |
|
402 |
* defined by <code>getFirstDayOfWeek()</code> and |
|
403 |
* <code>getMinimalDaysInFirstWeek()</code>, has value 1. Subclasses define |
|
404 |
* the value of <code>WEEK_OF_YEAR</code> for days before the first week of |
|
405 |
* the year. |
|
406 |
* |
|
407 |
* @see #getFirstDayOfWeek |
|
408 |
* @see #getMinimalDaysInFirstWeek |
|
409 |
*/ |
|
410 |
public final static int WEEK_OF_YEAR = 3; |
|
411 |
||
412 |
/** |
|
413 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
414 |
* week number within the current month. The first week of the month, as |
|
415 |
* defined by <code>getFirstDayOfWeek()</code> and |
|
416 |
* <code>getMinimalDaysInFirstWeek()</code>, has value 1. Subclasses define |
|
417 |
* the value of <code>WEEK_OF_MONTH</code> for days before the first week of |
|
418 |
* the month. |
|
419 |
* |
|
420 |
* @see #getFirstDayOfWeek |
|
421 |
* @see #getMinimalDaysInFirstWeek |
|
422 |
*/ |
|
423 |
public final static int WEEK_OF_MONTH = 4; |
|
424 |
||
425 |
/** |
|
426 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
427 |
* day of the month. This is a synonym for <code>DAY_OF_MONTH</code>. |
|
428 |
* The first day of the month has value 1. |
|
429 |
* |
|
430 |
* @see #DAY_OF_MONTH |
|
431 |
*/ |
|
432 |
public final static int DATE = 5; |
|
433 |
||
434 |
/** |
|
435 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
436 |
* day of the month. This is a synonym for <code>DATE</code>. |
|
437 |
* The first day of the month has value 1. |
|
438 |
* |
|
439 |
* @see #DATE |
|
440 |
*/ |
|
441 |
public final static int DAY_OF_MONTH = 5; |
|
442 |
||
443 |
/** |
|
444 |
* Field number for <code>get</code> and <code>set</code> indicating the day |
|
445 |
* number within the current year. The first day of the year has value 1. |
|
446 |
*/ |
|
447 |
public final static int DAY_OF_YEAR = 6; |
|
448 |
||
449 |
/** |
|
450 |
* Field number for <code>get</code> and <code>set</code> indicating the day |
|
451 |
* of the week. This field takes values <code>SUNDAY</code>, |
|
452 |
* <code>MONDAY</code>, <code>TUESDAY</code>, <code>WEDNESDAY</code>, |
|
453 |
* <code>THURSDAY</code>, <code>FRIDAY</code>, and <code>SATURDAY</code>. |
|
454 |
* |
|
455 |
* @see #SUNDAY |
|
456 |
* @see #MONDAY |
|
457 |
* @see #TUESDAY |
|
458 |
* @see #WEDNESDAY |
|
459 |
* @see #THURSDAY |
|
460 |
* @see #FRIDAY |
|
461 |
* @see #SATURDAY |
|
462 |
*/ |
|
463 |
public final static int DAY_OF_WEEK = 7; |
|
464 |
||
465 |
/** |
|
466 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
467 |
* ordinal number of the day of the week within the current month. Together |
|
468 |
* with the <code>DAY_OF_WEEK</code> field, this uniquely specifies a day |
|
469 |
* within a month. Unlike <code>WEEK_OF_MONTH</code> and |
|
470 |
* <code>WEEK_OF_YEAR</code>, this field's value does <em>not</em> depend on |
|
471 |
* <code>getFirstDayOfWeek()</code> or |
|
472 |
* <code>getMinimalDaysInFirstWeek()</code>. <code>DAY_OF_MONTH 1</code> |
|
473 |
* through <code>7</code> always correspond to <code>DAY_OF_WEEK_IN_MONTH |
|
474 |
* 1</code>; <code>8</code> through <code>14</code> correspond to |
|
475 |
* <code>DAY_OF_WEEK_IN_MONTH 2</code>, and so on. |
|
476 |
* <code>DAY_OF_WEEK_IN_MONTH 0</code> indicates the week before |
|
477 |
* <code>DAY_OF_WEEK_IN_MONTH 1</code>. Negative values count back from the |
|
478 |
* end of the month, so the last Sunday of a month is specified as |
|
479 |
* <code>DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1</code>. Because |
|
480 |
* negative values count backward they will usually be aligned differently |
|
481 |
* within the month than positive values. For example, if a month has 31 |
|
482 |
* days, <code>DAY_OF_WEEK_IN_MONTH -1</code> will overlap |
|
483 |
* <code>DAY_OF_WEEK_IN_MONTH 5</code> and the end of <code>4</code>. |
|
484 |
* |
|
485 |
* @see #DAY_OF_WEEK |
|
486 |
* @see #WEEK_OF_MONTH |
|
487 |
*/ |
|
488 |
public final static int DAY_OF_WEEK_IN_MONTH = 8; |
|
489 |
||
490 |
/** |
|
491 |
* Field number for <code>get</code> and <code>set</code> indicating |
|
492 |
* whether the <code>HOUR</code> is before or after noon. |
|
493 |
* E.g., at 10:04:15.250 PM the <code>AM_PM</code> is <code>PM</code>. |
|
494 |
* |
|
495 |
* @see #AM |
|
496 |
* @see #PM |
|
497 |
* @see #HOUR |
|
498 |
*/ |
|
499 |
public final static int AM_PM = 9; |
|
500 |
||
501 |
/** |
|
502 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
503 |
* hour of the morning or afternoon. <code>HOUR</code> is used for the |
|
504 |
* 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. |
|
505 |
* E.g., at 10:04:15.250 PM the <code>HOUR</code> is 10. |
|
506 |
* |
|
507 |
* @see #AM_PM |
|
508 |
* @see #HOUR_OF_DAY |
|
509 |
*/ |
|
510 |
public final static int HOUR = 10; |
|
511 |
||
512 |
/** |
|
513 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
514 |
* hour of the day. <code>HOUR_OF_DAY</code> is used for the 24-hour clock. |
|
515 |
* E.g., at 10:04:15.250 PM the <code>HOUR_OF_DAY</code> is 22. |
|
516 |
* |
|
517 |
* @see #HOUR |
|
518 |
*/ |
|
519 |
public final static int HOUR_OF_DAY = 11; |
|
520 |
||
521 |
/** |
|
522 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
523 |
* minute within the hour. |
|
524 |
* E.g., at 10:04:15.250 PM the <code>MINUTE</code> is 4. |
|
525 |
*/ |
|
526 |
public final static int MINUTE = 12; |
|
527 |
||
528 |
/** |
|
529 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
530 |
* second within the minute. |
|
531 |
* E.g., at 10:04:15.250 PM the <code>SECOND</code> is 15. |
|
532 |
*/ |
|
533 |
public final static int SECOND = 13; |
|
534 |
||
535 |
/** |
|
536 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
537 |
* millisecond within the second. |
|
538 |
* E.g., at 10:04:15.250 PM the <code>MILLISECOND</code> is 250. |
|
539 |
*/ |
|
540 |
public final static int MILLISECOND = 14; |
|
541 |
||
542 |
/** |
|
543 |
* Field number for <code>get</code> and <code>set</code> |
|
544 |
* indicating the raw offset from GMT in milliseconds. |
|
545 |
* <p> |
|
546 |
* This field reflects the correct GMT offset value of the time |
|
547 |
* zone of this <code>Calendar</code> if the |
|
548 |
* <code>TimeZone</code> implementation subclass supports |
|
549 |
* historical GMT offset changes. |
|
550 |
*/ |
|
551 |
public final static int ZONE_OFFSET = 15; |
|
552 |
||
553 |
/** |
|
554 |
* Field number for <code>get</code> and <code>set</code> indicating the |
|
555 |
* daylight savings offset in milliseconds. |
|
556 |
* <p> |
|
557 |
* This field reflects the correct daylight saving offset value of |
|
558 |
* the time zone of this <code>Calendar</code> if the |
|
559 |
* <code>TimeZone</code> implementation subclass supports |
|
560 |
* historical Daylight Saving Time schedule changes. |
|
561 |
*/ |
|
562 |
public final static int DST_OFFSET = 16; |
|
563 |
||
564 |
/** |
|
565 |
* The number of distinct fields recognized by <code>get</code> and <code>set</code>. |
|
566 |
* Field numbers range from <code>0..FIELD_COUNT-1</code>. |
|
567 |
*/ |
|
568 |
public final static int FIELD_COUNT = 17; |
|
569 |
||
570 |
/** |
|
571 |
* Value of the {@link #DAY_OF_WEEK} field indicating |
|
572 |
* Sunday. |
|
573 |
*/ |
|
574 |
public final static int SUNDAY = 1; |
|
575 |
||
576 |
/** |
|
577 |
* Value of the {@link #DAY_OF_WEEK} field indicating |
|
578 |
* Monday. |
|
579 |
*/ |
|
580 |
public final static int MONDAY = 2; |
|
581 |
||
582 |
/** |
|
583 |
* Value of the {@link #DAY_OF_WEEK} field indicating |
|
584 |
* Tuesday. |
|
585 |
*/ |
|
586 |
public final static int TUESDAY = 3; |
|
587 |
||
588 |
/** |
|
589 |
* Value of the {@link #DAY_OF_WEEK} field indicating |
|
590 |
* Wednesday. |
|
591 |
*/ |
|
592 |
public final static int WEDNESDAY = 4; |
|
593 |
||
594 |
/** |
|
595 |
* Value of the {@link #DAY_OF_WEEK} field indicating |
|
596 |
* Thursday. |
|
597 |
*/ |
|
598 |
public final static int THURSDAY = 5; |
|
599 |
||
600 |
/** |
|
601 |
* Value of the {@link #DAY_OF_WEEK} field indicating |
|
602 |
* Friday. |
|
603 |
*/ |
|
604 |
public final static int FRIDAY = 6; |
|
605 |
||
606 |
/** |
|
607 |
* Value of the {@link #DAY_OF_WEEK} field indicating |
|
608 |
* Saturday. |
|
609 |
*/ |
|
610 |
public final static int SATURDAY = 7; |
|
611 |
||
612 |
/** |
|
613 |
* Value of the {@link #MONTH} field indicating the |
|
614 |
* first month of the year in the Gregorian and Julian calendars. |
|
615 |
*/ |
|
616 |
public final static int JANUARY = 0; |
|
617 |
||
618 |
/** |
|
619 |
* Value of the {@link #MONTH} field indicating the |
|
620 |
* second month of the year in the Gregorian and Julian calendars. |
|
621 |
*/ |
|
622 |
public final static int FEBRUARY = 1; |
|
623 |
||
624 |
/** |
|
625 |
* Value of the {@link #MONTH} field indicating the |
|
626 |
* third month of the year in the Gregorian and Julian calendars. |
|
627 |
*/ |
|
628 |
public final static int MARCH = 2; |
|
629 |
||
630 |
/** |
|
631 |
* Value of the {@link #MONTH} field indicating the |
|
632 |
* fourth month of the year in the Gregorian and Julian calendars. |
|
633 |
*/ |
|
634 |
public final static int APRIL = 3; |
|
635 |
||
636 |
/** |
|
637 |
* Value of the {@link #MONTH} field indicating the |
|
638 |
* fifth month of the year in the Gregorian and Julian calendars. |
|
639 |
*/ |
|
640 |
public final static int MAY = 4; |
|
641 |
||
642 |
/** |
|
643 |
* Value of the {@link #MONTH} field indicating the |
|
644 |
* sixth month of the year in the Gregorian and Julian calendars. |
|
645 |
*/ |
|
646 |
public final static int JUNE = 5; |
|
647 |
||
648 |
/** |
|
649 |
* Value of the {@link #MONTH} field indicating the |
|
650 |
* seventh month of the year in the Gregorian and Julian calendars. |
|
651 |
*/ |
|
652 |
public final static int JULY = 6; |
|
653 |
||
654 |
/** |
|
655 |
* Value of the {@link #MONTH} field indicating the |
|
656 |
* eighth month of the year in the Gregorian and Julian calendars. |
|
657 |
*/ |
|
658 |
public final static int AUGUST = 7; |
|
659 |
||
660 |
/** |
|
661 |
* Value of the {@link #MONTH} field indicating the |
|
662 |
* ninth month of the year in the Gregorian and Julian calendars. |
|
663 |
*/ |
|
664 |
public final static int SEPTEMBER = 8; |
|
665 |
||
666 |
/** |
|
667 |
* Value of the {@link #MONTH} field indicating the |
|
668 |
* tenth month of the year in the Gregorian and Julian calendars. |
|
669 |
*/ |
|
670 |
public final static int OCTOBER = 9; |
|
671 |
||
672 |
/** |
|
673 |
* Value of the {@link #MONTH} field indicating the |
|
674 |
* eleventh month of the year in the Gregorian and Julian calendars. |
|
675 |
*/ |
|
676 |
public final static int NOVEMBER = 10; |
|
677 |
||
678 |
/** |
|
679 |
* Value of the {@link #MONTH} field indicating the |
|
680 |
* twelfth month of the year in the Gregorian and Julian calendars. |
|
681 |
*/ |
|
682 |
public final static int DECEMBER = 11; |
|
683 |
||
684 |
/** |
|
685 |
* Value of the {@link #MONTH} field indicating the |
|
686 |
* thirteenth month of the year. Although <code>GregorianCalendar</code> |
|
687 |
* does not use this value, lunar calendars do. |
|
688 |
*/ |
|
689 |
public final static int UNDECIMBER = 12; |
|
690 |
||
691 |
/** |
|
692 |
* Value of the {@link #AM_PM} field indicating the |
|
693 |
* period of the day from midnight to just before noon. |
|
694 |
*/ |
|
695 |
public final static int AM = 0; |
|
696 |
||
697 |
/** |
|
698 |
* Value of the {@link #AM_PM} field indicating the |
|
699 |
* period of the day from noon to just before midnight. |
|
700 |
*/ |
|
701 |
public final static int PM = 1; |
|
702 |
||
703 |
/** |
|
704 |
* A style specifier for {@link #getDisplayNames(int, int, Locale) |
|
705 |
* getDisplayNames} indicating names in all styles, such as |
|
706 |
* "January" and "Jan". |
|
707 |
* |
|
708 |
* @see #SHORT |
|
709 |
* @see #LONG |
|
710 |
* @since 1.6 |
|
711 |
*/ |
|
712 |
public static final int ALL_STYLES = 0; |
|
713 |
||
714 |
/** |
|
715 |
* A style specifier for {@link #getDisplayName(int, int, Locale) |
|
716 |
* getDisplayName} and {@link #getDisplayNames(int, int, Locale) |
|
717 |
* getDisplayNames} indicating a short name, such as "Jan". |
|
718 |
* |
|
719 |
* @see #LONG |
|
720 |
* @since 1.6 |
|
721 |
*/ |
|
722 |
public static final int SHORT = 1; |
|
723 |
||
724 |
/** |
|
725 |
* A style specifier for {@link #getDisplayName(int, int, Locale) |
|
726 |
* getDisplayName} and {@link #getDisplayNames(int, int, Locale) |
|
727 |
* getDisplayNames} indicating a long name, such as "January". |
|
728 |
* |
|
729 |
* @see #SHORT |
|
730 |
* @since 1.6 |
|
731 |
*/ |
|
732 |
public static final int LONG = 2; |
|
733 |
||
734 |
// Internal notes: |
|
735 |
// Calendar contains two kinds of time representations: current "time" in |
|
736 |
// milliseconds, and a set of calendar "fields" representing the current time. |
|
737 |
// The two representations are usually in sync, but can get out of sync |
|
738 |
// as follows. |
|
739 |
// 1. Initially, no fields are set, and the time is invalid. |
|
740 |
// 2. If the time is set, all fields are computed and in sync. |
|
741 |
// 3. If a single field is set, the time is invalid. |
|
742 |
// Recomputation of the time and fields happens when the object needs |
|
743 |
// to return a result to the user, or use a result for a computation. |
|
744 |
||
745 |
/** |
|
746 |
* The calendar field values for the currently set time for this calendar. |
|
747 |
* This is an array of <code>FIELD_COUNT</code> integers, with index values |
|
748 |
* <code>ERA</code> through <code>DST_OFFSET</code>. |
|
749 |
* @serial |
|
750 |
*/ |
|
751 |
protected int fields[]; |
|
752 |
||
753 |
/** |
|
754 |
* The flags which tell if a specified calendar field for the calendar is set. |
|
755 |
* A new object has no fields set. After the first call to a method |
|
756 |
* which generates the fields, they all remain set after that. |
|
757 |
* This is an array of <code>FIELD_COUNT</code> booleans, with index values |
|
758 |
* <code>ERA</code> through <code>DST_OFFSET</code>. |
|
759 |
* @serial |
|
760 |
*/ |
|
761 |
protected boolean isSet[]; |
|
762 |
||
763 |
/** |
|
764 |
* Pseudo-time-stamps which specify when each field was set. There |
|
765 |
* are two special values, UNSET and COMPUTED. Values from |
|
766 |
* MINIMUM_USER_SET to Integer.MAX_VALUE are legal user set values. |
|
767 |
*/ |
|
768 |
transient private int stamp[]; |
|
769 |
||
770 |
/** |
|
771 |
* The currently set time for this calendar, expressed in milliseconds after |
|
772 |
* January 1, 1970, 0:00:00 GMT. |
|
773 |
* @see #isTimeSet |
|
774 |
* @serial |
|
775 |
*/ |
|
776 |
protected long time; |
|
777 |
||
778 |
/** |
|
779 |
* True if then the value of <code>time</code> is valid. |
|
780 |
* The time is made invalid by a change to an item of <code>field[]</code>. |
|
781 |
* @see #time |
|
782 |
* @serial |
|
783 |
*/ |
|
784 |
protected boolean isTimeSet; |
|
785 |
||
786 |
/** |
|
787 |
* True if <code>fields[]</code> are in sync with the currently set time. |
|
788 |
* If false, then the next attempt to get the value of a field will |
|
789 |
* force a recomputation of all fields from the current value of |
|
790 |
* <code>time</code>. |
|
791 |
* @serial |
|
792 |
*/ |
|
793 |
protected boolean areFieldsSet; |
|
794 |
||
795 |
/** |
|
796 |
* True if all fields have been set. |
|
797 |
* @serial |
|
798 |
*/ |
|
799 |
transient boolean areAllFieldsSet; |
|
800 |
||
801 |
/** |
|
802 |
* <code>True</code> if this calendar allows out-of-range field values during computation |
|
803 |
* of <code>time</code> from <code>fields[]</code>. |
|
804 |
* @see #setLenient |
|
805 |
* @see #isLenient |
|
806 |
* @serial |
|
807 |
*/ |
|
808 |
private boolean lenient = true; |
|
809 |
||
810 |
/** |
|
811 |
* The <code>TimeZone</code> used by this calendar. <code>Calendar</code> |
|
812 |
* uses the time zone data to translate between locale and GMT time. |
|
813 |
* @serial |
|
814 |
*/ |
|
815 |
private TimeZone zone; |
|
816 |
||
817 |
/** |
|
818 |
* <code>True</code> if zone references to a shared TimeZone object. |
|
819 |
*/ |
|
820 |
transient private boolean sharedZone = false; |
|
821 |
||
822 |
/** |
|
823 |
* The first day of the week, with possible values <code>SUNDAY</code>, |
|
824 |
* <code>MONDAY</code>, etc. This is a locale-dependent value. |
|
825 |
* @serial |
|
826 |
*/ |
|
827 |
private int firstDayOfWeek; |
|
828 |
||
829 |
/** |
|
830 |
* The number of days required for the first week in a month or year, |
|
831 |
* with possible values from 1 to 7. This is a locale-dependent value. |
|
832 |
* @serial |
|
833 |
*/ |
|
834 |
private int minimalDaysInFirstWeek; |
|
835 |
||
836 |
/** |
|
837 |
* Cache to hold the firstDayOfWeek and minimalDaysInFirstWeek |
|
838 |
* of a Locale. |
|
839 |
*/ |
|
840 |
private static Hashtable<Locale, int[]> cachedLocaleData = new Hashtable<Locale, int[]>(3); |
|
841 |
||
842 |
// Special values of stamp[] |
|
843 |
/** |
|
844 |
* The corresponding fields[] has no value. |
|
845 |
*/ |
|
846 |
private static final int UNSET = 0; |
|
847 |
||
848 |
/** |
|
849 |
* The value of the corresponding fields[] has been calculated internally. |
|
850 |
*/ |
|
851 |
private static final int COMPUTED = 1; |
|
852 |
||
853 |
/** |
|
854 |
* The value of the corresponding fields[] has been set externally. Stamp |
|
855 |
* values which are greater than 1 represents the (pseudo) time when the |
|
856 |
* corresponding fields[] value was set. |
|
857 |
*/ |
|
858 |
private static final int MINIMUM_USER_STAMP = 2; |
|
859 |
||
860 |
/** |
|
861 |
* The mask value that represents all of the fields. |
|
862 |
*/ |
|
863 |
static final int ALL_FIELDS = (1 << FIELD_COUNT) - 1; |
|
864 |
||
865 |
/** |
|
866 |
* The next available value for <code>stamp[]</code>, an internal array. |
|
867 |
* This actually should not be written out to the stream, and will probably |
|
868 |
* be removed from the stream in the near future. In the meantime, |
|
869 |
* a value of <code>MINIMUM_USER_STAMP</code> should be used. |
|
870 |
* @serial |
|
871 |
*/ |
|
872 |
private int nextStamp = MINIMUM_USER_STAMP; |
|
873 |
||
874 |
// the internal serial version which says which version was written |
|
875 |
// - 0 (default) for version up to JDK 1.1.5 |
|
876 |
// - 1 for version from JDK 1.1.6, which writes a correct 'time' value |
|
877 |
// as well as compatible values for other fields. This is a |
|
878 |
// transitional format. |
|
879 |
// - 2 (not implemented yet) a future version, in which fields[], |
|
880 |
// areFieldsSet, and isTimeSet become transient, and isSet[] is |
|
881 |
// removed. In JDK 1.1.6 we write a format compatible with version 2. |
|
882 |
static final int currentSerialVersion = 1; |
|
883 |
||
884 |
/** |
|
885 |
* The version of the serialized data on the stream. Possible values: |
|
886 |
* <dl> |
|
887 |
* <dt><b>0</b> or not present on stream</dt> |
|
888 |
* <dd> |
|
889 |
* JDK 1.1.5 or earlier. |
|
890 |
* </dd> |
|
891 |
* <dt><b>1</b></dt> |
|
892 |
* <dd> |
|
893 |
* JDK 1.1.6 or later. Writes a correct 'time' value |
|
894 |
* as well as compatible values for other fields. This is a |
|
895 |
* transitional format. |
|
896 |
* </dd> |
|
897 |
* </dl> |
|
898 |
* When streaming out this class, the most recent format |
|
899 |
* and the highest allowable <code>serialVersionOnStream</code> |
|
900 |
* is written. |
|
901 |
* @serial |
|
902 |
* @since JDK1.1.6 |
|
903 |
*/ |
|
904 |
private int serialVersionOnStream = currentSerialVersion; |
|
905 |
||
906 |
// Proclaim serialization compatibility with JDK 1.1 |
|
907 |
static final long serialVersionUID = -1807547505821590642L; |
|
908 |
||
909 |
// Mask values for calendar fields |
|
910 |
final static int ERA_MASK = (1 << ERA); |
|
911 |
final static int YEAR_MASK = (1 << YEAR); |
|
912 |
final static int MONTH_MASK = (1 << MONTH); |
|
913 |
final static int WEEK_OF_YEAR_MASK = (1 << WEEK_OF_YEAR); |
|
914 |
final static int WEEK_OF_MONTH_MASK = (1 << WEEK_OF_MONTH); |
|
915 |
final static int DAY_OF_MONTH_MASK = (1 << DAY_OF_MONTH); |
|
916 |
final static int DATE_MASK = DAY_OF_MONTH_MASK; |
|
917 |
final static int DAY_OF_YEAR_MASK = (1 << DAY_OF_YEAR); |
|
918 |
final static int DAY_OF_WEEK_MASK = (1 << DAY_OF_WEEK); |
|
919 |
final static int DAY_OF_WEEK_IN_MONTH_MASK = (1 << DAY_OF_WEEK_IN_MONTH); |
|
920 |
final static int AM_PM_MASK = (1 << AM_PM); |
|
921 |
final static int HOUR_MASK = (1 << HOUR); |
|
922 |
final static int HOUR_OF_DAY_MASK = (1 << HOUR_OF_DAY); |
|
923 |
final static int MINUTE_MASK = (1 << MINUTE); |
|
924 |
final static int SECOND_MASK = (1 << SECOND); |
|
925 |
final static int MILLISECOND_MASK = (1 << MILLISECOND); |
|
926 |
final static int ZONE_OFFSET_MASK = (1 << ZONE_OFFSET); |
|
927 |
final static int DST_OFFSET_MASK = (1 << DST_OFFSET); |
|
928 |
||
929 |
/** |
|
930 |
* Constructs a Calendar with the default time zone |
|
931 |
* and locale. |
|
932 |
* @see TimeZone#getDefault |
|
933 |
*/ |
|
934 |
protected Calendar() |
|
935 |
{ |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
936 |
this(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT)); |
2 | 937 |
sharedZone = true; |
938 |
} |
|
939 |
||
940 |
/** |
|
941 |
* Constructs a calendar with the specified time zone and locale. |
|
942 |
* |
|
943 |
* @param zone the time zone to use |
|
944 |
* @param aLocale the locale for the week data |
|
945 |
*/ |
|
946 |
protected Calendar(TimeZone zone, Locale aLocale) |
|
947 |
{ |
|
948 |
fields = new int[FIELD_COUNT]; |
|
949 |
isSet = new boolean[FIELD_COUNT]; |
|
950 |
stamp = new int[FIELD_COUNT]; |
|
951 |
||
952 |
this.zone = zone; |
|
953 |
setWeekCountData(aLocale); |
|
954 |
} |
|
955 |
||
956 |
/** |
|
957 |
* Gets a calendar using the default time zone and locale. The |
|
958 |
* <code>Calendar</code> returned is based on the current time |
|
959 |
* in the default time zone with the default locale. |
|
960 |
* |
|
961 |
* @return a Calendar. |
|
962 |
*/ |
|
963 |
public static Calendar getInstance() |
|
964 |
{ |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
965 |
Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT)); |
2 | 966 |
cal.sharedZone = true; |
967 |
return cal; |
|
968 |
} |
|
969 |
||
970 |
/** |
|
971 |
* Gets a calendar using the specified time zone and default locale. |
|
972 |
* The <code>Calendar</code> returned is based on the current time |
|
973 |
* in the given time zone with the default locale. |
|
974 |
* |
|
975 |
* @param zone the time zone to use |
|
976 |
* @return a Calendar. |
|
977 |
*/ |
|
978 |
public static Calendar getInstance(TimeZone zone) |
|
979 |
{ |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
980 |
return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT)); |
2 | 981 |
} |
982 |
||
983 |
/** |
|
984 |
* Gets a calendar using the default time zone and specified locale. |
|
985 |
* The <code>Calendar</code> returned is based on the current time |
|
986 |
* in the default time zone with the given locale. |
|
987 |
* |
|
988 |
* @param aLocale the locale for the week data |
|
989 |
* @return a Calendar. |
|
990 |
*/ |
|
991 |
public static Calendar getInstance(Locale aLocale) |
|
992 |
{ |
|
993 |
Calendar cal = createCalendar(TimeZone.getDefaultRef(), aLocale); |
|
994 |
cal.sharedZone = true; |
|
995 |
return cal; |
|
996 |
} |
|
997 |
||
998 |
/** |
|
999 |
* Gets a calendar with the specified time zone and locale. |
|
1000 |
* The <code>Calendar</code> returned is based on the current time |
|
1001 |
* in the given time zone with the given locale. |
|
1002 |
* |
|
1003 |
* @param zone the time zone to use |
|
1004 |
* @param aLocale the locale for the week data |
|
1005 |
* @return a Calendar. |
|
1006 |
*/ |
|
1007 |
public static Calendar getInstance(TimeZone zone, |
|
1008 |
Locale aLocale) |
|
1009 |
{ |
|
1010 |
return createCalendar(zone, aLocale); |
|
1011 |
} |
|
1012 |
||
1013 |
private static Calendar createCalendar(TimeZone zone, |
|
1014 |
Locale aLocale) |
|
1015 |
{ |
|
1016 |
// If the specified locale is a Thai locale, returns a BuddhistCalendar |
|
1017 |
// instance. |
|
1018 |
if ("th".equals(aLocale.getLanguage()) |
|
1019 |
&& ("TH".equals(aLocale.getCountry()))) { |
|
1020 |
return new sun.util.BuddhistCalendar(zone, aLocale); |
|
1021 |
} else if ("JP".equals(aLocale.getVariant()) |
|
1022 |
&& "JP".equals(aLocale.getCountry()) |
|
1023 |
&& "ja".equals(aLocale.getLanguage())) { |
|
1024 |
return new JapaneseImperialCalendar(zone, aLocale); |
|
1025 |
} |
|
1026 |
||
1027 |
// else create the default calendar |
|
1028 |
return new GregorianCalendar(zone, aLocale); |
|
1029 |
} |
|
1030 |
||
1031 |
/** |
|
1032 |
* Returns an array of all locales for which the <code>getInstance</code> |
|
1033 |
* methods of this class can return localized instances. |
|
1034 |
* The array returned must contain at least a <code>Locale</code> |
|
1035 |
* instance equal to {@link java.util.Locale#US Locale.US}. |
|
1036 |
* |
|
1037 |
* @return An array of locales for which localized |
|
1038 |
* <code>Calendar</code> instances are available. |
|
1039 |
*/ |
|
1040 |
public static synchronized Locale[] getAvailableLocales() |
|
1041 |
{ |
|
1042 |
return DateFormat.getAvailableLocales(); |
|
1043 |
} |
|
1044 |
||
1045 |
/** |
|
1046 |
* Converts the current calendar field values in {@link #fields fields[]} |
|
1047 |
* to the millisecond time value |
|
1048 |
* {@link #time}. |
|
1049 |
* |
|
1050 |
* @see #complete() |
|
1051 |
* @see #computeFields() |
|
1052 |
*/ |
|
1053 |
protected abstract void computeTime(); |
|
1054 |
||
1055 |
/** |
|
1056 |
* Converts the current millisecond time value {@link #time} |
|
1057 |
* to calendar field values in {@link #fields fields[]}. |
|
1058 |
* This allows you to sync up the calendar field values with |
|
1059 |
* a new time that is set for the calendar. The time is <em>not</em> |
|
1060 |
* recomputed first; to recompute the time, then the fields, call the |
|
1061 |
* {@link #complete()} method. |
|
1062 |
* |
|
1063 |
* @see #computeTime() |
|
1064 |
*/ |
|
1065 |
protected abstract void computeFields(); |
|
1066 |
||
1067 |
/** |
|
1068 |
* Returns a <code>Date</code> object representing this |
|
1069 |
* <code>Calendar</code>'s time value (millisecond offset from the <a |
|
1070 |
* href="#Epoch">Epoch</a>"). |
|
1071 |
* |
|
1072 |
* @return a <code>Date</code> representing the time value. |
|
1073 |
* @see #setTime(Date) |
|
1074 |
* @see #getTimeInMillis() |
|
1075 |
*/ |
|
1076 |
public final Date getTime() { |
|
1077 |
return new Date(getTimeInMillis()); |
|
1078 |
} |
|
1079 |
||
1080 |
/** |
|
1081 |
* Sets this Calendar's time with the given <code>Date</code>. |
|
1082 |
* <p> |
|
1083 |
* Note: Calling <code>setTime()</code> with |
|
1084 |
* <code>Date(Long.MAX_VALUE)</code> or <code>Date(Long.MIN_VALUE)</code> |
|
1085 |
* may yield incorrect field values from <code>get()</code>. |
|
1086 |
* |
|
1087 |
* @param date the given Date. |
|
1088 |
* @see #getTime() |
|
1089 |
* @see #setTimeInMillis(long) |
|
1090 |
*/ |
|
1091 |
public final void setTime(Date date) { |
|
1092 |
setTimeInMillis(date.getTime()); |
|
1093 |
} |
|
1094 |
||
1095 |
/** |
|
1096 |
* Returns this Calendar's time value in milliseconds. |
|
1097 |
* |
|
1098 |
* @return the current time as UTC milliseconds from the epoch. |
|
1099 |
* @see #getTime() |
|
1100 |
* @see #setTimeInMillis(long) |
|
1101 |
*/ |
|
1102 |
public long getTimeInMillis() { |
|
1103 |
if (!isTimeSet) { |
|
1104 |
updateTime(); |
|
1105 |
} |
|
1106 |
return time; |
|
1107 |
} |
|
1108 |
||
1109 |
/** |
|
1110 |
* Sets this Calendar's current time from the given long value. |
|
1111 |
* |
|
1112 |
* @param millis the new time in UTC milliseconds from the epoch. |
|
1113 |
* @see #setTime(Date) |
|
1114 |
* @see #getTimeInMillis() |
|
1115 |
*/ |
|
1116 |
public void setTimeInMillis(long millis) { |
|
1117 |
// If we don't need to recalculate the calendar field values, |
|
1118 |
// do nothing. |
|
1119 |
if (time == millis && isTimeSet && areFieldsSet && areAllFieldsSet |
|
1120 |
&& (zone instanceof ZoneInfo) && !((ZoneInfo)zone).isDirty()) { |
|
1121 |
return; |
|
1122 |
} |
|
1123 |
time = millis; |
|
1124 |
isTimeSet = true; |
|
1125 |
areFieldsSet = false; |
|
1126 |
computeFields(); |
|
1127 |
areAllFieldsSet = areFieldsSet = true; |
|
1128 |
} |
|
1129 |
||
1130 |
/** |
|
1131 |
* Returns the value of the given calendar field. In lenient mode, |
|
1132 |
* all calendar fields are normalized. In non-lenient mode, all |
|
1133 |
* calendar fields are validated and this method throws an |
|
1134 |
* exception if any calendar fields have out-of-range values. The |
|
1135 |
* normalization and validation are handled by the |
|
1136 |
* {@link #complete()} method, which process is calendar |
|
1137 |
* system dependent. |
|
1138 |
* |
|
1139 |
* @param field the given calendar field. |
|
1140 |
* @return the value for the given calendar field. |
|
1141 |
* @throws ArrayIndexOutOfBoundsException if the specified field is out of range |
|
1142 |
* (<code>field < 0 || field >= FIELD_COUNT</code>). |
|
1143 |
* @see #set(int,int) |
|
1144 |
* @see #complete() |
|
1145 |
*/ |
|
1146 |
public int get(int field) |
|
1147 |
{ |
|
1148 |
complete(); |
|
1149 |
return internalGet(field); |
|
1150 |
} |
|
1151 |
||
1152 |
/** |
|
1153 |
* Returns the value of the given calendar field. This method does |
|
1154 |
* not involve normalization or validation of the field value. |
|
1155 |
* |
|
1156 |
* @param field the given calendar field. |
|
1157 |
* @return the value for the given calendar field. |
|
1158 |
* @see #get(int) |
|
1159 |
*/ |
|
1160 |
protected final int internalGet(int field) |
|
1161 |
{ |
|
1162 |
return fields[field]; |
|
1163 |
} |
|
1164 |
||
1165 |
/** |
|
1166 |
* Sets the value of the given calendar field. This method does |
|
1167 |
* not affect any setting state of the field in this |
|
1168 |
* <code>Calendar</code> instance. |
|
1169 |
* |
|
1170 |
* @throws IndexOutOfBoundsException if the specified field is out of range |
|
1171 |
* (<code>field < 0 || field >= FIELD_COUNT</code>). |
|
1172 |
* @see #areFieldsSet |
|
1173 |
* @see #isTimeSet |
|
1174 |
* @see #areAllFieldsSet |
|
1175 |
* @see #set(int,int) |
|
1176 |
*/ |
|
1177 |
final void internalSet(int field, int value) |
|
1178 |
{ |
|
1179 |
fields[field] = value; |
|
1180 |
} |
|
1181 |
||
1182 |
/** |
|
1183 |
* Sets the given calendar field to the given value. The value is not |
|
1184 |
* interpreted by this method regardless of the leniency mode. |
|
1185 |
* |
|
1186 |
* @param field the given calendar field. |
|
1187 |
* @param value the value to be set for the given calendar field. |
|
1188 |
* @throws ArrayIndexOutOfBoundsException if the specified field is out of range |
|
1189 |
* (<code>field < 0 || field >= FIELD_COUNT</code>). |
|
1190 |
* in non-lenient mode. |
|
1191 |
* @see #set(int,int,int) |
|
1192 |
* @see #set(int,int,int,int,int) |
|
1193 |
* @see #set(int,int,int,int,int,int) |
|
1194 |
* @see #get(int) |
|
1195 |
*/ |
|
1196 |
public void set(int field, int value) |
|
1197 |
{ |
|
1828
26bee4a3a499
6645263: (cal) Calendar throw java.lang.IllegalArgumentException: WEEK_OF_MONTH
peytoia
parents:
2
diff
changeset
|
1198 |
// If the fields are partially normalized, calculate all the |
26bee4a3a499
6645263: (cal) Calendar throw java.lang.IllegalArgumentException: WEEK_OF_MONTH
peytoia
parents:
2
diff
changeset
|
1199 |
// fields before changing any fields. |
26bee4a3a499
6645263: (cal) Calendar throw java.lang.IllegalArgumentException: WEEK_OF_MONTH
peytoia
parents:
2
diff
changeset
|
1200 |
if (areFieldsSet && !areAllFieldsSet) { |
2 | 1201 |
computeFields(); |
1202 |
} |
|
1203 |
internalSet(field, value); |
|
1204 |
isTimeSet = false; |
|
1205 |
areFieldsSet = false; |
|
1206 |
isSet[field] = true; |
|
1207 |
stamp[field] = nextStamp++; |
|
1208 |
if (nextStamp == Integer.MAX_VALUE) { |
|
1209 |
adjustStamp(); |
|
1210 |
} |
|
1211 |
} |
|
1212 |
||
1213 |
/** |
|
1214 |
* Sets the values for the calendar fields <code>YEAR</code>, |
|
1215 |
* <code>MONTH</code>, and <code>DAY_OF_MONTH</code>. |
|
1216 |
* Previous values of other calendar fields are retained. If this is not desired, |
|
1217 |
* call {@link #clear()} first. |
|
1218 |
* |
|
1219 |
* @param year the value used to set the <code>YEAR</code> calendar field. |
|
1220 |
* @param month the value used to set the <code>MONTH</code> calendar field. |
|
1221 |
* Month value is 0-based. e.g., 0 for January. |
|
1222 |
* @param date the value used to set the <code>DAY_OF_MONTH</code> calendar field. |
|
1223 |
* @see #set(int,int) |
|
1224 |
* @see #set(int,int,int,int,int) |
|
1225 |
* @see #set(int,int,int,int,int,int) |
|
1226 |
*/ |
|
1227 |
public final void set(int year, int month, int date) |
|
1228 |
{ |
|
1229 |
set(YEAR, year); |
|
1230 |
set(MONTH, month); |
|
1231 |
set(DATE, date); |
|
1232 |
} |
|
1233 |
||
1234 |
/** |
|
1235 |
* Sets the values for the calendar fields <code>YEAR</code>, |
|
1236 |
* <code>MONTH</code>, <code>DAY_OF_MONTH</code>, |
|
1237 |
* <code>HOUR_OF_DAY</code>, and <code>MINUTE</code>. |
|
1238 |
* Previous values of other fields are retained. If this is not desired, |
|
1239 |
* call {@link #clear()} first. |
|
1240 |
* |
|
1241 |
* @param year the value used to set the <code>YEAR</code> calendar field. |
|
1242 |
* @param month the value used to set the <code>MONTH</code> calendar field. |
|
1243 |
* Month value is 0-based. e.g., 0 for January. |
|
1244 |
* @param date the value used to set the <code>DAY_OF_MONTH</code> calendar field. |
|
1245 |
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field. |
|
1246 |
* @param minute the value used to set the <code>MINUTE</code> calendar field. |
|
1247 |
* @see #set(int,int) |
|
1248 |
* @see #set(int,int,int) |
|
1249 |
* @see #set(int,int,int,int,int,int) |
|
1250 |
*/ |
|
1251 |
public final void set(int year, int month, int date, int hourOfDay, int minute) |
|
1252 |
{ |
|
1253 |
set(YEAR, year); |
|
1254 |
set(MONTH, month); |
|
1255 |
set(DATE, date); |
|
1256 |
set(HOUR_OF_DAY, hourOfDay); |
|
1257 |
set(MINUTE, minute); |
|
1258 |
} |
|
1259 |
||
1260 |
/** |
|
1261 |
* Sets the values for the fields <code>YEAR</code>, <code>MONTH</code>, |
|
1262 |
* <code>DAY_OF_MONTH</code>, <code>HOUR</code>, <code>MINUTE</code>, and |
|
1263 |
* <code>SECOND</code>. |
|
1264 |
* Previous values of other fields are retained. If this is not desired, |
|
1265 |
* call {@link #clear()} first. |
|
1266 |
* |
|
1267 |
* @param year the value used to set the <code>YEAR</code> calendar field. |
|
1268 |
* @param month the value used to set the <code>MONTH</code> calendar field. |
|
1269 |
* Month value is 0-based. e.g., 0 for January. |
|
1270 |
* @param date the value used to set the <code>DAY_OF_MONTH</code> calendar field. |
|
1271 |
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field. |
|
1272 |
* @param minute the value used to set the <code>MINUTE</code> calendar field. |
|
1273 |
* @param second the value used to set the <code>SECOND</code> calendar field. |
|
1274 |
* @see #set(int,int) |
|
1275 |
* @see #set(int,int,int) |
|
1276 |
* @see #set(int,int,int,int,int) |
|
1277 |
*/ |
|
1278 |
public final void set(int year, int month, int date, int hourOfDay, int minute, |
|
1279 |
int second) |
|
1280 |
{ |
|
1281 |
set(YEAR, year); |
|
1282 |
set(MONTH, month); |
|
1283 |
set(DATE, date); |
|
1284 |
set(HOUR_OF_DAY, hourOfDay); |
|
1285 |
set(MINUTE, minute); |
|
1286 |
set(SECOND, second); |
|
1287 |
} |
|
1288 |
||
1289 |
/** |
|
1290 |
* Sets all the calendar field values and the time value |
|
1291 |
* (millisecond offset from the <a href="#Epoch">Epoch</a>) of |
|
1292 |
* this <code>Calendar</code> undefined. This means that {@link |
|
1293 |
* #isSet(int) isSet()} will return <code>false</code> for all the |
|
1294 |
* calendar fields, and the date and time calculations will treat |
|
1295 |
* the fields as if they had never been set. A |
|
1296 |
* <code>Calendar</code> implementation class may use its specific |
|
1297 |
* default field values for date/time calculations. For example, |
|
1298 |
* <code>GregorianCalendar</code> uses 1970 if the |
|
1299 |
* <code>YEAR</code> field value is undefined. |
|
1300 |
* |
|
1301 |
* @see #clear(int) |
|
1302 |
*/ |
|
1303 |
public final void clear() |
|
1304 |
{ |
|
1305 |
for (int i = 0; i < fields.length; ) { |
|
1306 |
stamp[i] = fields[i] = 0; // UNSET == 0 |
|
1307 |
isSet[i++] = false; |
|
1308 |
} |
|
1309 |
areAllFieldsSet = areFieldsSet = false; |
|
1310 |
isTimeSet = false; |
|
1311 |
} |
|
1312 |
||
1313 |
/** |
|
1314 |
* Sets the given calendar field value and the time value |
|
1315 |
* (millisecond offset from the <a href="#Epoch">Epoch</a>) of |
|
1316 |
* this <code>Calendar</code> undefined. This means that {@link |
|
1317 |
* #isSet(int) isSet(field)} will return <code>false</code>, and |
|
1318 |
* the date and time calculations will treat the field as if it |
|
1319 |
* had never been set. A <code>Calendar</code> implementation |
|
1320 |
* class may use the field's specific default value for date and |
|
1321 |
* time calculations. |
|
1322 |
* |
|
1323 |
* <p>The {@link #HOUR_OF_DAY}, {@link #HOUR} and {@link #AM_PM} |
|
1324 |
* fields are handled independently and the <a |
|
1325 |
* href="#time_resolution">the resolution rule for the time of |
|
1326 |
* day</a> is applied. Clearing one of the fields doesn't reset |
|
1327 |
* the hour of day value of this <code>Calendar</code>. Use {@link |
|
1328 |
* #set(int,int) set(Calendar.HOUR_OF_DAY, 0)} to reset the hour |
|
1329 |
* value. |
|
1330 |
* |
|
1331 |
* @param field the calendar field to be cleared. |
|
1332 |
* @see #clear() |
|
1333 |
*/ |
|
1334 |
public final void clear(int field) |
|
1335 |
{ |
|
1336 |
fields[field] = 0; |
|
1337 |
stamp[field] = UNSET; |
|
1338 |
isSet[field] = false; |
|
1339 |
||
1340 |
areAllFieldsSet = areFieldsSet = false; |
|
1341 |
isTimeSet = false; |
|
1342 |
} |
|
1343 |
||
1344 |
/** |
|
1345 |
* Determines if the given calendar field has a value set, |
|
1346 |
* including cases that the value has been set by internal fields |
|
1347 |
* calculations triggered by a <code>get</code> method call. |
|
1348 |
* |
|
1349 |
* @return <code>true</code> if the given calendar field has a value set; |
|
1350 |
* <code>false</code> otherwise. |
|
1351 |
*/ |
|
1352 |
public final boolean isSet(int field) |
|
1353 |
{ |
|
1354 |
return stamp[field] != UNSET; |
|
1355 |
} |
|
1356 |
||
1357 |
/** |
|
1358 |
* Returns the string representation of the calendar |
|
1359 |
* <code>field</code> value in the given <code>style</code> and |
|
1360 |
* <code>locale</code>. If no string representation is |
|
1361 |
* applicable, <code>null</code> is returned. This method calls |
|
1362 |
* {@link Calendar#get(int) get(field)} to get the calendar |
|
1363 |
* <code>field</code> value if the string representation is |
|
1364 |
* applicable to the given calendar <code>field</code>. |
|
1365 |
* |
|
1366 |
* <p>For example, if this <code>Calendar</code> is a |
|
1367 |
* <code>GregorianCalendar</code> and its date is 2005-01-01, then |
|
1368 |
* the string representation of the {@link #MONTH} field would be |
|
1369 |
* "January" in the long style in an English locale or "Jan" in |
|
1370 |
* the short style. However, no string representation would be |
|
1371 |
* available for the {@link #DAY_OF_MONTH} field, and this method |
|
1372 |
* would return <code>null</code>. |
|
1373 |
* |
|
1374 |
* <p>The default implementation supports the calendar fields for |
|
1375 |
* which a {@link DateFormatSymbols} has names in the given |
|
1376 |
* <code>locale</code>. |
|
1377 |
* |
|
1378 |
* @param field |
|
1379 |
* the calendar field for which the string representation |
|
1380 |
* is returned |
|
1381 |
* @param style |
|
1382 |
* the style applied to the string representation; one of |
|
1383 |
* {@link #SHORT} or {@link #LONG}. |
|
1384 |
* @param locale |
|
1385 |
* the locale for the string representation |
|
1386 |
* @return the string representation of the given |
|
1387 |
* <code>field</code> in the given <code>style</code>, or |
|
1388 |
* <code>null</code> if no string representation is |
|
1389 |
* applicable. |
|
1390 |
* @exception IllegalArgumentException |
|
1391 |
* if <code>field</code> or <code>style</code> is invalid, |
|
1392 |
* or if this <code>Calendar</code> is non-lenient and any |
|
1393 |
* of the calendar fields have invalid values |
|
1394 |
* @exception NullPointerException |
|
1395 |
* if <code>locale</code> is null |
|
1396 |
* @since 1.6 |
|
1397 |
*/ |
|
1398 |
public String getDisplayName(int field, int style, Locale locale) { |
|
1399 |
if (!checkDisplayNameParams(field, style, ALL_STYLES, LONG, locale, |
|
1400 |
ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) { |
|
1401 |
return null; |
|
1402 |
} |
|
1403 |
||
1404 |
DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale); |
|
1405 |
String[] strings = getFieldStrings(field, style, symbols); |
|
1406 |
if (strings != null) { |
|
1407 |
int fieldValue = get(field); |
|
1408 |
if (fieldValue < strings.length) { |
|
1409 |
return strings[fieldValue]; |
|
1410 |
} |
|
1411 |
} |
|
1412 |
return null; |
|
1413 |
} |
|
1414 |
||
1415 |
/** |
|
1416 |
* Returns a <code>Map</code> containing all names of the calendar |
|
1417 |
* <code>field</code> in the given <code>style</code> and |
|
1418 |
* <code>locale</code> and their corresponding field values. For |
|
1419 |
* example, if this <code>Calendar</code> is a {@link |
|
1420 |
* GregorianCalendar}, the returned map would contain "Jan" to |
|
1421 |
* {@link #JANUARY}, "Feb" to {@link #FEBRUARY}, and so on, in the |
|
1422 |
* {@linkplain #SHORT short} style in an English locale. |
|
1423 |
* |
|
1424 |
* <p>The values of other calendar fields may be taken into |
|
1425 |
* account to determine a set of display names. For example, if |
|
1426 |
* this <code>Calendar</code> is a lunisolar calendar system and |
|
1427 |
* the year value given by the {@link #YEAR} field has a leap |
|
1428 |
* month, this method would return month names containing the leap |
|
1429 |
* month name, and month names are mapped to their values specific |
|
1430 |
* for the year. |
|
1431 |
* |
|
1432 |
* <p>The default implementation supports display names contained in |
|
1433 |
* a {@link DateFormatSymbols}. For example, if <code>field</code> |
|
1434 |
* is {@link #MONTH} and <code>style</code> is {@link |
|
1435 |
* #ALL_STYLES}, this method returns a <code>Map</code> containing |
|
1436 |
* all strings returned by {@link DateFormatSymbols#getShortMonths()} |
|
1437 |
* and {@link DateFormatSymbols#getMonths()}. |
|
1438 |
* |
|
1439 |
* @param field |
|
1440 |
* the calendar field for which the display names are returned |
|
1441 |
* @param style |
|
1442 |
* the style applied to the display names; one of {@link |
|
1443 |
* #SHORT}, {@link #LONG}, or {@link #ALL_STYLES}. |
|
1444 |
* @param locale |
|
1445 |
* the locale for the display names |
|
1446 |
* @return a <code>Map</code> containing all display names in |
|
1447 |
* <code>style</code> and <code>locale</code> and their |
|
1448 |
* field values, or <code>null</code> if no display names |
|
1449 |
* are defined for <code>field</code> |
|
1450 |
* @exception IllegalArgumentException |
|
1451 |
* if <code>field</code> or <code>style</code> is invalid, |
|
1452 |
* or if this <code>Calendar</code> is non-lenient and any |
|
1453 |
* of the calendar fields have invalid values |
|
1454 |
* @exception NullPointerException |
|
1455 |
* if <code>locale</code> is null |
|
1456 |
* @since 1.6 |
|
1457 |
*/ |
|
1458 |
public Map<String, Integer> getDisplayNames(int field, int style, Locale locale) { |
|
1459 |
if (!checkDisplayNameParams(field, style, ALL_STYLES, LONG, locale, |
|
1460 |
ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) { |
|
1461 |
return null; |
|
1462 |
} |
|
1463 |
||
1464 |
// ALL_STYLES |
|
1465 |
if (style == ALL_STYLES) { |
|
1466 |
Map<String,Integer> shortNames = getDisplayNamesImpl(field, SHORT, locale); |
|
1467 |
if (field == ERA || field == AM_PM) { |
|
1468 |
return shortNames; |
|
1469 |
} |
|
1470 |
Map<String,Integer> longNames = getDisplayNamesImpl(field, LONG, locale); |
|
1471 |
if (shortNames == null) { |
|
1472 |
return longNames; |
|
1473 |
} |
|
1474 |
if (longNames != null) { |
|
1475 |
shortNames.putAll(longNames); |
|
1476 |
} |
|
1477 |
return shortNames; |
|
1478 |
} |
|
1479 |
||
1480 |
// SHORT or LONG |
|
1481 |
return getDisplayNamesImpl(field, style, locale); |
|
1482 |
} |
|
1483 |
||
1484 |
private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) { |
|
1485 |
DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale); |
|
1486 |
String[] strings = getFieldStrings(field, style, symbols); |
|
1487 |
if (strings != null) { |
|
1488 |
Map<String,Integer> names = new HashMap<String,Integer>(); |
|
1489 |
for (int i = 0; i < strings.length; i++) { |
|
1490 |
if (strings[i].length() == 0) { |
|
1491 |
continue; |
|
1492 |
} |
|
1493 |
names.put(strings[i], i); |
|
1494 |
} |
|
1495 |
return names; |
|
1496 |
} |
|
1497 |
return null; |
|
1498 |
} |
|
1499 |
||
1500 |
boolean checkDisplayNameParams(int field, int style, int minStyle, int maxStyle, |
|
1501 |
Locale locale, int fieldMask) { |
|
1502 |
if (field < 0 || field >= fields.length || |
|
1503 |
style < minStyle || style > maxStyle) { |
|
1504 |
throw new IllegalArgumentException(); |
|
1505 |
} |
|
1506 |
if (locale == null) { |
|
1507 |
throw new NullPointerException(); |
|
1508 |
} |
|
1509 |
return isFieldSet(fieldMask, field); |
|
1510 |
} |
|
1511 |
||
1512 |
private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) { |
|
1513 |
String[] strings = null; |
|
1514 |
switch (field) { |
|
1515 |
case ERA: |
|
1516 |
strings = symbols.getEras(); |
|
1517 |
break; |
|
1518 |
||
1519 |
case MONTH: |
|
1520 |
strings = (style == LONG) ? symbols.getMonths() : symbols.getShortMonths(); |
|
1521 |
break; |
|
1522 |
||
1523 |
case DAY_OF_WEEK: |
|
1524 |
strings = (style == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays(); |
|
1525 |
break; |
|
1526 |
||
1527 |
case AM_PM: |
|
1528 |
strings = symbols.getAmPmStrings(); |
|
1529 |
break; |
|
1530 |
} |
|
1531 |
return strings; |
|
1532 |
} |
|
1533 |
||
1534 |
/** |
|
1535 |
* Fills in any unset fields in the calendar fields. First, the {@link |
|
1536 |
* #computeTime()} method is called if the time value (millisecond offset |
|
1537 |
* from the <a href="#Epoch">Epoch</a>) has not been calculated from |
|
1538 |
* calendar field values. Then, the {@link #computeFields()} method is |
|
1539 |
* called to calculate all calendar field values. |
|
1540 |
*/ |
|
1541 |
protected void complete() |
|
1542 |
{ |
|
1543 |
if (!isTimeSet) |
|
1544 |
updateTime(); |
|
1545 |
if (!areFieldsSet || !areAllFieldsSet) { |
|
1546 |
computeFields(); // fills in unset fields |
|
1547 |
areAllFieldsSet = areFieldsSet = true; |
|
1548 |
} |
|
1549 |
} |
|
1550 |
||
1551 |
/** |
|
1552 |
* Returns whether the value of the specified calendar field has been set |
|
1553 |
* externally by calling one of the setter methods rather than by the |
|
1554 |
* internal time calculation. |
|
1555 |
* |
|
1556 |
* @return <code>true</code> if the field has been set externally, |
|
1557 |
* <code>false</code> otherwise. |
|
1558 |
* @exception IndexOutOfBoundsException if the specified |
|
1559 |
* <code>field</code> is out of range |
|
1560 |
* (<code>field < 0 || field >= FIELD_COUNT</code>). |
|
1561 |
* @see #selectFields() |
|
1562 |
* @see #setFieldsComputed(int) |
|
1563 |
*/ |
|
1564 |
final boolean isExternallySet(int field) { |
|
1565 |
return stamp[field] >= MINIMUM_USER_STAMP; |
|
1566 |
} |
|
1567 |
||
1568 |
/** |
|
1569 |
* Returns a field mask (bit mask) indicating all calendar fields that |
|
1570 |
* have the state of externally or internally set. |
|
1571 |
* |
|
1572 |
* @return a bit mask indicating set state fields |
|
1573 |
*/ |
|
1574 |
final int getSetStateFields() { |
|
1575 |
int mask = 0; |
|
1576 |
for (int i = 0; i < fields.length; i++) { |
|
1577 |
if (stamp[i] != UNSET) { |
|
1578 |
mask |= 1 << i; |
|
1579 |
} |
|
1580 |
} |
|
1581 |
return mask; |
|
1582 |
} |
|
1583 |
||
1584 |
/** |
|
1585 |
* Sets the state of the specified calendar fields to |
|
1586 |
* <em>computed</em>. This state means that the specified calendar fields |
|
1587 |
* have valid values that have been set by internal time calculation |
|
1588 |
* rather than by calling one of the setter methods. |
|
1589 |
* |
|
1590 |
* @param fieldMask the field to be marked as computed. |
|
1591 |
* @exception IndexOutOfBoundsException if the specified |
|
1592 |
* <code>field</code> is out of range |
|
1593 |
* (<code>field < 0 || field >= FIELD_COUNT</code>). |
|
1594 |
* @see #isExternallySet(int) |
|
1595 |
* @see #selectFields() |
|
1596 |
*/ |
|
1597 |
final void setFieldsComputed(int fieldMask) { |
|
1598 |
if (fieldMask == ALL_FIELDS) { |
|
1599 |
for (int i = 0; i < fields.length; i++) { |
|
1600 |
stamp[i] = COMPUTED; |
|
1601 |
isSet[i] = true; |
|
1602 |
} |
|
1603 |
areFieldsSet = areAllFieldsSet = true; |
|
1604 |
} else { |
|
1605 |
for (int i = 0; i < fields.length; i++) { |
|
1606 |
if ((fieldMask & 1) == 1) { |
|
1607 |
stamp[i] = COMPUTED; |
|
1608 |
isSet[i] = true; |
|
1609 |
} else { |
|
1610 |
if (areAllFieldsSet && !isSet[i]) { |
|
1611 |
areAllFieldsSet = false; |
|
1612 |
} |
|
1613 |
} |
|
1614 |
fieldMask >>>= 1; |
|
1615 |
} |
|
1616 |
} |
|
1617 |
} |
|
1618 |
||
1619 |
/** |
|
1620 |
* Sets the state of the calendar fields that are <em>not</em> specified |
|
1621 |
* by <code>fieldMask</code> to <em>unset</em>. If <code>fieldMask</code> |
|
1622 |
* specifies all the calendar fields, then the state of this |
|
1623 |
* <code>Calendar</code> becomes that all the calendar fields are in sync |
|
1624 |
* with the time value (millisecond offset from the Epoch). |
|
1625 |
* |
|
1626 |
* @param fieldMask the field mask indicating which calendar fields are in |
|
1627 |
* sync with the time value. |
|
1628 |
* @exception IndexOutOfBoundsException if the specified |
|
1629 |
* <code>field</code> is out of range |
|
1630 |
* (<code>field < 0 || field >= FIELD_COUNT</code>). |
|
1631 |
* @see #isExternallySet(int) |
|
1632 |
* @see #selectFields() |
|
1633 |
*/ |
|
1634 |
final void setFieldsNormalized(int fieldMask) { |
|
1635 |
if (fieldMask != ALL_FIELDS) { |
|
1636 |
for (int i = 0; i < fields.length; i++) { |
|
1637 |
if ((fieldMask & 1) == 0) { |
|
1638 |
stamp[i] = fields[i] = 0; // UNSET == 0 |
|
1639 |
isSet[i] = false; |
|
1640 |
} |
|
1641 |
fieldMask >>= 1; |
|
1642 |
} |
|
1643 |
} |
|
1644 |
||
1645 |
// Some or all of the fields are in sync with the |
|
1646 |
// milliseconds, but the stamp values are not normalized yet. |
|
1647 |
areFieldsSet = true; |
|
1648 |
areAllFieldsSet = false; |
|
1649 |
} |
|
1650 |
||
1651 |
/** |
|
1652 |
* Returns whether the calendar fields are partially in sync with the time |
|
1653 |
* value or fully in sync but not stamp values are not normalized yet. |
|
1654 |
*/ |
|
1655 |
final boolean isPartiallyNormalized() { |
|
1656 |
return areFieldsSet && !areAllFieldsSet; |
|
1657 |
} |
|
1658 |
||
1659 |
/** |
|
1660 |
* Returns whether the calendar fields are fully in sync with the time |
|
1661 |
* value. |
|
1662 |
*/ |
|
1663 |
final boolean isFullyNormalized() { |
|
1664 |
return areFieldsSet && areAllFieldsSet; |
|
1665 |
} |
|
1666 |
||
1667 |
/** |
|
1668 |
* Marks this Calendar as not sync'd. |
|
1669 |
*/ |
|
1670 |
final void setUnnormalized() { |
|
1671 |
areFieldsSet = areAllFieldsSet = false; |
|
1672 |
} |
|
1673 |
||
1674 |
/** |
|
1675 |
* Returns whether the specified <code>field</code> is on in the |
|
1676 |
* <code>fieldMask</code>. |
|
1677 |
*/ |
|
1678 |
static final boolean isFieldSet(int fieldMask, int field) { |
|
1679 |
return (fieldMask & (1 << field)) != 0; |
|
1680 |
} |
|
1681 |
||
1682 |
/** |
|
1683 |
* Returns a field mask indicating which calendar field values |
|
1684 |
* to be used to calculate the time value. The calendar fields are |
|
1685 |
* returned as a bit mask, each bit of which corresponds to a field, i.e., |
|
1686 |
* the mask value of <code>field</code> is <code>(1 << |
|
1687 |
* field)</code>. For example, 0x26 represents the <code>YEAR</code>, |
|
1688 |
* <code>MONTH</code>, and <code>DAY_OF_MONTH</code> fields (i.e., 0x26 is |
|
1689 |
* equal to |
|
1690 |
* <code>(1<<YEAR)|(1<<MONTH)|(1<<DAY_OF_MONTH))</code>. |
|
1691 |
* |
|
1692 |
* <p>This method supports the calendar fields resolution as described in |
|
1693 |
* the class description. If the bit mask for a given field is on and its |
|
1694 |
* field has not been set (i.e., <code>isSet(field)</code> is |
|
1695 |
* <code>false</code>), then the default value of the field has to be |
|
1696 |
* used, which case means that the field has been selected because the |
|
1697 |
* selected combination involves the field. |
|
1698 |
* |
|
1699 |
* @return a bit mask of selected fields |
|
1700 |
* @see #isExternallySet(int) |
|
1701 |
* @see #setInternallySetState(int) |
|
1702 |
*/ |
|
1703 |
final int selectFields() { |
|
1704 |
// This implementation has been taken from the GregorianCalendar class. |
|
1705 |
||
1706 |
// The YEAR field must always be used regardless of its SET |
|
1707 |
// state because YEAR is a mandatory field to determine the date |
|
1708 |
// and the default value (EPOCH_YEAR) may change through the |
|
1709 |
// normalization process. |
|
1710 |
int fieldMask = YEAR_MASK; |
|
1711 |
||
1712 |
if (stamp[ERA] != UNSET) { |
|
1713 |
fieldMask |= ERA_MASK; |
|
1714 |
} |
|
1715 |
// Find the most recent group of fields specifying the day within |
|
1716 |
// the year. These may be any of the following combinations: |
|
1717 |
// MONTH + DAY_OF_MONTH |
|
1718 |
// MONTH + WEEK_OF_MONTH + DAY_OF_WEEK |
|
1719 |
// MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK |
|
1720 |
// DAY_OF_YEAR |
|
1721 |
// WEEK_OF_YEAR + DAY_OF_WEEK |
|
1722 |
// We look for the most recent of the fields in each group to determine |
|
1723 |
// the age of the group. For groups involving a week-related field such |
|
1724 |
// as WEEK_OF_MONTH, DAY_OF_WEEK_IN_MONTH, or WEEK_OF_YEAR, both the |
|
1725 |
// week-related field and the DAY_OF_WEEK must be set for the group as a |
|
1726 |
// whole to be considered. (See bug 4153860 - liu 7/24/98.) |
|
1727 |
int dowStamp = stamp[DAY_OF_WEEK]; |
|
1728 |
int monthStamp = stamp[MONTH]; |
|
1729 |
int domStamp = stamp[DAY_OF_MONTH]; |
|
1730 |
int womStamp = aggregateStamp(stamp[WEEK_OF_MONTH], dowStamp); |
|
1731 |
int dowimStamp = aggregateStamp(stamp[DAY_OF_WEEK_IN_MONTH], dowStamp); |
|
1732 |
int doyStamp = stamp[DAY_OF_YEAR]; |
|
1733 |
int woyStamp = aggregateStamp(stamp[WEEK_OF_YEAR], dowStamp); |
|
1734 |
||
1735 |
int bestStamp = domStamp; |
|
1736 |
if (womStamp > bestStamp) { |
|
1737 |
bestStamp = womStamp; |
|
1738 |
} |
|
1739 |
if (dowimStamp > bestStamp) { |
|
1740 |
bestStamp = dowimStamp; |
|
1741 |
} |
|
1742 |
if (doyStamp > bestStamp) { |
|
1743 |
bestStamp = doyStamp; |
|
1744 |
} |
|
1745 |
if (woyStamp > bestStamp) { |
|
1746 |
bestStamp = woyStamp; |
|
1747 |
} |
|
1748 |
||
1749 |
/* No complete combination exists. Look for WEEK_OF_MONTH, |
|
1750 |
* DAY_OF_WEEK_IN_MONTH, or WEEK_OF_YEAR alone. Treat DAY_OF_WEEK alone |
|
1751 |
* as DAY_OF_WEEK_IN_MONTH. |
|
1752 |
*/ |
|
1753 |
if (bestStamp == UNSET) { |
|
1754 |
womStamp = stamp[WEEK_OF_MONTH]; |
|
1755 |
dowimStamp = Math.max(stamp[DAY_OF_WEEK_IN_MONTH], dowStamp); |
|
1756 |
woyStamp = stamp[WEEK_OF_YEAR]; |
|
1757 |
bestStamp = Math.max(Math.max(womStamp, dowimStamp), woyStamp); |
|
1758 |
||
1759 |
/* Treat MONTH alone or no fields at all as DAY_OF_MONTH. This may |
|
1760 |
* result in bestStamp = domStamp = UNSET if no fields are set, |
|
1761 |
* which indicates DAY_OF_MONTH. |
|
1762 |
*/ |
|
1763 |
if (bestStamp == UNSET) { |
|
1764 |
bestStamp = domStamp = monthStamp; |
|
1765 |
} |
|
1766 |
} |
|
1767 |
||
1768 |
if (bestStamp == domStamp || |
|
1769 |
(bestStamp == womStamp && stamp[WEEK_OF_MONTH] >= stamp[WEEK_OF_YEAR]) || |
|
1770 |
(bestStamp == dowimStamp && stamp[DAY_OF_WEEK_IN_MONTH] >= stamp[WEEK_OF_YEAR])) { |
|
1771 |
fieldMask |= MONTH_MASK; |
|
1772 |
if (bestStamp == domStamp) { |
|
1773 |
fieldMask |= DAY_OF_MONTH_MASK; |
|
1774 |
} else { |
|
1775 |
assert (bestStamp == womStamp || bestStamp == dowimStamp); |
|
1776 |
if (dowStamp != UNSET) { |
|
1777 |
fieldMask |= DAY_OF_WEEK_MASK; |
|
1778 |
} |
|
1779 |
if (womStamp == dowimStamp) { |
|
1780 |
// When they are equal, give the priority to |
|
1781 |
// WEEK_OF_MONTH for compatibility. |
|
1782 |
if (stamp[WEEK_OF_MONTH] >= stamp[DAY_OF_WEEK_IN_MONTH]) { |
|
1783 |
fieldMask |= WEEK_OF_MONTH_MASK; |
|
1784 |
} else { |
|
1785 |
fieldMask |= DAY_OF_WEEK_IN_MONTH_MASK; |
|
1786 |
} |
|
1787 |
} else { |
|
1788 |
if (bestStamp == womStamp) { |
|
1789 |
fieldMask |= WEEK_OF_MONTH_MASK; |
|
1790 |
} else { |
|
1791 |
assert (bestStamp == dowimStamp); |
|
1792 |
if (stamp[DAY_OF_WEEK_IN_MONTH] != UNSET) { |
|
1793 |
fieldMask |= DAY_OF_WEEK_IN_MONTH_MASK; |
|
1794 |
} |
|
1795 |
} |
|
1796 |
} |
|
1797 |
} |
|
1798 |
} else { |
|
1799 |
assert (bestStamp == doyStamp || bestStamp == woyStamp || |
|
1800 |
bestStamp == UNSET); |
|
1801 |
if (bestStamp == doyStamp) { |
|
1802 |
fieldMask |= DAY_OF_YEAR_MASK; |
|
1803 |
} else { |
|
1804 |
assert (bestStamp == woyStamp); |
|
1805 |
if (dowStamp != UNSET) { |
|
1806 |
fieldMask |= DAY_OF_WEEK_MASK; |
|
1807 |
} |
|
1808 |
fieldMask |= WEEK_OF_YEAR_MASK; |
|
1809 |
} |
|
1810 |
} |
|
1811 |
||
1812 |
// Find the best set of fields specifying the time of day. There |
|
1813 |
// are only two possibilities here; the HOUR_OF_DAY or the |
|
1814 |
// AM_PM and the HOUR. |
|
1815 |
int hourOfDayStamp = stamp[HOUR_OF_DAY]; |
|
1816 |
int hourStamp = aggregateStamp(stamp[HOUR], stamp[AM_PM]); |
|
1817 |
bestStamp = (hourStamp > hourOfDayStamp) ? hourStamp : hourOfDayStamp; |
|
1818 |
||
1819 |
// if bestStamp is still UNSET, then take HOUR or AM_PM. (See 4846659) |
|
1820 |
if (bestStamp == UNSET) { |
|
1821 |
bestStamp = Math.max(stamp[HOUR], stamp[AM_PM]); |
|
1822 |
} |
|
1823 |
||
1824 |
// Hours |
|
1825 |
if (bestStamp != UNSET) { |
|
1826 |
if (bestStamp == hourOfDayStamp) { |
|
1827 |
fieldMask |= HOUR_OF_DAY_MASK; |
|
1828 |
} else { |
|
1829 |
fieldMask |= HOUR_MASK; |
|
1830 |
if (stamp[AM_PM] != UNSET) { |
|
1831 |
fieldMask |= AM_PM_MASK; |
|
1832 |
} |
|
1833 |
} |
|
1834 |
} |
|
1835 |
if (stamp[MINUTE] != UNSET) { |
|
1836 |
fieldMask |= MINUTE_MASK; |
|
1837 |
} |
|
1838 |
if (stamp[SECOND] != UNSET) { |
|
1839 |
fieldMask |= SECOND_MASK; |
|
1840 |
} |
|
1841 |
if (stamp[MILLISECOND] != UNSET) { |
|
1842 |
fieldMask |= MILLISECOND_MASK; |
|
1843 |
} |
|
1844 |
if (stamp[ZONE_OFFSET] >= MINIMUM_USER_STAMP) { |
|
1845 |
fieldMask |= ZONE_OFFSET_MASK; |
|
1846 |
} |
|
1847 |
if (stamp[DST_OFFSET] >= MINIMUM_USER_STAMP) { |
|
1848 |
fieldMask |= DST_OFFSET_MASK; |
|
1849 |
} |
|
1850 |
||
1851 |
return fieldMask; |
|
1852 |
} |
|
1853 |
||
1854 |
/** |
|
1855 |
* Returns the pseudo-time-stamp for two fields, given their |
|
1856 |
* individual pseudo-time-stamps. If either of the fields |
|
1857 |
* is unset, then the aggregate is unset. Otherwise, the |
|
1858 |
* aggregate is the later of the two stamps. |
|
1859 |
*/ |
|
1860 |
private static final int aggregateStamp(int stamp_a, int stamp_b) { |
|
1861 |
if (stamp_a == UNSET || stamp_b == UNSET) { |
|
1862 |
return UNSET; |
|
1863 |
} |
|
1864 |
return (stamp_a > stamp_b) ? stamp_a : stamp_b; |
|
1865 |
} |
|
1866 |
||
1867 |
/** |
|
1868 |
* Compares this <code>Calendar</code> to the specified |
|
1869 |
* <code>Object</code>. The result is <code>true</code> if and only if |
|
1870 |
* the argument is a <code>Calendar</code> object of the same calendar |
|
1871 |
* system that represents the same time value (millisecond offset from the |
|
1872 |
* <a href="#Epoch">Epoch</a>) under the same |
|
1873 |
* <code>Calendar</code> parameters as this object. |
|
1874 |
* |
|
1875 |
* <p>The <code>Calendar</code> parameters are the values represented |
|
1876 |
* by the <code>isLenient</code>, <code>getFirstDayOfWeek</code>, |
|
1877 |
* <code>getMinimalDaysInFirstWeek</code> and <code>getTimeZone</code> |
|
1878 |
* methods. If there is any difference in those parameters |
|
1879 |
* between the two <code>Calendar</code>s, this method returns |
|
1880 |
* <code>false</code>. |
|
1881 |
* |
|
1882 |
* <p>Use the {@link #compareTo(Calendar) compareTo} method to |
|
1883 |
* compare only the time values. |
|
1884 |
* |
|
1885 |
* @param obj the object to compare with. |
|
1886 |
* @return <code>true</code> if this object is equal to <code>obj</code>; |
|
1887 |
* <code>false</code> otherwise. |
|
1888 |
*/ |
|
1889 |
public boolean equals(Object obj) { |
|
1890 |
if (this == obj) |
|
1891 |
return true; |
|
1892 |
try { |
|
1893 |
Calendar that = (Calendar)obj; |
|
1894 |
return compareTo(getMillisOf(that)) == 0 && |
|
1895 |
lenient == that.lenient && |
|
1896 |
firstDayOfWeek == that.firstDayOfWeek && |
|
1897 |
minimalDaysInFirstWeek == that.minimalDaysInFirstWeek && |
|
1898 |
zone.equals(that.zone); |
|
1899 |
} catch (Exception e) { |
|
1900 |
// Note: GregorianCalendar.computeTime throws |
|
1901 |
// IllegalArgumentException if the ERA value is invalid |
|
1902 |
// even it's in lenient mode. |
|
1903 |
} |
|
1904 |
return false; |
|
1905 |
} |
|
1906 |
||
1907 |
/** |
|
1908 |
* Returns a hash code for this calendar. |
|
1909 |
* |
|
1910 |
* @return a hash code value for this object. |
|
1911 |
* @since 1.2 |
|
1912 |
*/ |
|
1913 |
public int hashCode() { |
|
1914 |
// 'otheritems' represents the hash code for the previous versions. |
|
1915 |
int otheritems = (lenient ? 1 : 0) |
|
1916 |
| (firstDayOfWeek << 1) |
|
1917 |
| (minimalDaysInFirstWeek << 4) |
|
1918 |
| (zone.hashCode() << 7); |
|
1919 |
long t = getMillisOf(this); |
|
1920 |
return (int) t ^ (int)(t >> 32) ^ otheritems; |
|
1921 |
} |
|
1922 |
||
1923 |
/** |
|
1924 |
* Returns whether this <code>Calendar</code> represents a time |
|
1925 |
* before the time represented by the specified |
|
1926 |
* <code>Object</code>. This method is equivalent to: |
|
1927 |
* <pre><blockquote> |
|
1928 |
* compareTo(when) < 0 |
|
1929 |
* </blockquote></pre> |
|
1930 |
* if and only if <code>when</code> is a <code>Calendar</code> |
|
1931 |
* instance. Otherwise, the method returns <code>false</code>. |
|
1932 |
* |
|
1933 |
* @param when the <code>Object</code> to be compared |
|
1934 |
* @return <code>true</code> if the time of this |
|
1935 |
* <code>Calendar</code> is before the time represented by |
|
1936 |
* <code>when</code>; <code>false</code> otherwise. |
|
1937 |
* @see #compareTo(Calendar) |
|
1938 |
*/ |
|
1939 |
public boolean before(Object when) { |
|
1940 |
return when instanceof Calendar |
|
1941 |
&& compareTo((Calendar)when) < 0; |
|
1942 |
} |
|
1943 |
||
1944 |
/** |
|
1945 |
* Returns whether this <code>Calendar</code> represents a time |
|
1946 |
* after the time represented by the specified |
|
1947 |
* <code>Object</code>. This method is equivalent to: |
|
1948 |
* <pre><blockquote> |
|
1949 |
* compareTo(when) > 0 |
|
1950 |
* </blockquote></pre> |
|
1951 |
* if and only if <code>when</code> is a <code>Calendar</code> |
|
1952 |
* instance. Otherwise, the method returns <code>false</code>. |
|
1953 |
* |
|
1954 |
* @param when the <code>Object</code> to be compared |
|
1955 |
* @return <code>true</code> if the time of this <code>Calendar</code> is |
|
1956 |
* after the time represented by <code>when</code>; <code>false</code> |
|
1957 |
* otherwise. |
|
1958 |
* @see #compareTo(Calendar) |
|
1959 |
*/ |
|
1960 |
public boolean after(Object when) { |
|
1961 |
return when instanceof Calendar |
|
1962 |
&& compareTo((Calendar)when) > 0; |
|
1963 |
} |
|
1964 |
||
1965 |
/** |
|
1966 |
* Compares the time values (millisecond offsets from the <a |
|
1967 |
* href="#Epoch">Epoch</a>) represented by two |
|
1968 |
* <code>Calendar</code> objects. |
|
1969 |
* |
|
1970 |
* @param anotherCalendar the <code>Calendar</code> to be compared. |
|
1971 |
* @return the value <code>0</code> if the time represented by the argument |
|
1972 |
* is equal to the time represented by this <code>Calendar</code>; a value |
|
1973 |
* less than <code>0</code> if the time of this <code>Calendar</code> is |
|
1974 |
* before the time represented by the argument; and a value greater than |
|
1975 |
* <code>0</code> if the time of this <code>Calendar</code> is after the |
|
1976 |
* time represented by the argument. |
|
1977 |
* @exception NullPointerException if the specified <code>Calendar</code> is |
|
1978 |
* <code>null</code>. |
|
1979 |
* @exception IllegalArgumentException if the time value of the |
|
1980 |
* specified <code>Calendar</code> object can't be obtained due to |
|
1981 |
* any invalid calendar values. |
|
1982 |
* @since 1.5 |
|
1983 |
*/ |
|
1984 |
public int compareTo(Calendar anotherCalendar) { |
|
1985 |
return compareTo(getMillisOf(anotherCalendar)); |
|
1986 |
} |
|
1987 |
||
1988 |
/** |
|
1989 |
* Adds or subtracts the specified amount of time to the given calendar field, |
|
1990 |
* based on the calendar's rules. For example, to subtract 5 days from |
|
1991 |
* the current time of the calendar, you can achieve it by calling: |
|
1992 |
* <p><code>add(Calendar.DAY_OF_MONTH, -5)</code>. |
|
1993 |
* |
|
1994 |
* @param field the calendar field. |
|
1995 |
* @param amount the amount of date or time to be added to the field. |
|
1996 |
* @see #roll(int,int) |
|
1997 |
* @see #set(int,int) |
|
1998 |
*/ |
|
1999 |
abstract public void add(int field, int amount); |
|
2000 |
||
2001 |
/** |
|
2002 |
* Adds or subtracts (up/down) a single unit of time on the given time |
|
2003 |
* field without changing larger fields. For example, to roll the current |
|
2004 |
* date up by one day, you can achieve it by calling: |
|
2005 |
* <p>roll(Calendar.DATE, true). |
|
2006 |
* When rolling on the year or Calendar.YEAR field, it will roll the year |
|
2007 |
* value in the range between 1 and the value returned by calling |
|
2008 |
* <code>getMaximum(Calendar.YEAR)</code>. |
|
2009 |
* When rolling on the month or Calendar.MONTH field, other fields like |
|
2010 |
* date might conflict and, need to be changed. For instance, |
|
2011 |
* rolling the month on the date 01/31/96 will result in 02/29/96. |
|
2012 |
* When rolling on the hour-in-day or Calendar.HOUR_OF_DAY field, it will |
|
2013 |
* roll the hour value in the range between 0 and 23, which is zero-based. |
|
2014 |
* |
|
2015 |
* @param field the time field. |
|
2016 |
* @param up indicates if the value of the specified time field is to be |
|
2017 |
* rolled up or rolled down. Use true if rolling up, false otherwise. |
|
2018 |
* @see Calendar#add(int,int) |
|
2019 |
* @see Calendar#set(int,int) |
|
2020 |
*/ |
|
2021 |
abstract public void roll(int field, boolean up); |
|
2022 |
||
2023 |
/** |
|
2024 |
* Adds the specified (signed) amount to the specified calendar field |
|
2025 |
* without changing larger fields. A negative amount means to roll |
|
2026 |
* down. |
|
2027 |
* |
|
2028 |
* <p>NOTE: This default implementation on <code>Calendar</code> just repeatedly calls the |
|
2029 |
* version of {@link #roll(int,boolean) roll()} that rolls by one unit. This may not |
|
2030 |
* always do the right thing. For example, if the <code>DAY_OF_MONTH</code> field is 31, |
|
2031 |
* rolling through February will leave it set to 28. The <code>GregorianCalendar</code> |
|
2032 |
* version of this function takes care of this problem. Other subclasses |
|
2033 |
* should also provide overrides of this function that do the right thing. |
|
2034 |
* |
|
2035 |
* @param field the calendar field. |
|
2036 |
* @param amount the signed amount to add to the calendar <code>field</code>. |
|
2037 |
* @since 1.2 |
|
2038 |
* @see #roll(int,boolean) |
|
2039 |
* @see #add(int,int) |
|
2040 |
* @see #set(int,int) |
|
2041 |
*/ |
|
2042 |
public void roll(int field, int amount) |
|
2043 |
{ |
|
2044 |
while (amount > 0) { |
|
2045 |
roll(field, true); |
|
2046 |
amount--; |
|
2047 |
} |
|
2048 |
while (amount < 0) { |
|
2049 |
roll(field, false); |
|
2050 |
amount++; |
|
2051 |
} |
|
2052 |
} |
|
2053 |
||
2054 |
/** |
|
2055 |
* Sets the time zone with the given time zone value. |
|
2056 |
* |
|
2057 |
* @param value the given time zone. |
|
2058 |
*/ |
|
2059 |
public void setTimeZone(TimeZone value) |
|
2060 |
{ |
|
2061 |
zone = value; |
|
2062 |
sharedZone = false; |
|
2063 |
/* Recompute the fields from the time using the new zone. This also |
|
2064 |
* works if isTimeSet is false (after a call to set()). In that case |
|
2065 |
* the time will be computed from the fields using the new zone, then |
|
2066 |
* the fields will get recomputed from that. Consider the sequence of |
|
2067 |
* calls: cal.setTimeZone(EST); cal.set(HOUR, 1); cal.setTimeZone(PST). |
|
2068 |
* Is cal set to 1 o'clock EST or 1 o'clock PST? Answer: PST. More |
|
2069 |
* generally, a call to setTimeZone() affects calls to set() BEFORE AND |
|
2070 |
* AFTER it up to the next call to complete(). |
|
2071 |
*/ |
|
2072 |
areAllFieldsSet = areFieldsSet = false; |
|
2073 |
} |
|
2074 |
||
2075 |
/** |
|
2076 |
* Gets the time zone. |
|
2077 |
* |
|
2078 |
* @return the time zone object associated with this calendar. |
|
2079 |
*/ |
|
2080 |
public TimeZone getTimeZone() |
|
2081 |
{ |
|
2082 |
// If the TimeZone object is shared by other Calendar instances, then |
|
2083 |
// create a clone. |
|
2084 |
if (sharedZone) { |
|
2085 |
zone = (TimeZone) zone.clone(); |
|
2086 |
sharedZone = false; |
|
2087 |
} |
|
2088 |
return zone; |
|
2089 |
} |
|
2090 |
||
2091 |
/** |
|
2092 |
* Returns the time zone (without cloning). |
|
2093 |
*/ |
|
2094 |
TimeZone getZone() { |
|
2095 |
return zone; |
|
2096 |
} |
|
2097 |
||
2098 |
/** |
|
2099 |
* Sets the sharedZone flag to <code>shared</code>. |
|
2100 |
*/ |
|
2101 |
void setZoneShared(boolean shared) { |
|
2102 |
sharedZone = shared; |
|
2103 |
} |
|
2104 |
||
2105 |
/** |
|
2106 |
* Specifies whether or not date/time interpretation is to be lenient. With |
|
2107 |
* lenient interpretation, a date such as "February 942, 1996" will be |
|
2108 |
* treated as being equivalent to the 941st day after February 1, 1996. |
|
2109 |
* With strict (non-lenient) interpretation, such dates will cause an exception to be |
|
2110 |
* thrown. The default is lenient. |
|
2111 |
* |
|
2112 |
* @param lenient <code>true</code> if the lenient mode is to be turned |
|
2113 |
* on; <code>false</code> if it is to be turned off. |
|
2114 |
* @see #isLenient() |
|
2115 |
* @see java.text.DateFormat#setLenient |
|
2116 |
*/ |
|
2117 |
public void setLenient(boolean lenient) |
|
2118 |
{ |
|
2119 |
this.lenient = lenient; |
|
2120 |
} |
|
2121 |
||
2122 |
/** |
|
2123 |
* Tells whether date/time interpretation is to be lenient. |
|
2124 |
* |
|
2125 |
* @return <code>true</code> if the interpretation mode of this calendar is lenient; |
|
2126 |
* <code>false</code> otherwise. |
|
2127 |
* @see #setLenient(boolean) |
|
2128 |
*/ |
|
2129 |
public boolean isLenient() |
|
2130 |
{ |
|
2131 |
return lenient; |
|
2132 |
} |
|
2133 |
||
2134 |
/** |
|
2135 |
* Sets what the first day of the week is; e.g., <code>SUNDAY</code> in the U.S., |
|
2136 |
* <code>MONDAY</code> in France. |
|
2137 |
* |
|
2138 |
* @param value the given first day of the week. |
|
2139 |
* @see #getFirstDayOfWeek() |
|
2140 |
* @see #getMinimalDaysInFirstWeek() |
|
2141 |
*/ |
|
2142 |
public void setFirstDayOfWeek(int value) |
|
2143 |
{ |
|
2144 |
if (firstDayOfWeek == value) { |
|
2145 |
return; |
|
2146 |
} |
|
2147 |
firstDayOfWeek = value; |
|
2148 |
invalidateWeekFields(); |
|
2149 |
} |
|
2150 |
||
2151 |
/** |
|
2152 |
* Gets what the first day of the week is; e.g., <code>SUNDAY</code> in the U.S., |
|
2153 |
* <code>MONDAY</code> in France. |
|
2154 |
* |
|
2155 |
* @return the first day of the week. |
|
2156 |
* @see #setFirstDayOfWeek(int) |
|
2157 |
* @see #getMinimalDaysInFirstWeek() |
|
2158 |
*/ |
|
2159 |
public int getFirstDayOfWeek() |
|
2160 |
{ |
|
2161 |
return firstDayOfWeek; |
|
2162 |
} |
|
2163 |
||
2164 |
/** |
|
2165 |
* Sets what the minimal days required in the first week of the year are; |
|
2166 |
* For example, if the first week is defined as one that contains the first |
|
2167 |
* day of the first month of a year, call this method with value 1. If it |
|
2168 |
* must be a full week, use value 7. |
|
2169 |
* |
|
2170 |
* @param value the given minimal days required in the first week |
|
2171 |
* of the year. |
|
2172 |
* @see #getMinimalDaysInFirstWeek() |
|
2173 |
*/ |
|
2174 |
public void setMinimalDaysInFirstWeek(int value) |
|
2175 |
{ |
|
2176 |
if (minimalDaysInFirstWeek == value) { |
|
2177 |
return; |
|
2178 |
} |
|
2179 |
minimalDaysInFirstWeek = value; |
|
2180 |
invalidateWeekFields(); |
|
2181 |
} |
|
2182 |
||
2183 |
/** |
|
2184 |
* Gets what the minimal days required in the first week of the year are; |
|
2185 |
* e.g., if the first week is defined as one that contains the first day |
|
2186 |
* of the first month of a year, this method returns 1. If |
|
2187 |
* the minimal days required must be a full week, this method |
|
2188 |
* returns 7. |
|
2189 |
* |
|
2190 |
* @return the minimal days required in the first week of the year. |
|
2191 |
* @see #setMinimalDaysInFirstWeek(int) |
|
2192 |
*/ |
|
2193 |
public int getMinimalDaysInFirstWeek() |
|
2194 |
{ |
|
2195 |
return minimalDaysInFirstWeek; |
|
2196 |
} |
|
2197 |
||
2198 |
/** |
|
2199 |
* Returns the minimum value for the given calendar field of this |
|
2200 |
* <code>Calendar</code> instance. The minimum value is defined as |
|
2201 |
* the smallest value returned by the {@link #get(int) get} method |
|
2202 |
* for any possible time value. The minimum value depends on |
|
2203 |
* calendar system specific parameters of the instance. |
|
2204 |
* |
|
2205 |
* @param field the calendar field. |
|
2206 |
* @return the minimum value for the given calendar field. |
|
2207 |
* @see #getMaximum(int) |
|
2208 |
* @see #getGreatestMinimum(int) |
|
2209 |
* @see #getLeastMaximum(int) |
|
2210 |
* @see #getActualMinimum(int) |
|
2211 |
* @see #getActualMaximum(int) |
|
2212 |
*/ |
|
2213 |
abstract public int getMinimum(int field); |
|
2214 |
||
2215 |
/** |
|
2216 |
* Returns the maximum value for the given calendar field of this |
|
2217 |
* <code>Calendar</code> instance. The maximum value is defined as |
|
2218 |
* the largest value returned by the {@link #get(int) get} method |
|
2219 |
* for any possible time value. The maximum value depends on |
|
2220 |
* calendar system specific parameters of the instance. |
|
2221 |
* |
|
2222 |
* @param field the calendar field. |
|
2223 |
* @return the maximum value for the given calendar field. |
|
2224 |
* @see #getMinimum(int) |
|
2225 |
* @see #getGreatestMinimum(int) |
|
2226 |
* @see #getLeastMaximum(int) |
|
2227 |
* @see #getActualMinimum(int) |
|
2228 |
* @see #getActualMaximum(int) |
|
2229 |
*/ |
|
2230 |
abstract public int getMaximum(int field); |
|
2231 |
||
2232 |
/** |
|
2233 |
* Returns the highest minimum value for the given calendar field |
|
2234 |
* of this <code>Calendar</code> instance. The highest minimum |
|
2235 |
* value is defined as the largest value returned by {@link |
|
2236 |
* #getActualMinimum(int)} for any possible time value. The |
|
2237 |
* greatest minimum value depends on calendar system specific |
|
2238 |
* parameters of the instance. |
|
2239 |
* |
|
2240 |
* @param field the calendar field. |
|
2241 |
* @return the highest minimum value for the given calendar field. |
|
2242 |
* @see #getMinimum(int) |
|
2243 |
* @see #getMaximum(int) |
|
2244 |
* @see #getLeastMaximum(int) |
|
2245 |
* @see #getActualMinimum(int) |
|
2246 |
* @see #getActualMaximum(int) |
|
2247 |
*/ |
|
2248 |
abstract public int getGreatestMinimum(int field); |
|
2249 |
||
2250 |
/** |
|
2251 |
* Returns the lowest maximum value for the given calendar field |
|
2252 |
* of this <code>Calendar</code> instance. The lowest maximum |
|
2253 |
* value is defined as the smallest value returned by {@link |
|
2254 |
* #getActualMaximum(int)} for any possible time value. The least |
|
2255 |
* maximum value depends on calendar system specific parameters of |
|
2256 |
* the instance. For example, a <code>Calendar</code> for the |
|
2257 |
* Gregorian calendar system returns 28 for the |
|
2258 |
* <code>DAY_OF_MONTH</code> field, because the 28th is the last |
|
2259 |
* day of the shortest month of this calendar, February in a |
|
2260 |
* common year. |
|
2261 |
* |
|
2262 |
* @param field the calendar field. |
|
2263 |
* @return the lowest maximum value for the given calendar field. |
|
2264 |
* @see #getMinimum(int) |
|
2265 |
* @see #getMaximum(int) |
|
2266 |
* @see #getGreatestMinimum(int) |
|
2267 |
* @see #getActualMinimum(int) |
|
2268 |
* @see #getActualMaximum(int) |
|
2269 |
*/ |
|
2270 |
abstract public int getLeastMaximum(int field); |
|
2271 |
||
2272 |
/** |
|
2273 |
* Returns the minimum value that the specified calendar field |
|
2274 |
* could have, given the time value of this <code>Calendar</code>. |
|
2275 |
* |
|
2276 |
* <p>The default implementation of this method uses an iterative |
|
2277 |
* algorithm to determine the actual minimum value for the |
|
2278 |
* calendar field. Subclasses should, if possible, override this |
|
2279 |
* with a more efficient implementation - in many cases, they can |
|
2280 |
* simply return <code>getMinimum()</code>. |
|
2281 |
* |
|
2282 |
* @param field the calendar field |
|
2283 |
* @return the minimum of the given calendar field for the time |
|
2284 |
* value of this <code>Calendar</code> |
|
2285 |
* @see #getMinimum(int) |
|
2286 |
* @see #getMaximum(int) |
|
2287 |
* @see #getGreatestMinimum(int) |
|
2288 |
* @see #getLeastMaximum(int) |
|
2289 |
* @see #getActualMaximum(int) |
|
2290 |
* @since 1.2 |
|
2291 |
*/ |
|
2292 |
public int getActualMinimum(int field) { |
|
2293 |
int fieldValue = getGreatestMinimum(field); |
|
2294 |
int endValue = getMinimum(field); |
|
2295 |
||
2296 |
// if we know that the minimum value is always the same, just return it |
|
2297 |
if (fieldValue == endValue) { |
|
2298 |
return fieldValue; |
|
2299 |
} |
|
2300 |
||
2301 |
// clone the calendar so we don't mess with the real one, and set it to |
|
2302 |
// accept anything for the field values |
|
2303 |
Calendar work = (Calendar)this.clone(); |
|
2304 |
work.setLenient(true); |
|
2305 |
||
2306 |
// now try each value from getLeastMaximum() to getMaximum() one by one until |
|
2307 |
// we get a value that normalizes to another value. The last value that |
|
2308 |
// normalizes to itself is the actual minimum for the current date |
|
2309 |
int result = fieldValue; |
|
2310 |
||
2311 |
do { |
|
2312 |
work.set(field, fieldValue); |
|
2313 |
if (work.get(field) != fieldValue) { |
|
2314 |
break; |
|
2315 |
} else { |
|
2316 |
result = fieldValue; |
|
2317 |
fieldValue--; |
|
2318 |
} |
|
2319 |
} while (fieldValue >= endValue); |
|
2320 |
||
2321 |
return result; |
|
2322 |
} |
|
2323 |
||
2324 |
/** |
|
2325 |
* Returns the maximum value that the specified calendar field |
|
2326 |
* could have, given the time value of this |
|
2327 |
* <code>Calendar</code>. For example, the actual maximum value of |
|
2328 |
* the <code>MONTH</code> field is 12 in some years, and 13 in |
|
2329 |
* other years in the Hebrew calendar system. |
|
2330 |
* |
|
2331 |
* <p>The default implementation of this method uses an iterative |
|
2332 |
* algorithm to determine the actual maximum value for the |
|
2333 |
* calendar field. Subclasses should, if possible, override this |
|
2334 |
* with a more efficient implementation. |
|
2335 |
* |
|
2336 |
* @param field the calendar field |
|
2337 |
* @return the maximum of the given calendar field for the time |
|
2338 |
* value of this <code>Calendar</code> |
|
2339 |
* @see #getMinimum(int) |
|
2340 |
* @see #getMaximum(int) |
|
2341 |
* @see #getGreatestMinimum(int) |
|
2342 |
* @see #getLeastMaximum(int) |
|
2343 |
* @see #getActualMinimum(int) |
|
2344 |
* @since 1.2 |
|
2345 |
*/ |
|
2346 |
public int getActualMaximum(int field) { |
|
2347 |
int fieldValue = getLeastMaximum(field); |
|
2348 |
int endValue = getMaximum(field); |
|
2349 |
||
2350 |
// if we know that the maximum value is always the same, just return it. |
|
2351 |
if (fieldValue == endValue) { |
|
2352 |
return fieldValue; |
|
2353 |
} |
|
2354 |
||
2355 |
// clone the calendar so we don't mess with the real one, and set it to |
|
2356 |
// accept anything for the field values. |
|
2357 |
Calendar work = (Calendar)this.clone(); |
|
2358 |
work.setLenient(true); |
|
2359 |
||
2360 |
// if we're counting weeks, set the day of the week to Sunday. We know the |
|
2361 |
// last week of a month or year will contain the first day of the week. |
|
2362 |
if (field == WEEK_OF_YEAR || field == WEEK_OF_MONTH) |
|
2363 |
work.set(DAY_OF_WEEK, firstDayOfWeek); |
|
2364 |
||
2365 |
// now try each value from getLeastMaximum() to getMaximum() one by one until |
|
2366 |
// we get a value that normalizes to another value. The last value that |
|
2367 |
// normalizes to itself is the actual maximum for the current date |
|
2368 |
int result = fieldValue; |
|
2369 |
||
2370 |
do { |
|
2371 |
work.set(field, fieldValue); |
|
2372 |
if (work.get(field) != fieldValue) { |
|
2373 |
break; |
|
2374 |
} else { |
|
2375 |
result = fieldValue; |
|
2376 |
fieldValue++; |
|
2377 |
} |
|
2378 |
} while (fieldValue <= endValue); |
|
2379 |
||
2380 |
return result; |
|
2381 |
} |
|
2382 |
||
2383 |
/** |
|
2384 |
* Creates and returns a copy of this object. |
|
2385 |
* |
|
2386 |
* @return a copy of this object. |
|
2387 |
*/ |
|
2388 |
public Object clone() |
|
2389 |
{ |
|
2390 |
try { |
|
2391 |
Calendar other = (Calendar) super.clone(); |
|
2392 |
||
2393 |
other.fields = new int[FIELD_COUNT]; |
|
2394 |
other.isSet = new boolean[FIELD_COUNT]; |
|
2395 |
other.stamp = new int[FIELD_COUNT]; |
|
2396 |
for (int i = 0; i < FIELD_COUNT; i++) { |
|
2397 |
other.fields[i] = fields[i]; |
|
2398 |
other.stamp[i] = stamp[i]; |
|
2399 |
other.isSet[i] = isSet[i]; |
|
2400 |
} |
|
2401 |
other.zone = (TimeZone) zone.clone(); |
|
2402 |
return other; |
|
2403 |
} |
|
2404 |
catch (CloneNotSupportedException e) { |
|
2405 |
// this shouldn't happen, since we are Cloneable |
|
2406 |
throw new InternalError(); |
|
2407 |
} |
|
2408 |
} |
|
2409 |
||
2410 |
private static final String[] FIELD_NAME = { |
|
2411 |
"ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH", |
|
2412 |
"DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", |
|
2413 |
"HOUR_OF_DAY", "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET", |
|
2414 |
"DST_OFFSET" |
|
2415 |
}; |
|
2416 |
||
2417 |
/** |
|
2418 |
* Returns the name of the specified calendar field. |
|
2419 |
* |
|
2420 |
* @param field the calendar field |
|
2421 |
* @return the calendar field name |
|
2422 |
* @exception IndexOutOfBoundsException if <code>field</code> is negative, |
|
2423 |
* equal to or greater then <code>FIELD_COUNT</code>. |
|
2424 |
*/ |
|
2425 |
static final String getFieldName(int field) { |
|
2426 |
return FIELD_NAME[field]; |
|
2427 |
} |
|
2428 |
||
2429 |
/** |
|
2430 |
* Return a string representation of this calendar. This method |
|
2431 |
* is intended to be used only for debugging purposes, and the |
|
2432 |
* format of the returned string may vary between implementations. |
|
2433 |
* The returned string may be empty but may not be <code>null</code>. |
|
2434 |
* |
|
2435 |
* @return a string representation of this calendar. |
|
2436 |
*/ |
|
2437 |
public String toString() { |
|
2438 |
// NOTE: BuddhistCalendar.toString() interprets the string |
|
2439 |
// produced by this method so that the Gregorian year number |
|
2440 |
// is substituted by its B.E. year value. It relies on |
|
2441 |
// "...,YEAR=<year>,..." or "...,YEAR=?,...". |
|
2442 |
StringBuilder buffer = new StringBuilder(800); |
|
2443 |
buffer.append(getClass().getName()).append('['); |
|
2444 |
appendValue(buffer, "time", isTimeSet, time); |
|
2445 |
buffer.append(",areFieldsSet=").append(areFieldsSet); |
|
2446 |
buffer.append(",areAllFieldsSet=").append(areAllFieldsSet); |
|
2447 |
buffer.append(",lenient=").append(lenient); |
|
2448 |
buffer.append(",zone=").append(zone); |
|
2449 |
appendValue(buffer, ",firstDayOfWeek", true, (long) firstDayOfWeek); |
|
2450 |
appendValue(buffer, ",minimalDaysInFirstWeek", true, (long) minimalDaysInFirstWeek); |
|
2451 |
for (int i = 0; i < FIELD_COUNT; ++i) { |
|
2452 |
buffer.append(','); |
|
2453 |
appendValue(buffer, FIELD_NAME[i], isSet(i), (long) fields[i]); |
|
2454 |
} |
|
2455 |
buffer.append(']'); |
|
2456 |
return buffer.toString(); |
|
2457 |
} |
|
2458 |
||
2459 |
// =======================privates=============================== |
|
2460 |
||
2461 |
private static final void appendValue(StringBuilder sb, String item, boolean valid, long value) { |
|
2462 |
sb.append(item).append('='); |
|
2463 |
if (valid) { |
|
2464 |
sb.append(value); |
|
2465 |
} else { |
|
2466 |
sb.append('?'); |
|
2467 |
} |
|
2468 |
} |
|
2469 |
||
2470 |
/** |
|
2471 |
* Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent. |
|
2472 |
* They are used to figure out the week count for a specific date for |
|
2473 |
* a given locale. These must be set when a Calendar is constructed. |
|
2474 |
* @param desiredLocale the given locale. |
|
2475 |
*/ |
|
2476 |
private void setWeekCountData(Locale desiredLocale) |
|
2477 |
{ |
|
2478 |
/* try to get the Locale data from the cache */ |
|
2479 |
int[] data = cachedLocaleData.get(desiredLocale); |
|
2480 |
if (data == null) { /* cache miss */ |
|
2481 |
ResourceBundle bundle = LocaleData.getCalendarData(desiredLocale); |
|
2482 |
data = new int[2]; |
|
2483 |
data[0] = Integer.parseInt(bundle.getString("firstDayOfWeek")); |
|
2484 |
data[1] = Integer.parseInt(bundle.getString("minimalDaysInFirstWeek")); |
|
2485 |
cachedLocaleData.put(desiredLocale, data); |
|
2486 |
} |
|
2487 |
firstDayOfWeek = data[0]; |
|
2488 |
minimalDaysInFirstWeek = data[1]; |
|
2489 |
} |
|
2490 |
||
2491 |
/** |
|
2492 |
* Recomputes the time and updates the status fields isTimeSet |
|
2493 |
* and areFieldsSet. Callers should check isTimeSet and only |
|
2494 |
* call this method if isTimeSet is false. |
|
2495 |
*/ |
|
2496 |
private void updateTime() { |
|
2497 |
computeTime(); |
|
2498 |
// The areFieldsSet and areAllFieldsSet values are no longer |
|
2499 |
// controlled here (as of 1.5). |
|
2500 |
isTimeSet = true; |
|
2501 |
} |
|
2502 |
||
2503 |
private int compareTo(long t) { |
|
2504 |
long thisTime = getMillisOf(this); |
|
2505 |
return (thisTime > t) ? 1 : (thisTime == t) ? 0 : -1; |
|
2506 |
} |
|
2507 |
||
2508 |
private static final long getMillisOf(Calendar calendar) { |
|
2509 |
if (calendar.isTimeSet) { |
|
2510 |
return calendar.time; |
|
2511 |
} |
|
2512 |
Calendar cal = (Calendar) calendar.clone(); |
|
2513 |
cal.setLenient(true); |
|
2514 |
return cal.getTimeInMillis(); |
|
2515 |
} |
|
2516 |
||
2517 |
/** |
|
2518 |
* Adjusts the stamp[] values before nextStamp overflow. nextStamp |
|
2519 |
* is set to the next stamp value upon the return. |
|
2520 |
*/ |
|
2521 |
private final void adjustStamp() { |
|
2522 |
int max = MINIMUM_USER_STAMP; |
|
2523 |
int newStamp = MINIMUM_USER_STAMP; |
|
2524 |
||
2525 |
for (;;) { |
|
2526 |
int min = Integer.MAX_VALUE; |
|
2527 |
for (int i = 0; i < stamp.length; i++) { |
|
2528 |
int v = stamp[i]; |
|
2529 |
if (v >= newStamp && min > v) { |
|
2530 |
min = v; |
|
2531 |
} |
|
2532 |
if (max < v) { |
|
2533 |
max = v; |
|
2534 |
} |
|
2535 |
} |
|
2536 |
if (max != min && min == Integer.MAX_VALUE) { |
|
2537 |
break; |
|
2538 |
} |
|
2539 |
for (int i = 0; i < stamp.length; i++) { |
|
2540 |
if (stamp[i] == min) { |
|
2541 |
stamp[i] = newStamp; |
|
2542 |
} |
|
2543 |
} |
|
2544 |
newStamp++; |
|
2545 |
if (min == max) { |
|
2546 |
break; |
|
2547 |
} |
|
2548 |
} |
|
2549 |
nextStamp = newStamp; |
|
2550 |
} |
|
2551 |
||
2552 |
/** |
|
2553 |
* Sets the WEEK_OF_MONTH and WEEK_OF_YEAR fields to new values with the |
|
2554 |
* new parameter value if they have been calculated internally. |
|
2555 |
*/ |
|
2556 |
private void invalidateWeekFields() |
|
2557 |
{ |
|
2558 |
if (stamp[WEEK_OF_MONTH] != COMPUTED && |
|
2559 |
stamp[WEEK_OF_YEAR] != COMPUTED) { |
|
2560 |
return; |
|
2561 |
} |
|
2562 |
||
2563 |
// We have to check the new values of these fields after changing |
|
2564 |
// firstDayOfWeek and/or minimalDaysInFirstWeek. If the field values |
|
2565 |
// have been changed, then set the new values. (4822110) |
|
2566 |
Calendar cal = (Calendar) clone(); |
|
2567 |
cal.setLenient(true); |
|
2568 |
cal.clear(WEEK_OF_MONTH); |
|
2569 |
cal.clear(WEEK_OF_YEAR); |
|
2570 |
||
2571 |
if (stamp[WEEK_OF_MONTH] == COMPUTED) { |
|
2572 |
int weekOfMonth = cal.get(WEEK_OF_MONTH); |
|
2573 |
if (fields[WEEK_OF_MONTH] != weekOfMonth) { |
|
2574 |
fields[WEEK_OF_MONTH] = weekOfMonth; |
|
2575 |
} |
|
2576 |
} |
|
2577 |
||
2578 |
if (stamp[WEEK_OF_YEAR] == COMPUTED) { |
|
2579 |
int weekOfYear = cal.get(WEEK_OF_YEAR); |
|
2580 |
if (fields[WEEK_OF_YEAR] != weekOfYear) { |
|
2581 |
fields[WEEK_OF_YEAR] = weekOfYear; |
|
2582 |
} |
|
2583 |
} |
|
2584 |
} |
|
2585 |
||
2586 |
/** |
|
2587 |
* Save the state of this object to a stream (i.e., serialize it). |
|
2588 |
* |
|
2589 |
* Ideally, <code>Calendar</code> would only write out its state data and |
|
2590 |
* the current time, and not write any field data out, such as |
|
2591 |
* <code>fields[]</code>, <code>isTimeSet</code>, <code>areFieldsSet</code>, |
|
2592 |
* and <code>isSet[]</code>. <code>nextStamp</code> also should not be part |
|
2593 |
* of the persistent state. Unfortunately, this didn't happen before JDK 1.1 |
|
2594 |
* shipped. To be compatible with JDK 1.1, we will always have to write out |
|
2595 |
* the field values and state flags. However, <code>nextStamp</code> can be |
|
2596 |
* removed from the serialization stream; this will probably happen in the |
|
2597 |
* near future. |
|
2598 |
*/ |
|
2599 |
private void writeObject(ObjectOutputStream stream) |
|
2600 |
throws IOException |
|
2601 |
{ |
|
2602 |
// Try to compute the time correctly, for the future (stream |
|
2603 |
// version 2) in which we don't write out fields[] or isSet[]. |
|
2604 |
if (!isTimeSet) { |
|
2605 |
try { |
|
2606 |
updateTime(); |
|
2607 |
} |
|
2608 |
catch (IllegalArgumentException e) {} |
|
2609 |
} |
|
2610 |
||
2611 |
// If this Calendar has a ZoneInfo, save it and set a |
|
2612 |
// SimpleTimeZone equivalent (as a single DST schedule) for |
|
2613 |
// backward compatibility. |
|
2614 |
TimeZone savedZone = null; |
|
2615 |
if (zone instanceof ZoneInfo) { |
|
2616 |
SimpleTimeZone stz = ((ZoneInfo)zone).getLastRuleInstance(); |
|
2617 |
if (stz == null) { |
|
2618 |
stz = new SimpleTimeZone(zone.getRawOffset(), zone.getID()); |
|
2619 |
} |
|
2620 |
savedZone = zone; |
|
2621 |
zone = stz; |
|
2622 |
} |
|
2623 |
||
2624 |
// Write out the 1.1 FCS object. |
|
2625 |
stream.defaultWriteObject(); |
|
2626 |
||
2627 |
// Write out the ZoneInfo object |
|
2628 |
// 4802409: we write out even if it is null, a temporary workaround |
|
2629 |
// the real fix for bug 4844924 in corba-iiop |
|
2630 |
stream.writeObject(savedZone); |
|
2631 |
if (savedZone != null) { |
|
2632 |
zone = savedZone; |
|
2633 |
} |
|
2634 |
} |
|
2635 |
||
2599
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2636 |
private static class CalendarAccessControlContext { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2637 |
private static final AccessControlContext INSTANCE; |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2638 |
static { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2639 |
RuntimePermission perm = new RuntimePermission("accessClassInPackage.sun.util.calendar"); |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2640 |
PermissionCollection perms = perm.newPermissionCollection(); |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2641 |
perms.add(perm); |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2642 |
INSTANCE = new AccessControlContext(new ProtectionDomain[] { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2643 |
new ProtectionDomain(null, perms) |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2644 |
}); |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2645 |
} |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2646 |
} |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2647 |
|
2 | 2648 |
/** |
2649 |
* Reconstitutes this object from a stream (i.e., deserialize it). |
|
2650 |
*/ |
|
2651 |
private void readObject(ObjectInputStream stream) |
|
2652 |
throws IOException, ClassNotFoundException |
|
2653 |
{ |
|
2654 |
final ObjectInputStream input = stream; |
|
2655 |
input.defaultReadObject(); |
|
2656 |
||
2657 |
stamp = new int[FIELD_COUNT]; |
|
2658 |
||
2659 |
// Starting with version 2 (not implemented yet), we expect that |
|
2660 |
// fields[], isSet[], isTimeSet, and areFieldsSet may not be |
|
2661 |
// streamed out anymore. We expect 'time' to be correct. |
|
2662 |
if (serialVersionOnStream >= 2) |
|
2663 |
{ |
|
2664 |
isTimeSet = true; |
|
2665 |
if (fields == null) fields = new int[FIELD_COUNT]; |
|
2666 |
if (isSet == null) isSet = new boolean[FIELD_COUNT]; |
|
2667 |
} |
|
2668 |
else if (serialVersionOnStream >= 0) |
|
2669 |
{ |
|
2670 |
for (int i=0; i<FIELD_COUNT; ++i) |
|
2671 |
stamp[i] = isSet[i] ? COMPUTED : UNSET; |
|
2672 |
} |
|
2673 |
||
2674 |
serialVersionOnStream = currentSerialVersion; |
|
2675 |
||
2676 |
// If there's a ZoneInfo object, use it for zone. |
|
2599
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2677 |
ZoneInfo zi = null; |
2 | 2678 |
try { |
2599
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2679 |
zi = AccessController.doPrivileged( |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2680 |
new PrivilegedExceptionAction<ZoneInfo>() { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2681 |
public ZoneInfo run() throws Exception { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2682 |
return (ZoneInfo) input.readObject(); |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2683 |
} |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2684 |
}, |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2685 |
CalendarAccessControlContext.INSTANCE); |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2686 |
} catch (PrivilegedActionException pae) { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2687 |
Exception e = pae.getException(); |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2688 |
if (!(e instanceof OptionalDataException)) { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2689 |
if (e instanceof RuntimeException) { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2690 |
throw (RuntimeException) e; |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2691 |
} else if (e instanceof IOException) { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2692 |
throw (IOException) e; |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2693 |
} else if (e instanceof ClassNotFoundException) { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2694 |
throw (ClassNotFoundException) e; |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2695 |
} |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2696 |
throw new RuntimeException(e); |
2 | 2697 |
} |
2599
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2698 |
} |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2699 |
if (zi != null) { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2700 |
zone = zi; |
2 | 2701 |
} |
2702 |
||
2703 |
// If the deserialized object has a SimpleTimeZone, try to |
|
2704 |
// replace it with a ZoneInfo equivalent (as of 1.4) in order |
|
2705 |
// to be compatible with the SimpleTimeZone-based |
|
2706 |
// implementation as much as possible. |
|
2707 |
if (zone instanceof SimpleTimeZone) { |
|
2708 |
String id = zone.getID(); |
|
2599
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2709 |
TimeZone tz = TimeZone.getTimeZone(id); |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2710 |
if (tz != null && tz.hasSameRules(zone) && tz.getID().equals(id)) { |
593d7b0502e2
6734167: Calendar.readObject allows elevation of privileges
okutsu
parents:
2
diff
changeset
|
2711 |
zone = tz; |
2 | 2712 |
} |
2713 |
} |
|
2714 |
} |
|
2715 |
} |