1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/util/Calendar.java Thu Oct 03 15:40:35 2013 +0200
1.3 @@ -0,0 +1,2824 @@
1.4 +/*
1.5 + * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +/*
1.30 + * (C) Copyright Taligent, Inc. 1996-1998 - All Rights Reserved
1.31 + * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
1.32 + *
1.33 + * The original version of this source code and documentation is copyrighted
1.34 + * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
1.35 + * materials are provided under terms of a License Agreement between Taligent
1.36 + * and Sun. This technology is protected by multiple US and International
1.37 + * patents. This notice and attribution to Taligent may not be removed.
1.38 + * Taligent is a registered trademark of Taligent, Inc.
1.39 + *
1.40 + */
1.41 +
1.42 +package java.util;
1.43 +
1.44 +import java.io.IOException;
1.45 +import java.io.ObjectInputStream;
1.46 +import java.io.ObjectOutputStream;
1.47 +import java.io.OptionalDataException;
1.48 +import java.io.Serializable;
1.49 +import java.security.AccessControlContext;
1.50 +import java.security.AccessController;
1.51 +import java.security.PermissionCollection;
1.52 +import java.security.PrivilegedActionException;
1.53 +import java.security.PrivilegedExceptionAction;
1.54 +import java.security.ProtectionDomain;
1.55 +import java.text.DateFormat;
1.56 +import java.text.DateFormatSymbols;
1.57 +import java.util.concurrent.ConcurrentHashMap;
1.58 +import java.util.concurrent.ConcurrentMap;
1.59 +import sun.util.BuddhistCalendar;
1.60 +import sun.util.calendar.ZoneInfo;
1.61 +import sun.util.resources.LocaleData;
1.62 +
1.63 +/**
1.64 + * The <code>Calendar</code> class is an abstract class that provides methods
1.65 + * for converting between a specific instant in time and a set of {@link
1.66 + * #fields calendar fields} such as <code>YEAR</code>, <code>MONTH</code>,
1.67 + * <code>DAY_OF_MONTH</code>, <code>HOUR</code>, and so on, and for
1.68 + * manipulating the calendar fields, such as getting the date of the next
1.69 + * week. An instant in time can be represented by a millisecond value that is
1.70 + * an offset from the <a name="Epoch"><em>Epoch</em></a>, January 1, 1970
1.71 + * 00:00:00.000 GMT (Gregorian).
1.72 + *
1.73 + * <p>The class also provides additional fields and methods for
1.74 + * implementing a concrete calendar system outside the package. Those
1.75 + * fields and methods are defined as <code>protected</code>.
1.76 + *
1.77 + * <p>
1.78 + * Like other locale-sensitive classes, <code>Calendar</code> provides a
1.79 + * class method, <code>getInstance</code>, for getting a generally useful
1.80 + * object of this type. <code>Calendar</code>'s <code>getInstance</code> method
1.81 + * returns a <code>Calendar</code> object whose
1.82 + * calendar fields have been initialized with the current date and time:
1.83 + * <blockquote>
1.84 + * <pre>
1.85 + * Calendar rightNow = Calendar.getInstance();
1.86 + * </pre>
1.87 + * </blockquote>
1.88 + *
1.89 + * <p>A <code>Calendar</code> object can produce all the calendar field values
1.90 + * needed to implement the date-time formatting for a particular language and
1.91 + * calendar style (for example, Japanese-Gregorian, Japanese-Traditional).
1.92 + * <code>Calendar</code> defines the range of values returned by
1.93 + * certain calendar fields, as well as their meaning. For example,
1.94 + * the first month of the calendar system has value <code>MONTH ==
1.95 + * JANUARY</code> for all calendars. Other values are defined by the
1.96 + * concrete subclass, such as <code>ERA</code>. See individual field
1.97 + * documentation and subclass documentation for details.
1.98 + *
1.99 + * <h4>Getting and Setting Calendar Field Values</h4>
1.100 + *
1.101 + * <p>The calendar field values can be set by calling the <code>set</code>
1.102 + * methods. Any field values set in a <code>Calendar</code> will not be
1.103 + * interpreted until it needs to calculate its time value (milliseconds from
1.104 + * the Epoch) or values of the calendar fields. Calling the
1.105 + * <code>get</code>, <code>getTimeInMillis</code>, <code>getTime</code>,
1.106 + * <code>add</code> and <code>roll</code> involves such calculation.
1.107 + *
1.108 + * <h4>Leniency</h4>
1.109 + *
1.110 + * <p><code>Calendar</code> has two modes for interpreting the calendar
1.111 + * fields, <em>lenient</em> and <em>non-lenient</em>. When a
1.112 + * <code>Calendar</code> is in lenient mode, it accepts a wider range of
1.113 + * calendar field values than it produces. When a <code>Calendar</code>
1.114 + * recomputes calendar field values for return by <code>get()</code>, all of
1.115 + * the calendar fields are normalized. For example, a lenient
1.116 + * <code>GregorianCalendar</code> interprets <code>MONTH == JANUARY</code>,
1.117 + * <code>DAY_OF_MONTH == 32</code> as February 1.
1.118 +
1.119 + * <p>When a <code>Calendar</code> is in non-lenient mode, it throws an
1.120 + * exception if there is any inconsistency in its calendar fields. For
1.121 + * example, a <code>GregorianCalendar</code> always produces
1.122 + * <code>DAY_OF_MONTH</code> values between 1 and the length of the month. A
1.123 + * non-lenient <code>GregorianCalendar</code> throws an exception upon
1.124 + * calculating its time or calendar field values if any out-of-range field
1.125 + * value has been set.
1.126 + *
1.127 + * <h4><a name="first_week">First Week</a></h4>
1.128 + *
1.129 + * <code>Calendar</code> defines a locale-specific seven day week using two
1.130 + * parameters: the first day of the week and the minimal days in first week
1.131 + * (from 1 to 7). These numbers are taken from the locale resource data when a
1.132 + * <code>Calendar</code> is constructed. They may also be specified explicitly
1.133 + * through the methods for setting their values.
1.134 + *
1.135 + * <p>When setting or getting the <code>WEEK_OF_MONTH</code> or
1.136 + * <code>WEEK_OF_YEAR</code> fields, <code>Calendar</code> must determine the
1.137 + * first week of the month or year as a reference point. The first week of a
1.138 + * month or year is defined as the earliest seven day period beginning on
1.139 + * <code>getFirstDayOfWeek()</code> and containing at least
1.140 + * <code>getMinimalDaysInFirstWeek()</code> days of that month or year. Weeks
1.141 + * numbered ..., -1, 0 precede the first week; weeks numbered 2, 3,... follow
1.142 + * it. Note that the normalized numbering returned by <code>get()</code> may be
1.143 + * different. For example, a specific <code>Calendar</code> subclass may
1.144 + * designate the week before week 1 of a year as week <code><i>n</i></code> of
1.145 + * the previous year.
1.146 + *
1.147 + * <h4>Calendar Fields Resolution</h4>
1.148 + *
1.149 + * When computing a date and time from the calendar fields, there
1.150 + * may be insufficient information for the computation (such as only
1.151 + * year and month with no day of month), or there may be inconsistent
1.152 + * information (such as Tuesday, July 15, 1996 (Gregorian) -- July 15,
1.153 + * 1996 is actually a Monday). <code>Calendar</code> will resolve
1.154 + * calendar field values to determine the date and time in the
1.155 + * following way.
1.156 + *
1.157 + * <p>If there is any conflict in calendar field values,
1.158 + * <code>Calendar</code> gives priorities to calendar fields that have been set
1.159 + * more recently. The following are the default combinations of the
1.160 + * calendar fields. The most recent combination, as determined by the
1.161 + * most recently set single field, will be used.
1.162 + *
1.163 + * <p><a name="date_resolution">For the date fields</a>:
1.164 + * <blockquote>
1.165 + * <pre>
1.166 + * YEAR + MONTH + DAY_OF_MONTH
1.167 + * YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
1.168 + * YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
1.169 + * YEAR + DAY_OF_YEAR
1.170 + * YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
1.171 + * </pre></blockquote>
1.172 + *
1.173 + * <a name="time_resolution">For the time of day fields</a>:
1.174 + * <blockquote>
1.175 + * <pre>
1.176 + * HOUR_OF_DAY
1.177 + * AM_PM + HOUR
1.178 + * </pre></blockquote>
1.179 + *
1.180 + * <p>If there are any calendar fields whose values haven't been set in the selected
1.181 + * field combination, <code>Calendar</code> uses their default values. The default
1.182 + * value of each field may vary by concrete calendar systems. For example, in
1.183 + * <code>GregorianCalendar</code>, the default of a field is the same as that
1.184 + * of the start of the Epoch: i.e., <code>YEAR = 1970</code>, <code>MONTH =
1.185 + * JANUARY</code>, <code>DAY_OF_MONTH = 1</code>, etc.
1.186 + *
1.187 + * <p>
1.188 + * <strong>Note:</strong> There are certain possible ambiguities in
1.189 + * interpretation of certain singular times, which are resolved in the
1.190 + * following ways:
1.191 + * <ol>
1.192 + * <li> 23:59 is the last minute of the day and 00:00 is the first
1.193 + * minute of the next day. Thus, 23:59 on Dec 31, 1999 < 00:00 on
1.194 + * Jan 1, 2000 < 00:01 on Jan 1, 2000.
1.195 + *
1.196 + * <li> Although historically not precise, midnight also belongs to "am",
1.197 + * and noon belongs to "pm", so on the same day,
1.198 + * 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm
1.199 + * </ol>
1.200 + *
1.201 + * <p>
1.202 + * The date or time format strings are not part of the definition of a
1.203 + * calendar, as those must be modifiable or overridable by the user at
1.204 + * runtime. Use {@link DateFormat}
1.205 + * to format dates.
1.206 + *
1.207 + * <h4>Field Manipulation</h4>
1.208 + *
1.209 + * The calendar fields can be changed using three methods:
1.210 + * <code>set()</code>, <code>add()</code>, and <code>roll()</code>.</p>
1.211 + *
1.212 + * <p><strong><code>set(f, value)</code></strong> changes calendar field
1.213 + * <code>f</code> to <code>value</code>. In addition, it sets an
1.214 + * internal member variable to indicate that calendar field <code>f</code> has
1.215 + * been changed. Although calendar field <code>f</code> is changed immediately,
1.216 + * the calendar's time value in milliseconds is not recomputed until the next call to
1.217 + * <code>get()</code>, <code>getTime()</code>, <code>getTimeInMillis()</code>,
1.218 + * <code>add()</code>, or <code>roll()</code> is made. Thus, multiple calls to
1.219 + * <code>set()</code> do not trigger multiple, unnecessary
1.220 + * computations. As a result of changing a calendar field using
1.221 + * <code>set()</code>, other calendar fields may also change, depending on the
1.222 + * calendar field, the calendar field value, and the calendar system. In addition,
1.223 + * <code>get(f)</code> will not necessarily return <code>value</code> set by
1.224 + * the call to the <code>set</code> method
1.225 + * after the calendar fields have been recomputed. The specifics are determined by
1.226 + * the concrete calendar class.</p>
1.227 + *
1.228 + * <p><em>Example</em>: Consider a <code>GregorianCalendar</code>
1.229 + * originally set to August 31, 1999. Calling <code>set(Calendar.MONTH,
1.230 + * Calendar.SEPTEMBER)</code> sets the date to September 31,
1.231 + * 1999. This is a temporary internal representation that resolves to
1.232 + * October 1, 1999 if <code>getTime()</code>is then called. However, a
1.233 + * call to <code>set(Calendar.DAY_OF_MONTH, 30)</code> before the call to
1.234 + * <code>getTime()</code> sets the date to September 30, 1999, since
1.235 + * no recomputation occurs after <code>set()</code> itself.</p>
1.236 + *
1.237 + * <p><strong><code>add(f, delta)</code></strong> adds <code>delta</code>
1.238 + * to field <code>f</code>. This is equivalent to calling <code>set(f,
1.239 + * get(f) + delta)</code> with two adjustments:</p>
1.240 + *
1.241 + * <blockquote>
1.242 + * <p><strong>Add rule 1</strong>. The value of field <code>f</code>
1.243 + * after the call minus the value of field <code>f</code> before the
1.244 + * call is <code>delta</code>, modulo any overflow that has occurred in
1.245 + * field <code>f</code>. Overflow occurs when a field value exceeds its
1.246 + * range and, as a result, the next larger field is incremented or
1.247 + * decremented and the field value is adjusted back into its range.</p>
1.248 + *
1.249 + * <p><strong>Add rule 2</strong>. If a smaller field is expected to be
1.250 + * invariant, but it is impossible for it to be equal to its
1.251 + * prior value because of changes in its minimum or maximum after field
1.252 + * <code>f</code> is changed or other constraints, such as time zone
1.253 + * offset changes, then its value is adjusted to be as close
1.254 + * as possible to its expected value. A smaller field represents a
1.255 + * smaller unit of time. <code>HOUR</code> is a smaller field than
1.256 + * <code>DAY_OF_MONTH</code>. No adjustment is made to smaller fields
1.257 + * that are not expected to be invariant. The calendar system
1.258 + * determines what fields are expected to be invariant.</p>
1.259 + * </blockquote>
1.260 + *
1.261 + * <p>In addition, unlike <code>set()</code>, <code>add()</code> forces
1.262 + * an immediate recomputation of the calendar's milliseconds and all
1.263 + * fields.</p>
1.264 + *
1.265 + * <p><em>Example</em>: Consider a <code>GregorianCalendar</code>
1.266 + * originally set to August 31, 1999. Calling <code>add(Calendar.MONTH,
1.267 + * 13)</code> sets the calendar to September 30, 2000. <strong>Add rule
1.268 + * 1</strong> sets the <code>MONTH</code> field to September, since
1.269 + * adding 13 months to August gives September of the next year. Since
1.270 + * <code>DAY_OF_MONTH</code> cannot be 31 in September in a
1.271 + * <code>GregorianCalendar</code>, <strong>add rule 2</strong> sets the
1.272 + * <code>DAY_OF_MONTH</code> to 30, the closest possible value. Although
1.273 + * it is a smaller field, <code>DAY_OF_WEEK</code> is not adjusted by
1.274 + * rule 2, since it is expected to change when the month changes in a
1.275 + * <code>GregorianCalendar</code>.</p>
1.276 + *
1.277 + * <p><strong><code>roll(f, delta)</code></strong> adds
1.278 + * <code>delta</code> to field <code>f</code> without changing larger
1.279 + * fields. This is equivalent to calling <code>add(f, delta)</code> with
1.280 + * the following adjustment:</p>
1.281 + *
1.282 + * <blockquote>
1.283 + * <p><strong>Roll rule</strong>. Larger fields are unchanged after the
1.284 + * call. A larger field represents a larger unit of
1.285 + * time. <code>DAY_OF_MONTH</code> is a larger field than
1.286 + * <code>HOUR</code>.</p>
1.287 + * </blockquote>
1.288 + *
1.289 + * <p><em>Example</em>: See {@link java.util.GregorianCalendar#roll(int, int)}.
1.290 + *
1.291 + * <p><strong>Usage model</strong>. To motivate the behavior of
1.292 + * <code>add()</code> and <code>roll()</code>, consider a user interface
1.293 + * component with increment and decrement buttons for the month, day, and
1.294 + * year, and an underlying <code>GregorianCalendar</code>. If the
1.295 + * interface reads January 31, 1999 and the user presses the month
1.296 + * increment button, what should it read? If the underlying
1.297 + * implementation uses <code>set()</code>, it might read March 3, 1999. A
1.298 + * better result would be February 28, 1999. Furthermore, if the user
1.299 + * presses the month increment button again, it should read March 31,
1.300 + * 1999, not March 28, 1999. By saving the original date and using either
1.301 + * <code>add()</code> or <code>roll()</code>, depending on whether larger
1.302 + * fields should be affected, the user interface can behave as most users
1.303 + * will intuitively expect.</p>
1.304 + *
1.305 + * @see java.lang.System#currentTimeMillis()
1.306 + * @see Date
1.307 + * @see GregorianCalendar
1.308 + * @see TimeZone
1.309 + * @see java.text.DateFormat
1.310 + * @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu
1.311 + * @since JDK1.1
1.312 + */
1.313 +public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar> {
1.314 +
1.315 + // Data flow in Calendar
1.316 + // ---------------------
1.317 +
1.318 + // The current time is represented in two ways by Calendar: as UTC
1.319 + // milliseconds from the epoch (1 January 1970 0:00 UTC), and as local
1.320 + // fields such as MONTH, HOUR, AM_PM, etc. It is possible to compute the
1.321 + // millis from the fields, and vice versa. The data needed to do this
1.322 + // conversion is encapsulated by a TimeZone object owned by the Calendar.
1.323 + // The data provided by the TimeZone object may also be overridden if the
1.324 + // user sets the ZONE_OFFSET and/or DST_OFFSET fields directly. The class
1.325 + // keeps track of what information was most recently set by the caller, and
1.326 + // uses that to compute any other information as needed.
1.327 +
1.328 + // If the user sets the fields using set(), the data flow is as follows.
1.329 + // This is implemented by the Calendar subclass's computeTime() method.
1.330 + // During this process, certain fields may be ignored. The disambiguation
1.331 + // algorithm for resolving which fields to pay attention to is described
1.332 + // in the class documentation.
1.333 +
1.334 + // local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.)
1.335 + // |
1.336 + // | Using Calendar-specific algorithm
1.337 + // V
1.338 + // local standard millis
1.339 + // |
1.340 + // | Using TimeZone or user-set ZONE_OFFSET / DST_OFFSET
1.341 + // V
1.342 + // UTC millis (in time data member)
1.343 +
1.344 + // If the user sets the UTC millis using setTime() or setTimeInMillis(),
1.345 + // the data flow is as follows. This is implemented by the Calendar
1.346 + // subclass's computeFields() method.
1.347 +
1.348 + // UTC millis (in time data member)
1.349 + // |
1.350 + // | Using TimeZone getOffset()
1.351 + // V
1.352 + // local standard millis
1.353 + // |
1.354 + // | Using Calendar-specific algorithm
1.355 + // V
1.356 + // local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.)
1.357 +
1.358 + // In general, a round trip from fields, through local and UTC millis, and
1.359 + // back out to fields is made when necessary. This is implemented by the
1.360 + // complete() method. Resolving a partial set of fields into a UTC millis
1.361 + // value allows all remaining fields to be generated from that value. If
1.362 + // the Calendar is lenient, the fields are also renormalized to standard
1.363 + // ranges when they are regenerated.
1.364 +
1.365 + /**
1.366 + * Field number for <code>get</code> and <code>set</code> indicating the
1.367 + * era, e.g., AD or BC in the Julian calendar. This is a calendar-specific
1.368 + * value; see subclass documentation.
1.369 + *
1.370 + * @see GregorianCalendar#AD
1.371 + * @see GregorianCalendar#BC
1.372 + */
1.373 + public final static int ERA = 0;
1.374 +
1.375 + /**
1.376 + * Field number for <code>get</code> and <code>set</code> indicating the
1.377 + * year. This is a calendar-specific value; see subclass documentation.
1.378 + */
1.379 + public final static int YEAR = 1;
1.380 +
1.381 + /**
1.382 + * Field number for <code>get</code> and <code>set</code> indicating the
1.383 + * month. This is a calendar-specific value. The first month of
1.384 + * the year in the Gregorian and Julian calendars is
1.385 + * <code>JANUARY</code> which is 0; the last depends on the number
1.386 + * of months in a year.
1.387 + *
1.388 + * @see #JANUARY
1.389 + * @see #FEBRUARY
1.390 + * @see #MARCH
1.391 + * @see #APRIL
1.392 + * @see #MAY
1.393 + * @see #JUNE
1.394 + * @see #JULY
1.395 + * @see #AUGUST
1.396 + * @see #SEPTEMBER
1.397 + * @see #OCTOBER
1.398 + * @see #NOVEMBER
1.399 + * @see #DECEMBER
1.400 + * @see #UNDECIMBER
1.401 + */
1.402 + public final static int MONTH = 2;
1.403 +
1.404 + /**
1.405 + * Field number for <code>get</code> and <code>set</code> indicating the
1.406 + * week number within the current year. The first week of the year, as
1.407 + * defined by <code>getFirstDayOfWeek()</code> and
1.408 + * <code>getMinimalDaysInFirstWeek()</code>, has value 1. Subclasses define
1.409 + * the value of <code>WEEK_OF_YEAR</code> for days before the first week of
1.410 + * the year.
1.411 + *
1.412 + * @see #getFirstDayOfWeek
1.413 + * @see #getMinimalDaysInFirstWeek
1.414 + */
1.415 + public final static int WEEK_OF_YEAR = 3;
1.416 +
1.417 + /**
1.418 + * Field number for <code>get</code> and <code>set</code> indicating the
1.419 + * week number within the current month. The first week of the month, as
1.420 + * defined by <code>getFirstDayOfWeek()</code> and
1.421 + * <code>getMinimalDaysInFirstWeek()</code>, has value 1. Subclasses define
1.422 + * the value of <code>WEEK_OF_MONTH</code> for days before the first week of
1.423 + * the month.
1.424 + *
1.425 + * @see #getFirstDayOfWeek
1.426 + * @see #getMinimalDaysInFirstWeek
1.427 + */
1.428 + public final static int WEEK_OF_MONTH = 4;
1.429 +
1.430 + /**
1.431 + * Field number for <code>get</code> and <code>set</code> indicating the
1.432 + * day of the month. This is a synonym for <code>DAY_OF_MONTH</code>.
1.433 + * The first day of the month has value 1.
1.434 + *
1.435 + * @see #DAY_OF_MONTH
1.436 + */
1.437 + public final static int DATE = 5;
1.438 +
1.439 + /**
1.440 + * Field number for <code>get</code> and <code>set</code> indicating the
1.441 + * day of the month. This is a synonym for <code>DATE</code>.
1.442 + * The first day of the month has value 1.
1.443 + *
1.444 + * @see #DATE
1.445 + */
1.446 + public final static int DAY_OF_MONTH = 5;
1.447 +
1.448 + /**
1.449 + * Field number for <code>get</code> and <code>set</code> indicating the day
1.450 + * number within the current year. The first day of the year has value 1.
1.451 + */
1.452 + public final static int DAY_OF_YEAR = 6;
1.453 +
1.454 + /**
1.455 + * Field number for <code>get</code> and <code>set</code> indicating the day
1.456 + * of the week. This field takes values <code>SUNDAY</code>,
1.457 + * <code>MONDAY</code>, <code>TUESDAY</code>, <code>WEDNESDAY</code>,
1.458 + * <code>THURSDAY</code>, <code>FRIDAY</code>, and <code>SATURDAY</code>.
1.459 + *
1.460 + * @see #SUNDAY
1.461 + * @see #MONDAY
1.462 + * @see #TUESDAY
1.463 + * @see #WEDNESDAY
1.464 + * @see #THURSDAY
1.465 + * @see #FRIDAY
1.466 + * @see #SATURDAY
1.467 + */
1.468 + public final static int DAY_OF_WEEK = 7;
1.469 +
1.470 + /**
1.471 + * Field number for <code>get</code> and <code>set</code> indicating the
1.472 + * ordinal number of the day of the week within the current month. Together
1.473 + * with the <code>DAY_OF_WEEK</code> field, this uniquely specifies a day
1.474 + * within a month. Unlike <code>WEEK_OF_MONTH</code> and
1.475 + * <code>WEEK_OF_YEAR</code>, this field's value does <em>not</em> depend on
1.476 + * <code>getFirstDayOfWeek()</code> or
1.477 + * <code>getMinimalDaysInFirstWeek()</code>. <code>DAY_OF_MONTH 1</code>
1.478 + * through <code>7</code> always correspond to <code>DAY_OF_WEEK_IN_MONTH
1.479 + * 1</code>; <code>8</code> through <code>14</code> correspond to
1.480 + * <code>DAY_OF_WEEK_IN_MONTH 2</code>, and so on.
1.481 + * <code>DAY_OF_WEEK_IN_MONTH 0</code> indicates the week before
1.482 + * <code>DAY_OF_WEEK_IN_MONTH 1</code>. Negative values count back from the
1.483 + * end of the month, so the last Sunday of a month is specified as
1.484 + * <code>DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1</code>. Because
1.485 + * negative values count backward they will usually be aligned differently
1.486 + * within the month than positive values. For example, if a month has 31
1.487 + * days, <code>DAY_OF_WEEK_IN_MONTH -1</code> will overlap
1.488 + * <code>DAY_OF_WEEK_IN_MONTH 5</code> and the end of <code>4</code>.
1.489 + *
1.490 + * @see #DAY_OF_WEEK
1.491 + * @see #WEEK_OF_MONTH
1.492 + */
1.493 + public final static int DAY_OF_WEEK_IN_MONTH = 8;
1.494 +
1.495 + /**
1.496 + * Field number for <code>get</code> and <code>set</code> indicating
1.497 + * whether the <code>HOUR</code> is before or after noon.
1.498 + * E.g., at 10:04:15.250 PM the <code>AM_PM</code> is <code>PM</code>.
1.499 + *
1.500 + * @see #AM
1.501 + * @see #PM
1.502 + * @see #HOUR
1.503 + */
1.504 + public final static int AM_PM = 9;
1.505 +
1.506 + /**
1.507 + * Field number for <code>get</code> and <code>set</code> indicating the
1.508 + * hour of the morning or afternoon. <code>HOUR</code> is used for the
1.509 + * 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12.
1.510 + * E.g., at 10:04:15.250 PM the <code>HOUR</code> is 10.
1.511 + *
1.512 + * @see #AM_PM
1.513 + * @see #HOUR_OF_DAY
1.514 + */
1.515 + public final static int HOUR = 10;
1.516 +
1.517 + /**
1.518 + * Field number for <code>get</code> and <code>set</code> indicating the
1.519 + * hour of the day. <code>HOUR_OF_DAY</code> is used for the 24-hour clock.
1.520 + * E.g., at 10:04:15.250 PM the <code>HOUR_OF_DAY</code> is 22.
1.521 + *
1.522 + * @see #HOUR
1.523 + */
1.524 + public final static int HOUR_OF_DAY = 11;
1.525 +
1.526 + /**
1.527 + * Field number for <code>get</code> and <code>set</code> indicating the
1.528 + * minute within the hour.
1.529 + * E.g., at 10:04:15.250 PM the <code>MINUTE</code> is 4.
1.530 + */
1.531 + public final static int MINUTE = 12;
1.532 +
1.533 + /**
1.534 + * Field number for <code>get</code> and <code>set</code> indicating the
1.535 + * second within the minute.
1.536 + * E.g., at 10:04:15.250 PM the <code>SECOND</code> is 15.
1.537 + */
1.538 + public final static int SECOND = 13;
1.539 +
1.540 + /**
1.541 + * Field number for <code>get</code> and <code>set</code> indicating the
1.542 + * millisecond within the second.
1.543 + * E.g., at 10:04:15.250 PM the <code>MILLISECOND</code> is 250.
1.544 + */
1.545 + public final static int MILLISECOND = 14;
1.546 +
1.547 + /**
1.548 + * Field number for <code>get</code> and <code>set</code>
1.549 + * indicating the raw offset from GMT in milliseconds.
1.550 + * <p>
1.551 + * This field reflects the correct GMT offset value of the time
1.552 + * zone of this <code>Calendar</code> if the
1.553 + * <code>TimeZone</code> implementation subclass supports
1.554 + * historical GMT offset changes.
1.555 + */
1.556 + public final static int ZONE_OFFSET = 15;
1.557 +
1.558 + /**
1.559 + * Field number for <code>get</code> and <code>set</code> indicating the
1.560 + * daylight saving offset in milliseconds.
1.561 + * <p>
1.562 + * This field reflects the correct daylight saving offset value of
1.563 + * the time zone of this <code>Calendar</code> if the
1.564 + * <code>TimeZone</code> implementation subclass supports
1.565 + * historical Daylight Saving Time schedule changes.
1.566 + */
1.567 + public final static int DST_OFFSET = 16;
1.568 +
1.569 + /**
1.570 + * The number of distinct fields recognized by <code>get</code> and <code>set</code>.
1.571 + * Field numbers range from <code>0..FIELD_COUNT-1</code>.
1.572 + */
1.573 + public final static int FIELD_COUNT = 17;
1.574 +
1.575 + /**
1.576 + * Value of the {@link #DAY_OF_WEEK} field indicating
1.577 + * Sunday.
1.578 + */
1.579 + public final static int SUNDAY = 1;
1.580 +
1.581 + /**
1.582 + * Value of the {@link #DAY_OF_WEEK} field indicating
1.583 + * Monday.
1.584 + */
1.585 + public final static int MONDAY = 2;
1.586 +
1.587 + /**
1.588 + * Value of the {@link #DAY_OF_WEEK} field indicating
1.589 + * Tuesday.
1.590 + */
1.591 + public final static int TUESDAY = 3;
1.592 +
1.593 + /**
1.594 + * Value of the {@link #DAY_OF_WEEK} field indicating
1.595 + * Wednesday.
1.596 + */
1.597 + public final static int WEDNESDAY = 4;
1.598 +
1.599 + /**
1.600 + * Value of the {@link #DAY_OF_WEEK} field indicating
1.601 + * Thursday.
1.602 + */
1.603 + public final static int THURSDAY = 5;
1.604 +
1.605 + /**
1.606 + * Value of the {@link #DAY_OF_WEEK} field indicating
1.607 + * Friday.
1.608 + */
1.609 + public final static int FRIDAY = 6;
1.610 +
1.611 + /**
1.612 + * Value of the {@link #DAY_OF_WEEK} field indicating
1.613 + * Saturday.
1.614 + */
1.615 + public final static int SATURDAY = 7;
1.616 +
1.617 + /**
1.618 + * Value of the {@link #MONTH} field indicating the
1.619 + * first month of the year in the Gregorian and Julian calendars.
1.620 + */
1.621 + public final static int JANUARY = 0;
1.622 +
1.623 + /**
1.624 + * Value of the {@link #MONTH} field indicating the
1.625 + * second month of the year in the Gregorian and Julian calendars.
1.626 + */
1.627 + public final static int FEBRUARY = 1;
1.628 +
1.629 + /**
1.630 + * Value of the {@link #MONTH} field indicating the
1.631 + * third month of the year in the Gregorian and Julian calendars.
1.632 + */
1.633 + public final static int MARCH = 2;
1.634 +
1.635 + /**
1.636 + * Value of the {@link #MONTH} field indicating the
1.637 + * fourth month of the year in the Gregorian and Julian calendars.
1.638 + */
1.639 + public final static int APRIL = 3;
1.640 +
1.641 + /**
1.642 + * Value of the {@link #MONTH} field indicating the
1.643 + * fifth month of the year in the Gregorian and Julian calendars.
1.644 + */
1.645 + public final static int MAY = 4;
1.646 +
1.647 + /**
1.648 + * Value of the {@link #MONTH} field indicating the
1.649 + * sixth month of the year in the Gregorian and Julian calendars.
1.650 + */
1.651 + public final static int JUNE = 5;
1.652 +
1.653 + /**
1.654 + * Value of the {@link #MONTH} field indicating the
1.655 + * seventh month of the year in the Gregorian and Julian calendars.
1.656 + */
1.657 + public final static int JULY = 6;
1.658 +
1.659 + /**
1.660 + * Value of the {@link #MONTH} field indicating the
1.661 + * eighth month of the year in the Gregorian and Julian calendars.
1.662 + */
1.663 + public final static int AUGUST = 7;
1.664 +
1.665 + /**
1.666 + * Value of the {@link #MONTH} field indicating the
1.667 + * ninth month of the year in the Gregorian and Julian calendars.
1.668 + */
1.669 + public final static int SEPTEMBER = 8;
1.670 +
1.671 + /**
1.672 + * Value of the {@link #MONTH} field indicating the
1.673 + * tenth month of the year in the Gregorian and Julian calendars.
1.674 + */
1.675 + public final static int OCTOBER = 9;
1.676 +
1.677 + /**
1.678 + * Value of the {@link #MONTH} field indicating the
1.679 + * eleventh month of the year in the Gregorian and Julian calendars.
1.680 + */
1.681 + public final static int NOVEMBER = 10;
1.682 +
1.683 + /**
1.684 + * Value of the {@link #MONTH} field indicating the
1.685 + * twelfth month of the year in the Gregorian and Julian calendars.
1.686 + */
1.687 + public final static int DECEMBER = 11;
1.688 +
1.689 + /**
1.690 + * Value of the {@link #MONTH} field indicating the
1.691 + * thirteenth month of the year. Although <code>GregorianCalendar</code>
1.692 + * does not use this value, lunar calendars do.
1.693 + */
1.694 + public final static int UNDECIMBER = 12;
1.695 +
1.696 + /**
1.697 + * Value of the {@link #AM_PM} field indicating the
1.698 + * period of the day from midnight to just before noon.
1.699 + */
1.700 + public final static int AM = 0;
1.701 +
1.702 + /**
1.703 + * Value of the {@link #AM_PM} field indicating the
1.704 + * period of the day from noon to just before midnight.
1.705 + */
1.706 + public final static int PM = 1;
1.707 +
1.708 + /**
1.709 + * A style specifier for {@link #getDisplayNames(int, int, Locale)
1.710 + * getDisplayNames} indicating names in all styles, such as
1.711 + * "January" and "Jan".
1.712 + *
1.713 + * @see #SHORT
1.714 + * @see #LONG
1.715 + * @since 1.6
1.716 + */
1.717 + public static final int ALL_STYLES = 0;
1.718 +
1.719 + /**
1.720 + * A style specifier for {@link #getDisplayName(int, int, Locale)
1.721 + * getDisplayName} and {@link #getDisplayNames(int, int, Locale)
1.722 + * getDisplayNames} indicating a short name, such as "Jan".
1.723 + *
1.724 + * @see #LONG
1.725 + * @since 1.6
1.726 + */
1.727 + public static final int SHORT = 1;
1.728 +
1.729 + /**
1.730 + * A style specifier for {@link #getDisplayName(int, int, Locale)
1.731 + * getDisplayName} and {@link #getDisplayNames(int, int, Locale)
1.732 + * getDisplayNames} indicating a long name, such as "January".
1.733 + *
1.734 + * @see #SHORT
1.735 + * @since 1.6
1.736 + */
1.737 + public static final int LONG = 2;
1.738 +
1.739 + // Internal notes:
1.740 + // Calendar contains two kinds of time representations: current "time" in
1.741 + // milliseconds, and a set of calendar "fields" representing the current time.
1.742 + // The two representations are usually in sync, but can get out of sync
1.743 + // as follows.
1.744 + // 1. Initially, no fields are set, and the time is invalid.
1.745 + // 2. If the time is set, all fields are computed and in sync.
1.746 + // 3. If a single field is set, the time is invalid.
1.747 + // Recomputation of the time and fields happens when the object needs
1.748 + // to return a result to the user, or use a result for a computation.
1.749 +
1.750 + /**
1.751 + * The calendar field values for the currently set time for this calendar.
1.752 + * This is an array of <code>FIELD_COUNT</code> integers, with index values
1.753 + * <code>ERA</code> through <code>DST_OFFSET</code>.
1.754 + * @serial
1.755 + */
1.756 + protected int fields[];
1.757 +
1.758 + /**
1.759 + * The flags which tell if a specified calendar field for the calendar is set.
1.760 + * A new object has no fields set. After the first call to a method
1.761 + * which generates the fields, they all remain set after that.
1.762 + * This is an array of <code>FIELD_COUNT</code> booleans, with index values
1.763 + * <code>ERA</code> through <code>DST_OFFSET</code>.
1.764 + * @serial
1.765 + */
1.766 + protected boolean isSet[];
1.767 +
1.768 + /**
1.769 + * Pseudo-time-stamps which specify when each field was set. There
1.770 + * are two special values, UNSET and COMPUTED. Values from
1.771 + * MINIMUM_USER_SET to Integer.MAX_VALUE are legal user set values.
1.772 + */
1.773 + transient private int stamp[];
1.774 +
1.775 + /**
1.776 + * The currently set time for this calendar, expressed in milliseconds after
1.777 + * January 1, 1970, 0:00:00 GMT.
1.778 + * @see #isTimeSet
1.779 + * @serial
1.780 + */
1.781 + protected long time;
1.782 +
1.783 + /**
1.784 + * True if then the value of <code>time</code> is valid.
1.785 + * The time is made invalid by a change to an item of <code>field[]</code>.
1.786 + * @see #time
1.787 + * @serial
1.788 + */
1.789 + protected boolean isTimeSet;
1.790 +
1.791 + /**
1.792 + * True if <code>fields[]</code> are in sync with the currently set time.
1.793 + * If false, then the next attempt to get the value of a field will
1.794 + * force a recomputation of all fields from the current value of
1.795 + * <code>time</code>.
1.796 + * @serial
1.797 + */
1.798 + protected boolean areFieldsSet;
1.799 +
1.800 + /**
1.801 + * True if all fields have been set.
1.802 + * @serial
1.803 + */
1.804 + transient boolean areAllFieldsSet;
1.805 +
1.806 + /**
1.807 + * <code>True</code> if this calendar allows out-of-range field values during computation
1.808 + * of <code>time</code> from <code>fields[]</code>.
1.809 + * @see #setLenient
1.810 + * @see #isLenient
1.811 + * @serial
1.812 + */
1.813 + private boolean lenient = true;
1.814 +
1.815 + /**
1.816 + * The <code>TimeZone</code> used by this calendar. <code>Calendar</code>
1.817 + * uses the time zone data to translate between locale and GMT time.
1.818 + * @serial
1.819 + */
1.820 + private TimeZone zone;
1.821 +
1.822 + /**
1.823 + * <code>True</code> if zone references to a shared TimeZone object.
1.824 + */
1.825 + transient private boolean sharedZone = false;
1.826 +
1.827 + /**
1.828 + * The first day of the week, with possible values <code>SUNDAY</code>,
1.829 + * <code>MONDAY</code>, etc. This is a locale-dependent value.
1.830 + * @serial
1.831 + */
1.832 + private int firstDayOfWeek;
1.833 +
1.834 + /**
1.835 + * The number of days required for the first week in a month or year,
1.836 + * with possible values from 1 to 7. This is a locale-dependent value.
1.837 + * @serial
1.838 + */
1.839 + private int minimalDaysInFirstWeek;
1.840 +
1.841 + /**
1.842 + * Cache to hold the firstDayOfWeek and minimalDaysInFirstWeek
1.843 + * of a Locale.
1.844 + */
1.845 + private static final ConcurrentMap<Locale, int[]> cachedLocaleData
1.846 + = new ConcurrentHashMap<Locale, int[]>(3);
1.847 +
1.848 + // Special values of stamp[]
1.849 + /**
1.850 + * The corresponding fields[] has no value.
1.851 + */
1.852 + private static final int UNSET = 0;
1.853 +
1.854 + /**
1.855 + * The value of the corresponding fields[] has been calculated internally.
1.856 + */
1.857 + private static final int COMPUTED = 1;
1.858 +
1.859 + /**
1.860 + * The value of the corresponding fields[] has been set externally. Stamp
1.861 + * values which are greater than 1 represents the (pseudo) time when the
1.862 + * corresponding fields[] value was set.
1.863 + */
1.864 + private static final int MINIMUM_USER_STAMP = 2;
1.865 +
1.866 + /**
1.867 + * The mask value that represents all of the fields.
1.868 + */
1.869 + static final int ALL_FIELDS = (1 << FIELD_COUNT) - 1;
1.870 +
1.871 + /**
1.872 + * The next available value for <code>stamp[]</code>, an internal array.
1.873 + * This actually should not be written out to the stream, and will probably
1.874 + * be removed from the stream in the near future. In the meantime,
1.875 + * a value of <code>MINIMUM_USER_STAMP</code> should be used.
1.876 + * @serial
1.877 + */
1.878 + private int nextStamp = MINIMUM_USER_STAMP;
1.879 +
1.880 + // the internal serial version which says which version was written
1.881 + // - 0 (default) for version up to JDK 1.1.5
1.882 + // - 1 for version from JDK 1.1.6, which writes a correct 'time' value
1.883 + // as well as compatible values for other fields. This is a
1.884 + // transitional format.
1.885 + // - 2 (not implemented yet) a future version, in which fields[],
1.886 + // areFieldsSet, and isTimeSet become transient, and isSet[] is
1.887 + // removed. In JDK 1.1.6 we write a format compatible with version 2.
1.888 + static final int currentSerialVersion = 1;
1.889 +
1.890 + /**
1.891 + * The version of the serialized data on the stream. Possible values:
1.892 + * <dl>
1.893 + * <dt><b>0</b> or not present on stream</dt>
1.894 + * <dd>
1.895 + * JDK 1.1.5 or earlier.
1.896 + * </dd>
1.897 + * <dt><b>1</b></dt>
1.898 + * <dd>
1.899 + * JDK 1.1.6 or later. Writes a correct 'time' value
1.900 + * as well as compatible values for other fields. This is a
1.901 + * transitional format.
1.902 + * </dd>
1.903 + * </dl>
1.904 + * When streaming out this class, the most recent format
1.905 + * and the highest allowable <code>serialVersionOnStream</code>
1.906 + * is written.
1.907 + * @serial
1.908 + * @since JDK1.1.6
1.909 + */
1.910 + private int serialVersionOnStream = currentSerialVersion;
1.911 +
1.912 + // Proclaim serialization compatibility with JDK 1.1
1.913 + static final long serialVersionUID = -1807547505821590642L;
1.914 +
1.915 + // Mask values for calendar fields
1.916 + final static int ERA_MASK = (1 << ERA);
1.917 + final static int YEAR_MASK = (1 << YEAR);
1.918 + final static int MONTH_MASK = (1 << MONTH);
1.919 + final static int WEEK_OF_YEAR_MASK = (1 << WEEK_OF_YEAR);
1.920 + final static int WEEK_OF_MONTH_MASK = (1 << WEEK_OF_MONTH);
1.921 + final static int DAY_OF_MONTH_MASK = (1 << DAY_OF_MONTH);
1.922 + final static int DATE_MASK = DAY_OF_MONTH_MASK;
1.923 + final static int DAY_OF_YEAR_MASK = (1 << DAY_OF_YEAR);
1.924 + final static int DAY_OF_WEEK_MASK = (1 << DAY_OF_WEEK);
1.925 + final static int DAY_OF_WEEK_IN_MONTH_MASK = (1 << DAY_OF_WEEK_IN_MONTH);
1.926 + final static int AM_PM_MASK = (1 << AM_PM);
1.927 + final static int HOUR_MASK = (1 << HOUR);
1.928 + final static int HOUR_OF_DAY_MASK = (1 << HOUR_OF_DAY);
1.929 + final static int MINUTE_MASK = (1 << MINUTE);
1.930 + final static int SECOND_MASK = (1 << SECOND);
1.931 + final static int MILLISECOND_MASK = (1 << MILLISECOND);
1.932 + final static int ZONE_OFFSET_MASK = (1 << ZONE_OFFSET);
1.933 + final static int DST_OFFSET_MASK = (1 << DST_OFFSET);
1.934 +
1.935 + /**
1.936 + * Constructs a Calendar with the default time zone
1.937 + * and locale.
1.938 + * @see TimeZone#getDefault
1.939 + */
1.940 + protected Calendar()
1.941 + {
1.942 + this(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
1.943 + sharedZone = true;
1.944 + }
1.945 +
1.946 + /**
1.947 + * Constructs a calendar with the specified time zone and locale.
1.948 + *
1.949 + * @param zone the time zone to use
1.950 + * @param aLocale the locale for the week data
1.951 + */
1.952 + protected Calendar(TimeZone zone, Locale aLocale)
1.953 + {
1.954 + fields = new int[FIELD_COUNT];
1.955 + isSet = new boolean[FIELD_COUNT];
1.956 + stamp = new int[FIELD_COUNT];
1.957 +
1.958 + this.zone = zone;
1.959 + setWeekCountData(aLocale);
1.960 + }
1.961 +
1.962 + /**
1.963 + * Gets a calendar using the default time zone and locale. The
1.964 + * <code>Calendar</code> returned is based on the current time
1.965 + * in the default time zone with the default locale.
1.966 + *
1.967 + * @return a Calendar.
1.968 + */
1.969 + public static Calendar getInstance()
1.970 + {
1.971 + Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
1.972 + cal.sharedZone = true;
1.973 + return cal;
1.974 + }
1.975 +
1.976 + /**
1.977 + * Gets a calendar using the specified time zone and default locale.
1.978 + * The <code>Calendar</code> returned is based on the current time
1.979 + * in the given time zone with the default locale.
1.980 + *
1.981 + * @param zone the time zone to use
1.982 + * @return a Calendar.
1.983 + */
1.984 + public static Calendar getInstance(TimeZone zone)
1.985 + {
1.986 + return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT));
1.987 + }
1.988 +
1.989 + /**
1.990 + * Gets a calendar using the default time zone and specified locale.
1.991 + * The <code>Calendar</code> returned is based on the current time
1.992 + * in the default time zone with the given locale.
1.993 + *
1.994 + * @param aLocale the locale for the week data
1.995 + * @return a Calendar.
1.996 + */
1.997 + public static Calendar getInstance(Locale aLocale)
1.998 + {
1.999 + Calendar cal = createCalendar(TimeZone.getDefaultRef(), aLocale);
1.1000 + cal.sharedZone = true;
1.1001 + return cal;
1.1002 + }
1.1003 +
1.1004 + /**
1.1005 + * Gets a calendar with the specified time zone and locale.
1.1006 + * The <code>Calendar</code> returned is based on the current time
1.1007 + * in the given time zone with the given locale.
1.1008 + *
1.1009 + * @param zone the time zone to use
1.1010 + * @param aLocale the locale for the week data
1.1011 + * @return a Calendar.
1.1012 + */
1.1013 + public static Calendar getInstance(TimeZone zone,
1.1014 + Locale aLocale)
1.1015 + {
1.1016 + return createCalendar(zone, aLocale);
1.1017 + }
1.1018 +
1.1019 + private static Calendar createCalendar(TimeZone zone,
1.1020 + Locale aLocale)
1.1021 + {
1.1022 + Calendar cal = null;
1.1023 +
1.1024 + String caltype = aLocale.getUnicodeLocaleType("ca");
1.1025 + if (caltype == null) {
1.1026 + // Calendar type is not specified.
1.1027 + // If the specified locale is a Thai locale,
1.1028 + // returns a BuddhistCalendar instance.
1.1029 + if ("th".equals(aLocale.getLanguage())
1.1030 + && ("TH".equals(aLocale.getCountry()))) {
1.1031 + cal = new BuddhistCalendar(zone, aLocale);
1.1032 + } else {
1.1033 + cal = new GregorianCalendar(zone, aLocale);
1.1034 + }
1.1035 + } else if (caltype.equals("japanese")) {
1.1036 + cal = new JapaneseImperialCalendar(zone, aLocale);
1.1037 + } else if (caltype.equals("buddhist")) {
1.1038 + cal = new BuddhistCalendar(zone, aLocale);
1.1039 + } else {
1.1040 + // Unsupported calendar type.
1.1041 + // Use Gregorian calendar as a fallback.
1.1042 + cal = new GregorianCalendar(zone, aLocale);
1.1043 + }
1.1044 +
1.1045 + return cal;
1.1046 + }
1.1047 +
1.1048 + /**
1.1049 + * Returns an array of all locales for which the <code>getInstance</code>
1.1050 + * methods of this class can return localized instances.
1.1051 + * The array returned must contain at least a <code>Locale</code>
1.1052 + * instance equal to {@link java.util.Locale#US Locale.US}.
1.1053 + *
1.1054 + * @return An array of locales for which localized
1.1055 + * <code>Calendar</code> instances are available.
1.1056 + */
1.1057 + public static synchronized Locale[] getAvailableLocales()
1.1058 + {
1.1059 + return DateFormat.getAvailableLocales();
1.1060 + }
1.1061 +
1.1062 + /**
1.1063 + * Converts the current calendar field values in {@link #fields fields[]}
1.1064 + * to the millisecond time value
1.1065 + * {@link #time}.
1.1066 + *
1.1067 + * @see #complete()
1.1068 + * @see #computeFields()
1.1069 + */
1.1070 + protected abstract void computeTime();
1.1071 +
1.1072 + /**
1.1073 + * Converts the current millisecond time value {@link #time}
1.1074 + * to calendar field values in {@link #fields fields[]}.
1.1075 + * This allows you to sync up the calendar field values with
1.1076 + * a new time that is set for the calendar. The time is <em>not</em>
1.1077 + * recomputed first; to recompute the time, then the fields, call the
1.1078 + * {@link #complete()} method.
1.1079 + *
1.1080 + * @see #computeTime()
1.1081 + */
1.1082 + protected abstract void computeFields();
1.1083 +
1.1084 + /**
1.1085 + * Returns a <code>Date</code> object representing this
1.1086 + * <code>Calendar</code>'s time value (millisecond offset from the <a
1.1087 + * href="#Epoch">Epoch</a>").
1.1088 + *
1.1089 + * @return a <code>Date</code> representing the time value.
1.1090 + * @see #setTime(Date)
1.1091 + * @see #getTimeInMillis()
1.1092 + */
1.1093 + public final Date getTime() {
1.1094 + return new Date(getTimeInMillis());
1.1095 + }
1.1096 +
1.1097 + /**
1.1098 + * Sets this Calendar's time with the given <code>Date</code>.
1.1099 + * <p>
1.1100 + * Note: Calling <code>setTime()</code> with
1.1101 + * <code>Date(Long.MAX_VALUE)</code> or <code>Date(Long.MIN_VALUE)</code>
1.1102 + * may yield incorrect field values from <code>get()</code>.
1.1103 + *
1.1104 + * @param date the given Date.
1.1105 + * @see #getTime()
1.1106 + * @see #setTimeInMillis(long)
1.1107 + */
1.1108 + public final void setTime(Date date) {
1.1109 + setTimeInMillis(date.getTime());
1.1110 + }
1.1111 +
1.1112 + /**
1.1113 + * Returns this Calendar's time value in milliseconds.
1.1114 + *
1.1115 + * @return the current time as UTC milliseconds from the epoch.
1.1116 + * @see #getTime()
1.1117 + * @see #setTimeInMillis(long)
1.1118 + */
1.1119 + public long getTimeInMillis() {
1.1120 + if (!isTimeSet) {
1.1121 + updateTime();
1.1122 + }
1.1123 + return time;
1.1124 + }
1.1125 +
1.1126 + /**
1.1127 + * Sets this Calendar's current time from the given long value.
1.1128 + *
1.1129 + * @param millis the new time in UTC milliseconds from the epoch.
1.1130 + * @see #setTime(Date)
1.1131 + * @see #getTimeInMillis()
1.1132 + */
1.1133 + public void setTimeInMillis(long millis) {
1.1134 + // If we don't need to recalculate the calendar field values,
1.1135 + // do nothing.
1.1136 + if (time == millis && isTimeSet && areFieldsSet && areAllFieldsSet
1.1137 + && (zone instanceof ZoneInfo) && !((ZoneInfo)zone).isDirty()) {
1.1138 + return;
1.1139 + }
1.1140 + time = millis;
1.1141 + isTimeSet = true;
1.1142 + areFieldsSet = false;
1.1143 + computeFields();
1.1144 + areAllFieldsSet = areFieldsSet = true;
1.1145 + }
1.1146 +
1.1147 + /**
1.1148 + * Returns the value of the given calendar field. In lenient mode,
1.1149 + * all calendar fields are normalized. In non-lenient mode, all
1.1150 + * calendar fields are validated and this method throws an
1.1151 + * exception if any calendar fields have out-of-range values. The
1.1152 + * normalization and validation are handled by the
1.1153 + * {@link #complete()} method, which process is calendar
1.1154 + * system dependent.
1.1155 + *
1.1156 + * @param field the given calendar field.
1.1157 + * @return the value for the given calendar field.
1.1158 + * @throws ArrayIndexOutOfBoundsException if the specified field is out of range
1.1159 + * (<code>field < 0 || field >= FIELD_COUNT</code>).
1.1160 + * @see #set(int,int)
1.1161 + * @see #complete()
1.1162 + */
1.1163 + public int get(int field)
1.1164 + {
1.1165 + complete();
1.1166 + return internalGet(field);
1.1167 + }
1.1168 +
1.1169 + /**
1.1170 + * Returns the value of the given calendar field. This method does
1.1171 + * not involve normalization or validation of the field value.
1.1172 + *
1.1173 + * @param field the given calendar field.
1.1174 + * @return the value for the given calendar field.
1.1175 + * @see #get(int)
1.1176 + */
1.1177 + protected final int internalGet(int field)
1.1178 + {
1.1179 + return fields[field];
1.1180 + }
1.1181 +
1.1182 + /**
1.1183 + * Sets the value of the given calendar field. This method does
1.1184 + * not affect any setting state of the field in this
1.1185 + * <code>Calendar</code> instance.
1.1186 + *
1.1187 + * @throws IndexOutOfBoundsException if the specified field is out of range
1.1188 + * (<code>field < 0 || field >= FIELD_COUNT</code>).
1.1189 + * @see #areFieldsSet
1.1190 + * @see #isTimeSet
1.1191 + * @see #areAllFieldsSet
1.1192 + * @see #set(int,int)
1.1193 + */
1.1194 + final void internalSet(int field, int value)
1.1195 + {
1.1196 + fields[field] = value;
1.1197 + }
1.1198 +
1.1199 + /**
1.1200 + * Sets the given calendar field to the given value. The value is not
1.1201 + * interpreted by this method regardless of the leniency mode.
1.1202 + *
1.1203 + * @param field the given calendar field.
1.1204 + * @param value the value to be set for the given calendar field.
1.1205 + * @throws ArrayIndexOutOfBoundsException if the specified field is out of range
1.1206 + * (<code>field < 0 || field >= FIELD_COUNT</code>).
1.1207 + * in non-lenient mode.
1.1208 + * @see #set(int,int,int)
1.1209 + * @see #set(int,int,int,int,int)
1.1210 + * @see #set(int,int,int,int,int,int)
1.1211 + * @see #get(int)
1.1212 + */
1.1213 + public void set(int field, int value)
1.1214 + {
1.1215 + // If the fields are partially normalized, calculate all the
1.1216 + // fields before changing any fields.
1.1217 + if (areFieldsSet && !areAllFieldsSet) {
1.1218 + computeFields();
1.1219 + }
1.1220 + internalSet(field, value);
1.1221 + isTimeSet = false;
1.1222 + areFieldsSet = false;
1.1223 + isSet[field] = true;
1.1224 + stamp[field] = nextStamp++;
1.1225 + if (nextStamp == Integer.MAX_VALUE) {
1.1226 + adjustStamp();
1.1227 + }
1.1228 + }
1.1229 +
1.1230 + /**
1.1231 + * Sets the values for the calendar fields <code>YEAR</code>,
1.1232 + * <code>MONTH</code>, and <code>DAY_OF_MONTH</code>.
1.1233 + * Previous values of other calendar fields are retained. If this is not desired,
1.1234 + * call {@link #clear()} first.
1.1235 + *
1.1236 + * @param year the value used to set the <code>YEAR</code> calendar field.
1.1237 + * @param month the value used to set the <code>MONTH</code> calendar field.
1.1238 + * Month value is 0-based. e.g., 0 for January.
1.1239 + * @param date the value used to set the <code>DAY_OF_MONTH</code> calendar field.
1.1240 + * @see #set(int,int)
1.1241 + * @see #set(int,int,int,int,int)
1.1242 + * @see #set(int,int,int,int,int,int)
1.1243 + */
1.1244 + public final void set(int year, int month, int date)
1.1245 + {
1.1246 + set(YEAR, year);
1.1247 + set(MONTH, month);
1.1248 + set(DATE, date);
1.1249 + }
1.1250 +
1.1251 + /**
1.1252 + * Sets the values for the calendar fields <code>YEAR</code>,
1.1253 + * <code>MONTH</code>, <code>DAY_OF_MONTH</code>,
1.1254 + * <code>HOUR_OF_DAY</code>, and <code>MINUTE</code>.
1.1255 + * Previous values of other fields are retained. If this is not desired,
1.1256 + * call {@link #clear()} first.
1.1257 + *
1.1258 + * @param year the value used to set the <code>YEAR</code> calendar field.
1.1259 + * @param month the value used to set the <code>MONTH</code> calendar field.
1.1260 + * Month value is 0-based. e.g., 0 for January.
1.1261 + * @param date the value used to set the <code>DAY_OF_MONTH</code> calendar field.
1.1262 + * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field.
1.1263 + * @param minute the value used to set the <code>MINUTE</code> calendar field.
1.1264 + * @see #set(int,int)
1.1265 + * @see #set(int,int,int)
1.1266 + * @see #set(int,int,int,int,int,int)
1.1267 + */
1.1268 + public final void set(int year, int month, int date, int hourOfDay, int minute)
1.1269 + {
1.1270 + set(YEAR, year);
1.1271 + set(MONTH, month);
1.1272 + set(DATE, date);
1.1273 + set(HOUR_OF_DAY, hourOfDay);
1.1274 + set(MINUTE, minute);
1.1275 + }
1.1276 +
1.1277 + /**
1.1278 + * Sets the values for the fields <code>YEAR</code>, <code>MONTH</code>,
1.1279 + * <code>DAY_OF_MONTH</code>, <code>HOUR</code>, <code>MINUTE</code>, and
1.1280 + * <code>SECOND</code>.
1.1281 + * Previous values of other fields are retained. If this is not desired,
1.1282 + * call {@link #clear()} first.
1.1283 + *
1.1284 + * @param year the value used to set the <code>YEAR</code> calendar field.
1.1285 + * @param month the value used to set the <code>MONTH</code> calendar field.
1.1286 + * Month value is 0-based. e.g., 0 for January.
1.1287 + * @param date the value used to set the <code>DAY_OF_MONTH</code> calendar field.
1.1288 + * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field.
1.1289 + * @param minute the value used to set the <code>MINUTE</code> calendar field.
1.1290 + * @param second the value used to set the <code>SECOND</code> calendar field.
1.1291 + * @see #set(int,int)
1.1292 + * @see #set(int,int,int)
1.1293 + * @see #set(int,int,int,int,int)
1.1294 + */
1.1295 + public final void set(int year, int month, int date, int hourOfDay, int minute,
1.1296 + int second)
1.1297 + {
1.1298 + set(YEAR, year);
1.1299 + set(MONTH, month);
1.1300 + set(DATE, date);
1.1301 + set(HOUR_OF_DAY, hourOfDay);
1.1302 + set(MINUTE, minute);
1.1303 + set(SECOND, second);
1.1304 + }
1.1305 +
1.1306 + /**
1.1307 + * Sets all the calendar field values and the time value
1.1308 + * (millisecond offset from the <a href="#Epoch">Epoch</a>) of
1.1309 + * this <code>Calendar</code> undefined. This means that {@link
1.1310 + * #isSet(int) isSet()} will return <code>false</code> for all the
1.1311 + * calendar fields, and the date and time calculations will treat
1.1312 + * the fields as if they had never been set. A
1.1313 + * <code>Calendar</code> implementation class may use its specific
1.1314 + * default field values for date/time calculations. For example,
1.1315 + * <code>GregorianCalendar</code> uses 1970 if the
1.1316 + * <code>YEAR</code> field value is undefined.
1.1317 + *
1.1318 + * @see #clear(int)
1.1319 + */
1.1320 + public final void clear()
1.1321 + {
1.1322 + for (int i = 0; i < fields.length; ) {
1.1323 + stamp[i] = fields[i] = 0; // UNSET == 0
1.1324 + isSet[i++] = false;
1.1325 + }
1.1326 + areAllFieldsSet = areFieldsSet = false;
1.1327 + isTimeSet = false;
1.1328 + }
1.1329 +
1.1330 + /**
1.1331 + * Sets the given calendar field value and the time value
1.1332 + * (millisecond offset from the <a href="#Epoch">Epoch</a>) of
1.1333 + * this <code>Calendar</code> undefined. This means that {@link
1.1334 + * #isSet(int) isSet(field)} will return <code>false</code>, and
1.1335 + * the date and time calculations will treat the field as if it
1.1336 + * had never been set. A <code>Calendar</code> implementation
1.1337 + * class may use the field's specific default value for date and
1.1338 + * time calculations.
1.1339 + *
1.1340 + * <p>The {@link #HOUR_OF_DAY}, {@link #HOUR} and {@link #AM_PM}
1.1341 + * fields are handled independently and the <a
1.1342 + * href="#time_resolution">the resolution rule for the time of
1.1343 + * day</a> is applied. Clearing one of the fields doesn't reset
1.1344 + * the hour of day value of this <code>Calendar</code>. Use {@link
1.1345 + * #set(int,int) set(Calendar.HOUR_OF_DAY, 0)} to reset the hour
1.1346 + * value.
1.1347 + *
1.1348 + * @param field the calendar field to be cleared.
1.1349 + * @see #clear()
1.1350 + */
1.1351 + public final void clear(int field)
1.1352 + {
1.1353 + fields[field] = 0;
1.1354 + stamp[field] = UNSET;
1.1355 + isSet[field] = false;
1.1356 +
1.1357 + areAllFieldsSet = areFieldsSet = false;
1.1358 + isTimeSet = false;
1.1359 + }
1.1360 +
1.1361 + /**
1.1362 + * Determines if the given calendar field has a value set,
1.1363 + * including cases that the value has been set by internal fields
1.1364 + * calculations triggered by a <code>get</code> method call.
1.1365 + *
1.1366 + * @return <code>true</code> if the given calendar field has a value set;
1.1367 + * <code>false</code> otherwise.
1.1368 + */
1.1369 + public final boolean isSet(int field)
1.1370 + {
1.1371 + return stamp[field] != UNSET;
1.1372 + }
1.1373 +
1.1374 + /**
1.1375 + * Returns the string representation of the calendar
1.1376 + * <code>field</code> value in the given <code>style</code> and
1.1377 + * <code>locale</code>. If no string representation is
1.1378 + * applicable, <code>null</code> is returned. This method calls
1.1379 + * {@link Calendar#get(int) get(field)} to get the calendar
1.1380 + * <code>field</code> value if the string representation is
1.1381 + * applicable to the given calendar <code>field</code>.
1.1382 + *
1.1383 + * <p>For example, if this <code>Calendar</code> is a
1.1384 + * <code>GregorianCalendar</code> and its date is 2005-01-01, then
1.1385 + * the string representation of the {@link #MONTH} field would be
1.1386 + * "January" in the long style in an English locale or "Jan" in
1.1387 + * the short style. However, no string representation would be
1.1388 + * available for the {@link #DAY_OF_MONTH} field, and this method
1.1389 + * would return <code>null</code>.
1.1390 + *
1.1391 + * <p>The default implementation supports the calendar fields for
1.1392 + * which a {@link DateFormatSymbols} has names in the given
1.1393 + * <code>locale</code>.
1.1394 + *
1.1395 + * @param field
1.1396 + * the calendar field for which the string representation
1.1397 + * is returned
1.1398 + * @param style
1.1399 + * the style applied to the string representation; one of
1.1400 + * {@link #SHORT} or {@link #LONG}.
1.1401 + * @param locale
1.1402 + * the locale for the string representation
1.1403 + * @return the string representation of the given
1.1404 + * <code>field</code> in the given <code>style</code>, or
1.1405 + * <code>null</code> if no string representation is
1.1406 + * applicable.
1.1407 + * @exception IllegalArgumentException
1.1408 + * if <code>field</code> or <code>style</code> is invalid,
1.1409 + * or if this <code>Calendar</code> is non-lenient and any
1.1410 + * of the calendar fields have invalid values
1.1411 + * @exception NullPointerException
1.1412 + * if <code>locale</code> is null
1.1413 + * @since 1.6
1.1414 + */
1.1415 + public String getDisplayName(int field, int style, Locale locale) {
1.1416 + if (!checkDisplayNameParams(field, style, ALL_STYLES, LONG, locale,
1.1417 + ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
1.1418 + return null;
1.1419 + }
1.1420 +
1.1421 + DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
1.1422 + String[] strings = getFieldStrings(field, style, symbols);
1.1423 + if (strings != null) {
1.1424 + int fieldValue = get(field);
1.1425 + if (fieldValue < strings.length) {
1.1426 + return strings[fieldValue];
1.1427 + }
1.1428 + }
1.1429 + return null;
1.1430 + }
1.1431 +
1.1432 + /**
1.1433 + * Returns a <code>Map</code> containing all names of the calendar
1.1434 + * <code>field</code> in the given <code>style</code> and
1.1435 + * <code>locale</code> and their corresponding field values. For
1.1436 + * example, if this <code>Calendar</code> is a {@link
1.1437 + * GregorianCalendar}, the returned map would contain "Jan" to
1.1438 + * {@link #JANUARY}, "Feb" to {@link #FEBRUARY}, and so on, in the
1.1439 + * {@linkplain #SHORT short} style in an English locale.
1.1440 + *
1.1441 + * <p>The values of other calendar fields may be taken into
1.1442 + * account to determine a set of display names. For example, if
1.1443 + * this <code>Calendar</code> is a lunisolar calendar system and
1.1444 + * the year value given by the {@link #YEAR} field has a leap
1.1445 + * month, this method would return month names containing the leap
1.1446 + * month name, and month names are mapped to their values specific
1.1447 + * for the year.
1.1448 + *
1.1449 + * <p>The default implementation supports display names contained in
1.1450 + * a {@link DateFormatSymbols}. For example, if <code>field</code>
1.1451 + * is {@link #MONTH} and <code>style</code> is {@link
1.1452 + * #ALL_STYLES}, this method returns a <code>Map</code> containing
1.1453 + * all strings returned by {@link DateFormatSymbols#getShortMonths()}
1.1454 + * and {@link DateFormatSymbols#getMonths()}.
1.1455 + *
1.1456 + * @param field
1.1457 + * the calendar field for which the display names are returned
1.1458 + * @param style
1.1459 + * the style applied to the display names; one of {@link
1.1460 + * #SHORT}, {@link #LONG}, or {@link #ALL_STYLES}.
1.1461 + * @param locale
1.1462 + * the locale for the display names
1.1463 + * @return a <code>Map</code> containing all display names in
1.1464 + * <code>style</code> and <code>locale</code> and their
1.1465 + * field values, or <code>null</code> if no display names
1.1466 + * are defined for <code>field</code>
1.1467 + * @exception IllegalArgumentException
1.1468 + * if <code>field</code> or <code>style</code> is invalid,
1.1469 + * or if this <code>Calendar</code> is non-lenient and any
1.1470 + * of the calendar fields have invalid values
1.1471 + * @exception NullPointerException
1.1472 + * if <code>locale</code> is null
1.1473 + * @since 1.6
1.1474 + */
1.1475 + public Map<String, Integer> getDisplayNames(int field, int style, Locale locale) {
1.1476 + if (!checkDisplayNameParams(field, style, ALL_STYLES, LONG, locale,
1.1477 + ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
1.1478 + return null;
1.1479 + }
1.1480 +
1.1481 + // ALL_STYLES
1.1482 + if (style == ALL_STYLES) {
1.1483 + Map<String,Integer> shortNames = getDisplayNamesImpl(field, SHORT, locale);
1.1484 + if (field == ERA || field == AM_PM) {
1.1485 + return shortNames;
1.1486 + }
1.1487 + Map<String,Integer> longNames = getDisplayNamesImpl(field, LONG, locale);
1.1488 + if (shortNames == null) {
1.1489 + return longNames;
1.1490 + }
1.1491 + if (longNames != null) {
1.1492 + shortNames.putAll(longNames);
1.1493 + }
1.1494 + return shortNames;
1.1495 + }
1.1496 +
1.1497 + // SHORT or LONG
1.1498 + return getDisplayNamesImpl(field, style, locale);
1.1499 + }
1.1500 +
1.1501 + private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
1.1502 + DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
1.1503 + String[] strings = getFieldStrings(field, style, symbols);
1.1504 + if (strings != null) {
1.1505 + Map<String,Integer> names = new HashMap<String,Integer>();
1.1506 + for (int i = 0; i < strings.length; i++) {
1.1507 + if (strings[i].length() == 0) {
1.1508 + continue;
1.1509 + }
1.1510 + names.put(strings[i], i);
1.1511 + }
1.1512 + return names;
1.1513 + }
1.1514 + return null;
1.1515 + }
1.1516 +
1.1517 + boolean checkDisplayNameParams(int field, int style, int minStyle, int maxStyle,
1.1518 + Locale locale, int fieldMask) {
1.1519 + if (field < 0 || field >= fields.length ||
1.1520 + style < minStyle || style > maxStyle) {
1.1521 + throw new IllegalArgumentException();
1.1522 + }
1.1523 + if (locale == null) {
1.1524 + throw new NullPointerException();
1.1525 + }
1.1526 + return isFieldSet(fieldMask, field);
1.1527 + }
1.1528 +
1.1529 + private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) {
1.1530 + String[] strings = null;
1.1531 + switch (field) {
1.1532 + case ERA:
1.1533 + strings = symbols.getEras();
1.1534 + break;
1.1535 +
1.1536 + case MONTH:
1.1537 + strings = (style == LONG) ? symbols.getMonths() : symbols.getShortMonths();
1.1538 + break;
1.1539 +
1.1540 + case DAY_OF_WEEK:
1.1541 + strings = (style == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays();
1.1542 + break;
1.1543 +
1.1544 + case AM_PM:
1.1545 + strings = symbols.getAmPmStrings();
1.1546 + break;
1.1547 + }
1.1548 + return strings;
1.1549 + }
1.1550 +
1.1551 + /**
1.1552 + * Fills in any unset fields in the calendar fields. First, the {@link
1.1553 + * #computeTime()} method is called if the time value (millisecond offset
1.1554 + * from the <a href="#Epoch">Epoch</a>) has not been calculated from
1.1555 + * calendar field values. Then, the {@link #computeFields()} method is
1.1556 + * called to calculate all calendar field values.
1.1557 + */
1.1558 + protected void complete()
1.1559 + {
1.1560 + if (!isTimeSet)
1.1561 + updateTime();
1.1562 + if (!areFieldsSet || !areAllFieldsSet) {
1.1563 + computeFields(); // fills in unset fields
1.1564 + areAllFieldsSet = areFieldsSet = true;
1.1565 + }
1.1566 + }
1.1567 +
1.1568 + /**
1.1569 + * Returns whether the value of the specified calendar field has been set
1.1570 + * externally by calling one of the setter methods rather than by the
1.1571 + * internal time calculation.
1.1572 + *
1.1573 + * @return <code>true</code> if the field has been set externally,
1.1574 + * <code>false</code> otherwise.
1.1575 + * @exception IndexOutOfBoundsException if the specified
1.1576 + * <code>field</code> is out of range
1.1577 + * (<code>field < 0 || field >= FIELD_COUNT</code>).
1.1578 + * @see #selectFields()
1.1579 + * @see #setFieldsComputed(int)
1.1580 + */
1.1581 + final boolean isExternallySet(int field) {
1.1582 + return stamp[field] >= MINIMUM_USER_STAMP;
1.1583 + }
1.1584 +
1.1585 + /**
1.1586 + * Returns a field mask (bit mask) indicating all calendar fields that
1.1587 + * have the state of externally or internally set.
1.1588 + *
1.1589 + * @return a bit mask indicating set state fields
1.1590 + */
1.1591 + final int getSetStateFields() {
1.1592 + int mask = 0;
1.1593 + for (int i = 0; i < fields.length; i++) {
1.1594 + if (stamp[i] != UNSET) {
1.1595 + mask |= 1 << i;
1.1596 + }
1.1597 + }
1.1598 + return mask;
1.1599 + }
1.1600 +
1.1601 + /**
1.1602 + * Sets the state of the specified calendar fields to
1.1603 + * <em>computed</em>. This state means that the specified calendar fields
1.1604 + * have valid values that have been set by internal time calculation
1.1605 + * rather than by calling one of the setter methods.
1.1606 + *
1.1607 + * @param fieldMask the field to be marked as computed.
1.1608 + * @exception IndexOutOfBoundsException if the specified
1.1609 + * <code>field</code> is out of range
1.1610 + * (<code>field < 0 || field >= FIELD_COUNT</code>).
1.1611 + * @see #isExternallySet(int)
1.1612 + * @see #selectFields()
1.1613 + */
1.1614 + final void setFieldsComputed(int fieldMask) {
1.1615 + if (fieldMask == ALL_FIELDS) {
1.1616 + for (int i = 0; i < fields.length; i++) {
1.1617 + stamp[i] = COMPUTED;
1.1618 + isSet[i] = true;
1.1619 + }
1.1620 + areFieldsSet = areAllFieldsSet = true;
1.1621 + } else {
1.1622 + for (int i = 0; i < fields.length; i++) {
1.1623 + if ((fieldMask & 1) == 1) {
1.1624 + stamp[i] = COMPUTED;
1.1625 + isSet[i] = true;
1.1626 + } else {
1.1627 + if (areAllFieldsSet && !isSet[i]) {
1.1628 + areAllFieldsSet = false;
1.1629 + }
1.1630 + }
1.1631 + fieldMask >>>= 1;
1.1632 + }
1.1633 + }
1.1634 + }
1.1635 +
1.1636 + /**
1.1637 + * Sets the state of the calendar fields that are <em>not</em> specified
1.1638 + * by <code>fieldMask</code> to <em>unset</em>. If <code>fieldMask</code>
1.1639 + * specifies all the calendar fields, then the state of this
1.1640 + * <code>Calendar</code> becomes that all the calendar fields are in sync
1.1641 + * with the time value (millisecond offset from the Epoch).
1.1642 + *
1.1643 + * @param fieldMask the field mask indicating which calendar fields are in
1.1644 + * sync with the time value.
1.1645 + * @exception IndexOutOfBoundsException if the specified
1.1646 + * <code>field</code> is out of range
1.1647 + * (<code>field < 0 || field >= FIELD_COUNT</code>).
1.1648 + * @see #isExternallySet(int)
1.1649 + * @see #selectFields()
1.1650 + */
1.1651 + final void setFieldsNormalized(int fieldMask) {
1.1652 + if (fieldMask != ALL_FIELDS) {
1.1653 + for (int i = 0; i < fields.length; i++) {
1.1654 + if ((fieldMask & 1) == 0) {
1.1655 + stamp[i] = fields[i] = 0; // UNSET == 0
1.1656 + isSet[i] = false;
1.1657 + }
1.1658 + fieldMask >>= 1;
1.1659 + }
1.1660 + }
1.1661 +
1.1662 + // Some or all of the fields are in sync with the
1.1663 + // milliseconds, but the stamp values are not normalized yet.
1.1664 + areFieldsSet = true;
1.1665 + areAllFieldsSet = false;
1.1666 + }
1.1667 +
1.1668 + /**
1.1669 + * Returns whether the calendar fields are partially in sync with the time
1.1670 + * value or fully in sync but not stamp values are not normalized yet.
1.1671 + */
1.1672 + final boolean isPartiallyNormalized() {
1.1673 + return areFieldsSet && !areAllFieldsSet;
1.1674 + }
1.1675 +
1.1676 + /**
1.1677 + * Returns whether the calendar fields are fully in sync with the time
1.1678 + * value.
1.1679 + */
1.1680 + final boolean isFullyNormalized() {
1.1681 + return areFieldsSet && areAllFieldsSet;
1.1682 + }
1.1683 +
1.1684 + /**
1.1685 + * Marks this Calendar as not sync'd.
1.1686 + */
1.1687 + final void setUnnormalized() {
1.1688 + areFieldsSet = areAllFieldsSet = false;
1.1689 + }
1.1690 +
1.1691 + /**
1.1692 + * Returns whether the specified <code>field</code> is on in the
1.1693 + * <code>fieldMask</code>.
1.1694 + */
1.1695 + static final boolean isFieldSet(int fieldMask, int field) {
1.1696 + return (fieldMask & (1 << field)) != 0;
1.1697 + }
1.1698 +
1.1699 + /**
1.1700 + * Returns a field mask indicating which calendar field values
1.1701 + * to be used to calculate the time value. The calendar fields are
1.1702 + * returned as a bit mask, each bit of which corresponds to a field, i.e.,
1.1703 + * the mask value of <code>field</code> is <code>(1 <<
1.1704 + * field)</code>. For example, 0x26 represents the <code>YEAR</code>,
1.1705 + * <code>MONTH</code>, and <code>DAY_OF_MONTH</code> fields (i.e., 0x26 is
1.1706 + * equal to
1.1707 + * <code>(1<<YEAR)|(1<<MONTH)|(1<<DAY_OF_MONTH))</code>.
1.1708 + *
1.1709 + * <p>This method supports the calendar fields resolution as described in
1.1710 + * the class description. If the bit mask for a given field is on and its
1.1711 + * field has not been set (i.e., <code>isSet(field)</code> is
1.1712 + * <code>false</code>), then the default value of the field has to be
1.1713 + * used, which case means that the field has been selected because the
1.1714 + * selected combination involves the field.
1.1715 + *
1.1716 + * @return a bit mask of selected fields
1.1717 + * @see #isExternallySet(int)
1.1718 + * @see #setInternallySetState(int)
1.1719 + */
1.1720 + final int selectFields() {
1.1721 + // This implementation has been taken from the GregorianCalendar class.
1.1722 +
1.1723 + // The YEAR field must always be used regardless of its SET
1.1724 + // state because YEAR is a mandatory field to determine the date
1.1725 + // and the default value (EPOCH_YEAR) may change through the
1.1726 + // normalization process.
1.1727 + int fieldMask = YEAR_MASK;
1.1728 +
1.1729 + if (stamp[ERA] != UNSET) {
1.1730 + fieldMask |= ERA_MASK;
1.1731 + }
1.1732 + // Find the most recent group of fields specifying the day within
1.1733 + // the year. These may be any of the following combinations:
1.1734 + // MONTH + DAY_OF_MONTH
1.1735 + // MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
1.1736 + // MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
1.1737 + // DAY_OF_YEAR
1.1738 + // WEEK_OF_YEAR + DAY_OF_WEEK
1.1739 + // We look for the most recent of the fields in each group to determine
1.1740 + // the age of the group. For groups involving a week-related field such
1.1741 + // as WEEK_OF_MONTH, DAY_OF_WEEK_IN_MONTH, or WEEK_OF_YEAR, both the
1.1742 + // week-related field and the DAY_OF_WEEK must be set for the group as a
1.1743 + // whole to be considered. (See bug 4153860 - liu 7/24/98.)
1.1744 + int dowStamp = stamp[DAY_OF_WEEK];
1.1745 + int monthStamp = stamp[MONTH];
1.1746 + int domStamp = stamp[DAY_OF_MONTH];
1.1747 + int womStamp = aggregateStamp(stamp[WEEK_OF_MONTH], dowStamp);
1.1748 + int dowimStamp = aggregateStamp(stamp[DAY_OF_WEEK_IN_MONTH], dowStamp);
1.1749 + int doyStamp = stamp[DAY_OF_YEAR];
1.1750 + int woyStamp = aggregateStamp(stamp[WEEK_OF_YEAR], dowStamp);
1.1751 +
1.1752 + int bestStamp = domStamp;
1.1753 + if (womStamp > bestStamp) {
1.1754 + bestStamp = womStamp;
1.1755 + }
1.1756 + if (dowimStamp > bestStamp) {
1.1757 + bestStamp = dowimStamp;
1.1758 + }
1.1759 + if (doyStamp > bestStamp) {
1.1760 + bestStamp = doyStamp;
1.1761 + }
1.1762 + if (woyStamp > bestStamp) {
1.1763 + bestStamp = woyStamp;
1.1764 + }
1.1765 +
1.1766 + /* No complete combination exists. Look for WEEK_OF_MONTH,
1.1767 + * DAY_OF_WEEK_IN_MONTH, or WEEK_OF_YEAR alone. Treat DAY_OF_WEEK alone
1.1768 + * as DAY_OF_WEEK_IN_MONTH.
1.1769 + */
1.1770 + if (bestStamp == UNSET) {
1.1771 + womStamp = stamp[WEEK_OF_MONTH];
1.1772 + dowimStamp = Math.max(stamp[DAY_OF_WEEK_IN_MONTH], dowStamp);
1.1773 + woyStamp = stamp[WEEK_OF_YEAR];
1.1774 + bestStamp = Math.max(Math.max(womStamp, dowimStamp), woyStamp);
1.1775 +
1.1776 + /* Treat MONTH alone or no fields at all as DAY_OF_MONTH. This may
1.1777 + * result in bestStamp = domStamp = UNSET if no fields are set,
1.1778 + * which indicates DAY_OF_MONTH.
1.1779 + */
1.1780 + if (bestStamp == UNSET) {
1.1781 + bestStamp = domStamp = monthStamp;
1.1782 + }
1.1783 + }
1.1784 +
1.1785 + if (bestStamp == domStamp ||
1.1786 + (bestStamp == womStamp && stamp[WEEK_OF_MONTH] >= stamp[WEEK_OF_YEAR]) ||
1.1787 + (bestStamp == dowimStamp && stamp[DAY_OF_WEEK_IN_MONTH] >= stamp[WEEK_OF_YEAR])) {
1.1788 + fieldMask |= MONTH_MASK;
1.1789 + if (bestStamp == domStamp) {
1.1790 + fieldMask |= DAY_OF_MONTH_MASK;
1.1791 + } else {
1.1792 + assert (bestStamp == womStamp || bestStamp == dowimStamp);
1.1793 + if (dowStamp != UNSET) {
1.1794 + fieldMask |= DAY_OF_WEEK_MASK;
1.1795 + }
1.1796 + if (womStamp == dowimStamp) {
1.1797 + // When they are equal, give the priority to
1.1798 + // WEEK_OF_MONTH for compatibility.
1.1799 + if (stamp[WEEK_OF_MONTH] >= stamp[DAY_OF_WEEK_IN_MONTH]) {
1.1800 + fieldMask |= WEEK_OF_MONTH_MASK;
1.1801 + } else {
1.1802 + fieldMask |= DAY_OF_WEEK_IN_MONTH_MASK;
1.1803 + }
1.1804 + } else {
1.1805 + if (bestStamp == womStamp) {
1.1806 + fieldMask |= WEEK_OF_MONTH_MASK;
1.1807 + } else {
1.1808 + assert (bestStamp == dowimStamp);
1.1809 + if (stamp[DAY_OF_WEEK_IN_MONTH] != UNSET) {
1.1810 + fieldMask |= DAY_OF_WEEK_IN_MONTH_MASK;
1.1811 + }
1.1812 + }
1.1813 + }
1.1814 + }
1.1815 + } else {
1.1816 + assert (bestStamp == doyStamp || bestStamp == woyStamp ||
1.1817 + bestStamp == UNSET);
1.1818 + if (bestStamp == doyStamp) {
1.1819 + fieldMask |= DAY_OF_YEAR_MASK;
1.1820 + } else {
1.1821 + assert (bestStamp == woyStamp);
1.1822 + if (dowStamp != UNSET) {
1.1823 + fieldMask |= DAY_OF_WEEK_MASK;
1.1824 + }
1.1825 + fieldMask |= WEEK_OF_YEAR_MASK;
1.1826 + }
1.1827 + }
1.1828 +
1.1829 + // Find the best set of fields specifying the time of day. There
1.1830 + // are only two possibilities here; the HOUR_OF_DAY or the
1.1831 + // AM_PM and the HOUR.
1.1832 + int hourOfDayStamp = stamp[HOUR_OF_DAY];
1.1833 + int hourStamp = aggregateStamp(stamp[HOUR], stamp[AM_PM]);
1.1834 + bestStamp = (hourStamp > hourOfDayStamp) ? hourStamp : hourOfDayStamp;
1.1835 +
1.1836 + // if bestStamp is still UNSET, then take HOUR or AM_PM. (See 4846659)
1.1837 + if (bestStamp == UNSET) {
1.1838 + bestStamp = Math.max(stamp[HOUR], stamp[AM_PM]);
1.1839 + }
1.1840 +
1.1841 + // Hours
1.1842 + if (bestStamp != UNSET) {
1.1843 + if (bestStamp == hourOfDayStamp) {
1.1844 + fieldMask |= HOUR_OF_DAY_MASK;
1.1845 + } else {
1.1846 + fieldMask |= HOUR_MASK;
1.1847 + if (stamp[AM_PM] != UNSET) {
1.1848 + fieldMask |= AM_PM_MASK;
1.1849 + }
1.1850 + }
1.1851 + }
1.1852 + if (stamp[MINUTE] != UNSET) {
1.1853 + fieldMask |= MINUTE_MASK;
1.1854 + }
1.1855 + if (stamp[SECOND] != UNSET) {
1.1856 + fieldMask |= SECOND_MASK;
1.1857 + }
1.1858 + if (stamp[MILLISECOND] != UNSET) {
1.1859 + fieldMask |= MILLISECOND_MASK;
1.1860 + }
1.1861 + if (stamp[ZONE_OFFSET] >= MINIMUM_USER_STAMP) {
1.1862 + fieldMask |= ZONE_OFFSET_MASK;
1.1863 + }
1.1864 + if (stamp[DST_OFFSET] >= MINIMUM_USER_STAMP) {
1.1865 + fieldMask |= DST_OFFSET_MASK;
1.1866 + }
1.1867 +
1.1868 + return fieldMask;
1.1869 + }
1.1870 +
1.1871 + /**
1.1872 + * Returns the pseudo-time-stamp for two fields, given their
1.1873 + * individual pseudo-time-stamps. If either of the fields
1.1874 + * is unset, then the aggregate is unset. Otherwise, the
1.1875 + * aggregate is the later of the two stamps.
1.1876 + */
1.1877 + private static final int aggregateStamp(int stamp_a, int stamp_b) {
1.1878 + if (stamp_a == UNSET || stamp_b == UNSET) {
1.1879 + return UNSET;
1.1880 + }
1.1881 + return (stamp_a > stamp_b) ? stamp_a : stamp_b;
1.1882 + }
1.1883 +
1.1884 + /**
1.1885 + * Compares this <code>Calendar</code> to the specified
1.1886 + * <code>Object</code>. The result is <code>true</code> if and only if
1.1887 + * the argument is a <code>Calendar</code> object of the same calendar
1.1888 + * system that represents the same time value (millisecond offset from the
1.1889 + * <a href="#Epoch">Epoch</a>) under the same
1.1890 + * <code>Calendar</code> parameters as this object.
1.1891 + *
1.1892 + * <p>The <code>Calendar</code> parameters are the values represented
1.1893 + * by the <code>isLenient</code>, <code>getFirstDayOfWeek</code>,
1.1894 + * <code>getMinimalDaysInFirstWeek</code> and <code>getTimeZone</code>
1.1895 + * methods. If there is any difference in those parameters
1.1896 + * between the two <code>Calendar</code>s, this method returns
1.1897 + * <code>false</code>.
1.1898 + *
1.1899 + * <p>Use the {@link #compareTo(Calendar) compareTo} method to
1.1900 + * compare only the time values.
1.1901 + *
1.1902 + * @param obj the object to compare with.
1.1903 + * @return <code>true</code> if this object is equal to <code>obj</code>;
1.1904 + * <code>false</code> otherwise.
1.1905 + */
1.1906 + public boolean equals(Object obj) {
1.1907 + if (this == obj)
1.1908 + return true;
1.1909 + try {
1.1910 + Calendar that = (Calendar)obj;
1.1911 + return compareTo(getMillisOf(that)) == 0 &&
1.1912 + lenient == that.lenient &&
1.1913 + firstDayOfWeek == that.firstDayOfWeek &&
1.1914 + minimalDaysInFirstWeek == that.minimalDaysInFirstWeek &&
1.1915 + zone.equals(that.zone);
1.1916 + } catch (Exception e) {
1.1917 + // Note: GregorianCalendar.computeTime throws
1.1918 + // IllegalArgumentException if the ERA value is invalid
1.1919 + // even it's in lenient mode.
1.1920 + }
1.1921 + return false;
1.1922 + }
1.1923 +
1.1924 + /**
1.1925 + * Returns a hash code for this calendar.
1.1926 + *
1.1927 + * @return a hash code value for this object.
1.1928 + * @since 1.2
1.1929 + */
1.1930 + public int hashCode() {
1.1931 + // 'otheritems' represents the hash code for the previous versions.
1.1932 + int otheritems = (lenient ? 1 : 0)
1.1933 + | (firstDayOfWeek << 1)
1.1934 + | (minimalDaysInFirstWeek << 4)
1.1935 + | (zone.hashCode() << 7);
1.1936 + long t = getMillisOf(this);
1.1937 + return (int) t ^ (int)(t >> 32) ^ otheritems;
1.1938 + }
1.1939 +
1.1940 + /**
1.1941 + * Returns whether this <code>Calendar</code> represents a time
1.1942 + * before the time represented by the specified
1.1943 + * <code>Object</code>. This method is equivalent to:
1.1944 + * <pre><blockquote>
1.1945 + * compareTo(when) < 0
1.1946 + * </blockquote></pre>
1.1947 + * if and only if <code>when</code> is a <code>Calendar</code>
1.1948 + * instance. Otherwise, the method returns <code>false</code>.
1.1949 + *
1.1950 + * @param when the <code>Object</code> to be compared
1.1951 + * @return <code>true</code> if the time of this
1.1952 + * <code>Calendar</code> is before the time represented by
1.1953 + * <code>when</code>; <code>false</code> otherwise.
1.1954 + * @see #compareTo(Calendar)
1.1955 + */
1.1956 + public boolean before(Object when) {
1.1957 + return when instanceof Calendar
1.1958 + && compareTo((Calendar)when) < 0;
1.1959 + }
1.1960 +
1.1961 + /**
1.1962 + * Returns whether this <code>Calendar</code> represents a time
1.1963 + * after the time represented by the specified
1.1964 + * <code>Object</code>. This method is equivalent to:
1.1965 + * <pre><blockquote>
1.1966 + * compareTo(when) > 0
1.1967 + * </blockquote></pre>
1.1968 + * if and only if <code>when</code> is a <code>Calendar</code>
1.1969 + * instance. Otherwise, the method returns <code>false</code>.
1.1970 + *
1.1971 + * @param when the <code>Object</code> to be compared
1.1972 + * @return <code>true</code> if the time of this <code>Calendar</code> is
1.1973 + * after the time represented by <code>when</code>; <code>false</code>
1.1974 + * otherwise.
1.1975 + * @see #compareTo(Calendar)
1.1976 + */
1.1977 + public boolean after(Object when) {
1.1978 + return when instanceof Calendar
1.1979 + && compareTo((Calendar)when) > 0;
1.1980 + }
1.1981 +
1.1982 + /**
1.1983 + * Compares the time values (millisecond offsets from the <a
1.1984 + * href="#Epoch">Epoch</a>) represented by two
1.1985 + * <code>Calendar</code> objects.
1.1986 + *
1.1987 + * @param anotherCalendar the <code>Calendar</code> to be compared.
1.1988 + * @return the value <code>0</code> if the time represented by the argument
1.1989 + * is equal to the time represented by this <code>Calendar</code>; a value
1.1990 + * less than <code>0</code> if the time of this <code>Calendar</code> is
1.1991 + * before the time represented by the argument; and a value greater than
1.1992 + * <code>0</code> if the time of this <code>Calendar</code> is after the
1.1993 + * time represented by the argument.
1.1994 + * @exception NullPointerException if the specified <code>Calendar</code> is
1.1995 + * <code>null</code>.
1.1996 + * @exception IllegalArgumentException if the time value of the
1.1997 + * specified <code>Calendar</code> object can't be obtained due to
1.1998 + * any invalid calendar values.
1.1999 + * @since 1.5
1.2000 + */
1.2001 + public int compareTo(Calendar anotherCalendar) {
1.2002 + return compareTo(getMillisOf(anotherCalendar));
1.2003 + }
1.2004 +
1.2005 + /**
1.2006 + * Adds or subtracts the specified amount of time to the given calendar field,
1.2007 + * based on the calendar's rules. For example, to subtract 5 days from
1.2008 + * the current time of the calendar, you can achieve it by calling:
1.2009 + * <p><code>add(Calendar.DAY_OF_MONTH, -5)</code>.
1.2010 + *
1.2011 + * @param field the calendar field.
1.2012 + * @param amount the amount of date or time to be added to the field.
1.2013 + * @see #roll(int,int)
1.2014 + * @see #set(int,int)
1.2015 + */
1.2016 + abstract public void add(int field, int amount);
1.2017 +
1.2018 + /**
1.2019 + * Adds or subtracts (up/down) a single unit of time on the given time
1.2020 + * field without changing larger fields. For example, to roll the current
1.2021 + * date up by one day, you can achieve it by calling:
1.2022 + * <p>roll(Calendar.DATE, true).
1.2023 + * When rolling on the year or Calendar.YEAR field, it will roll the year
1.2024 + * value in the range between 1 and the value returned by calling
1.2025 + * <code>getMaximum(Calendar.YEAR)</code>.
1.2026 + * When rolling on the month or Calendar.MONTH field, other fields like
1.2027 + * date might conflict and, need to be changed. For instance,
1.2028 + * rolling the month on the date 01/31/96 will result in 02/29/96.
1.2029 + * When rolling on the hour-in-day or Calendar.HOUR_OF_DAY field, it will
1.2030 + * roll the hour value in the range between 0 and 23, which is zero-based.
1.2031 + *
1.2032 + * @param field the time field.
1.2033 + * @param up indicates if the value of the specified time field is to be
1.2034 + * rolled up or rolled down. Use true if rolling up, false otherwise.
1.2035 + * @see Calendar#add(int,int)
1.2036 + * @see Calendar#set(int,int)
1.2037 + */
1.2038 + abstract public void roll(int field, boolean up);
1.2039 +
1.2040 + /**
1.2041 + * Adds the specified (signed) amount to the specified calendar field
1.2042 + * without changing larger fields. A negative amount means to roll
1.2043 + * down.
1.2044 + *
1.2045 + * <p>NOTE: This default implementation on <code>Calendar</code> just repeatedly calls the
1.2046 + * version of {@link #roll(int,boolean) roll()} that rolls by one unit. This may not
1.2047 + * always do the right thing. For example, if the <code>DAY_OF_MONTH</code> field is 31,
1.2048 + * rolling through February will leave it set to 28. The <code>GregorianCalendar</code>
1.2049 + * version of this function takes care of this problem. Other subclasses
1.2050 + * should also provide overrides of this function that do the right thing.
1.2051 + *
1.2052 + * @param field the calendar field.
1.2053 + * @param amount the signed amount to add to the calendar <code>field</code>.
1.2054 + * @since 1.2
1.2055 + * @see #roll(int,boolean)
1.2056 + * @see #add(int,int)
1.2057 + * @see #set(int,int)
1.2058 + */
1.2059 + public void roll(int field, int amount)
1.2060 + {
1.2061 + while (amount > 0) {
1.2062 + roll(field, true);
1.2063 + amount--;
1.2064 + }
1.2065 + while (amount < 0) {
1.2066 + roll(field, false);
1.2067 + amount++;
1.2068 + }
1.2069 + }
1.2070 +
1.2071 + /**
1.2072 + * Sets the time zone with the given time zone value.
1.2073 + *
1.2074 + * @param value the given time zone.
1.2075 + */
1.2076 + public void setTimeZone(TimeZone value)
1.2077 + {
1.2078 + zone = value;
1.2079 + sharedZone = false;
1.2080 + /* Recompute the fields from the time using the new zone. This also
1.2081 + * works if isTimeSet is false (after a call to set()). In that case
1.2082 + * the time will be computed from the fields using the new zone, then
1.2083 + * the fields will get recomputed from that. Consider the sequence of
1.2084 + * calls: cal.setTimeZone(EST); cal.set(HOUR, 1); cal.setTimeZone(PST).
1.2085 + * Is cal set to 1 o'clock EST or 1 o'clock PST? Answer: PST. More
1.2086 + * generally, a call to setTimeZone() affects calls to set() BEFORE AND
1.2087 + * AFTER it up to the next call to complete().
1.2088 + */
1.2089 + areAllFieldsSet = areFieldsSet = false;
1.2090 + }
1.2091 +
1.2092 + /**
1.2093 + * Gets the time zone.
1.2094 + *
1.2095 + * @return the time zone object associated with this calendar.
1.2096 + */
1.2097 + public TimeZone getTimeZone()
1.2098 + {
1.2099 + // If the TimeZone object is shared by other Calendar instances, then
1.2100 + // create a clone.
1.2101 + if (sharedZone) {
1.2102 + zone = (TimeZone) zone.clone();
1.2103 + sharedZone = false;
1.2104 + }
1.2105 + return zone;
1.2106 + }
1.2107 +
1.2108 + /**
1.2109 + * Returns the time zone (without cloning).
1.2110 + */
1.2111 + TimeZone getZone() {
1.2112 + return zone;
1.2113 + }
1.2114 +
1.2115 + /**
1.2116 + * Sets the sharedZone flag to <code>shared</code>.
1.2117 + */
1.2118 + void setZoneShared(boolean shared) {
1.2119 + sharedZone = shared;
1.2120 + }
1.2121 +
1.2122 + /**
1.2123 + * Specifies whether or not date/time interpretation is to be lenient. With
1.2124 + * lenient interpretation, a date such as "February 942, 1996" will be
1.2125 + * treated as being equivalent to the 941st day after February 1, 1996.
1.2126 + * With strict (non-lenient) interpretation, such dates will cause an exception to be
1.2127 + * thrown. The default is lenient.
1.2128 + *
1.2129 + * @param lenient <code>true</code> if the lenient mode is to be turned
1.2130 + * on; <code>false</code> if it is to be turned off.
1.2131 + * @see #isLenient()
1.2132 + * @see java.text.DateFormat#setLenient
1.2133 + */
1.2134 + public void setLenient(boolean lenient)
1.2135 + {
1.2136 + this.lenient = lenient;
1.2137 + }
1.2138 +
1.2139 + /**
1.2140 + * Tells whether date/time interpretation is to be lenient.
1.2141 + *
1.2142 + * @return <code>true</code> if the interpretation mode of this calendar is lenient;
1.2143 + * <code>false</code> otherwise.
1.2144 + * @see #setLenient(boolean)
1.2145 + */
1.2146 + public boolean isLenient()
1.2147 + {
1.2148 + return lenient;
1.2149 + }
1.2150 +
1.2151 + /**
1.2152 + * Sets what the first day of the week is; e.g., <code>SUNDAY</code> in the U.S.,
1.2153 + * <code>MONDAY</code> in France.
1.2154 + *
1.2155 + * @param value the given first day of the week.
1.2156 + * @see #getFirstDayOfWeek()
1.2157 + * @see #getMinimalDaysInFirstWeek()
1.2158 + */
1.2159 + public void setFirstDayOfWeek(int value)
1.2160 + {
1.2161 + if (firstDayOfWeek == value) {
1.2162 + return;
1.2163 + }
1.2164 + firstDayOfWeek = value;
1.2165 + invalidateWeekFields();
1.2166 + }
1.2167 +
1.2168 + /**
1.2169 + * Gets what the first day of the week is; e.g., <code>SUNDAY</code> in the U.S.,
1.2170 + * <code>MONDAY</code> in France.
1.2171 + *
1.2172 + * @return the first day of the week.
1.2173 + * @see #setFirstDayOfWeek(int)
1.2174 + * @see #getMinimalDaysInFirstWeek()
1.2175 + */
1.2176 + public int getFirstDayOfWeek()
1.2177 + {
1.2178 + return firstDayOfWeek;
1.2179 + }
1.2180 +
1.2181 + /**
1.2182 + * Sets what the minimal days required in the first week of the year are;
1.2183 + * For example, if the first week is defined as one that contains the first
1.2184 + * day of the first month of a year, call this method with value 1. If it
1.2185 + * must be a full week, use value 7.
1.2186 + *
1.2187 + * @param value the given minimal days required in the first week
1.2188 + * of the year.
1.2189 + * @see #getMinimalDaysInFirstWeek()
1.2190 + */
1.2191 + public void setMinimalDaysInFirstWeek(int value)
1.2192 + {
1.2193 + if (minimalDaysInFirstWeek == value) {
1.2194 + return;
1.2195 + }
1.2196 + minimalDaysInFirstWeek = value;
1.2197 + invalidateWeekFields();
1.2198 + }
1.2199 +
1.2200 + /**
1.2201 + * Gets what the minimal days required in the first week of the year are;
1.2202 + * e.g., if the first week is defined as one that contains the first day
1.2203 + * of the first month of a year, this method returns 1. If
1.2204 + * the minimal days required must be a full week, this method
1.2205 + * returns 7.
1.2206 + *
1.2207 + * @return the minimal days required in the first week of the year.
1.2208 + * @see #setMinimalDaysInFirstWeek(int)
1.2209 + */
1.2210 + public int getMinimalDaysInFirstWeek()
1.2211 + {
1.2212 + return minimalDaysInFirstWeek;
1.2213 + }
1.2214 +
1.2215 + /**
1.2216 + * Returns whether this {@code Calendar} supports week dates.
1.2217 + *
1.2218 + * <p>The default implementation of this method returns {@code false}.
1.2219 + *
1.2220 + * @return {@code true} if this {@code Calendar} supports week dates;
1.2221 + * {@code false} otherwise.
1.2222 + * @see #getWeekYear()
1.2223 + * @see #setWeekDate(int,int,int)
1.2224 + * @see #getWeeksInWeekYear()
1.2225 + * @since 1.7
1.2226 + */
1.2227 + public boolean isWeekDateSupported() {
1.2228 + return false;
1.2229 + }
1.2230 +
1.2231 + /**
1.2232 + * Returns the week year represented by this {@code Calendar}. The
1.2233 + * week year is in sync with the week cycle. The {@linkplain
1.2234 + * #getFirstDayOfWeek() first day of the first week} is the first
1.2235 + * day of the week year.
1.2236 + *
1.2237 + * <p>The default implementation of this method throws an
1.2238 + * {@link UnsupportedOperationException}.
1.2239 + *
1.2240 + * @return the week year of this {@code Calendar}
1.2241 + * @exception UnsupportedOperationException
1.2242 + * if any week year numbering isn't supported
1.2243 + * in this {@code Calendar}.
1.2244 + * @see #isWeekDateSupported()
1.2245 + * @see #getFirstDayOfWeek()
1.2246 + * @see #getMinimalDaysInFirstWeek()
1.2247 + * @since 1.7
1.2248 + */
1.2249 + public int getWeekYear() {
1.2250 + throw new UnsupportedOperationException();
1.2251 + }
1.2252 +
1.2253 + /**
1.2254 + * Sets the date of this {@code Calendar} with the the given date
1.2255 + * specifiers - week year, week of year, and day of week.
1.2256 + *
1.2257 + * <p>Unlike the {@code set} method, all of the calendar fields
1.2258 + * and {@code time} values are calculated upon return.
1.2259 + *
1.2260 + * <p>If {@code weekOfYear} is out of the valid week-of-year range
1.2261 + * in {@code weekYear}, the {@code weekYear} and {@code
1.2262 + * weekOfYear} values are adjusted in lenient mode, or an {@code
1.2263 + * IllegalArgumentException} is thrown in non-lenient mode.
1.2264 + *
1.2265 + * <p>The default implementation of this method throws an
1.2266 + * {@code UnsupportedOperationException}.
1.2267 + *
1.2268 + * @param weekYear the week year
1.2269 + * @param weekOfYear the week number based on {@code weekYear}
1.2270 + * @param dayOfWeek the day of week value: one of the constants
1.2271 + * for the {@link #DAY_OF_WEEK} field: {@link
1.2272 + * #SUNDAY}, ..., {@link #SATURDAY}.
1.2273 + * @exception IllegalArgumentException
1.2274 + * if any of the given date specifiers is invalid
1.2275 + * or any of the calendar fields are inconsistent
1.2276 + * with the given date specifiers in non-lenient mode
1.2277 + * @exception UnsupportedOperationException
1.2278 + * if any week year numbering isn't supported in this
1.2279 + * {@code Calendar}.
1.2280 + * @see #isWeekDateSupported()
1.2281 + * @see #getFirstDayOfWeek()
1.2282 + * @see #getMinimalDaysInFirstWeek()
1.2283 + * @since 1.7
1.2284 + */
1.2285 + public void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek) {
1.2286 + throw new UnsupportedOperationException();
1.2287 + }
1.2288 +
1.2289 + /**
1.2290 + * Returns the number of weeks in the week year represented by this
1.2291 + * {@code Calendar}.
1.2292 + *
1.2293 + * <p>The default implementation of this method throws an
1.2294 + * {@code UnsupportedOperationException}.
1.2295 + *
1.2296 + * @return the number of weeks in the week year.
1.2297 + * @exception UnsupportedOperationException
1.2298 + * if any week year numbering isn't supported in this
1.2299 + * {@code Calendar}.
1.2300 + * @see #WEEK_OF_YEAR
1.2301 + * @see #isWeekDateSupported()
1.2302 + * @see #getWeekYear()
1.2303 + * @see #getActualMaximum(int)
1.2304 + * @since 1.7
1.2305 + */
1.2306 + public int getWeeksInWeekYear() {
1.2307 + throw new UnsupportedOperationException();
1.2308 + }
1.2309 +
1.2310 + /**
1.2311 + * Returns the minimum value for the given calendar field of this
1.2312 + * <code>Calendar</code> instance. The minimum value is defined as
1.2313 + * the smallest value returned by the {@link #get(int) get} method
1.2314 + * for any possible time value. The minimum value depends on
1.2315 + * calendar system specific parameters of the instance.
1.2316 + *
1.2317 + * @param field the calendar field.
1.2318 + * @return the minimum value for the given calendar field.
1.2319 + * @see #getMaximum(int)
1.2320 + * @see #getGreatestMinimum(int)
1.2321 + * @see #getLeastMaximum(int)
1.2322 + * @see #getActualMinimum(int)
1.2323 + * @see #getActualMaximum(int)
1.2324 + */
1.2325 + abstract public int getMinimum(int field);
1.2326 +
1.2327 + /**
1.2328 + * Returns the maximum value for the given calendar field of this
1.2329 + * <code>Calendar</code> instance. The maximum value is defined as
1.2330 + * the largest value returned by the {@link #get(int) get} method
1.2331 + * for any possible time value. The maximum value depends on
1.2332 + * calendar system specific parameters of the instance.
1.2333 + *
1.2334 + * @param field the calendar field.
1.2335 + * @return the maximum value for the given calendar field.
1.2336 + * @see #getMinimum(int)
1.2337 + * @see #getGreatestMinimum(int)
1.2338 + * @see #getLeastMaximum(int)
1.2339 + * @see #getActualMinimum(int)
1.2340 + * @see #getActualMaximum(int)
1.2341 + */
1.2342 + abstract public int getMaximum(int field);
1.2343 +
1.2344 + /**
1.2345 + * Returns the highest minimum value for the given calendar field
1.2346 + * of this <code>Calendar</code> instance. The highest minimum
1.2347 + * value is defined as the largest value returned by {@link
1.2348 + * #getActualMinimum(int)} for any possible time value. The
1.2349 + * greatest minimum value depends on calendar system specific
1.2350 + * parameters of the instance.
1.2351 + *
1.2352 + * @param field the calendar field.
1.2353 + * @return the highest minimum value for the given calendar field.
1.2354 + * @see #getMinimum(int)
1.2355 + * @see #getMaximum(int)
1.2356 + * @see #getLeastMaximum(int)
1.2357 + * @see #getActualMinimum(int)
1.2358 + * @see #getActualMaximum(int)
1.2359 + */
1.2360 + abstract public int getGreatestMinimum(int field);
1.2361 +
1.2362 + /**
1.2363 + * Returns the lowest maximum value for the given calendar field
1.2364 + * of this <code>Calendar</code> instance. The lowest maximum
1.2365 + * value is defined as the smallest value returned by {@link
1.2366 + * #getActualMaximum(int)} for any possible time value. The least
1.2367 + * maximum value depends on calendar system specific parameters of
1.2368 + * the instance. For example, a <code>Calendar</code> for the
1.2369 + * Gregorian calendar system returns 28 for the
1.2370 + * <code>DAY_OF_MONTH</code> field, because the 28th is the last
1.2371 + * day of the shortest month of this calendar, February in a
1.2372 + * common year.
1.2373 + *
1.2374 + * @param field the calendar field.
1.2375 + * @return the lowest maximum value for the given calendar field.
1.2376 + * @see #getMinimum(int)
1.2377 + * @see #getMaximum(int)
1.2378 + * @see #getGreatestMinimum(int)
1.2379 + * @see #getActualMinimum(int)
1.2380 + * @see #getActualMaximum(int)
1.2381 + */
1.2382 + abstract public int getLeastMaximum(int field);
1.2383 +
1.2384 + /**
1.2385 + * Returns the minimum value that the specified calendar field
1.2386 + * could have, given the time value of this <code>Calendar</code>.
1.2387 + *
1.2388 + * <p>The default implementation of this method uses an iterative
1.2389 + * algorithm to determine the actual minimum value for the
1.2390 + * calendar field. Subclasses should, if possible, override this
1.2391 + * with a more efficient implementation - in many cases, they can
1.2392 + * simply return <code>getMinimum()</code>.
1.2393 + *
1.2394 + * @param field the calendar field
1.2395 + * @return the minimum of the given calendar field for the time
1.2396 + * value of this <code>Calendar</code>
1.2397 + * @see #getMinimum(int)
1.2398 + * @see #getMaximum(int)
1.2399 + * @see #getGreatestMinimum(int)
1.2400 + * @see #getLeastMaximum(int)
1.2401 + * @see #getActualMaximum(int)
1.2402 + * @since 1.2
1.2403 + */
1.2404 + public int getActualMinimum(int field) {
1.2405 + int fieldValue = getGreatestMinimum(field);
1.2406 + int endValue = getMinimum(field);
1.2407 +
1.2408 + // if we know that the minimum value is always the same, just return it
1.2409 + if (fieldValue == endValue) {
1.2410 + return fieldValue;
1.2411 + }
1.2412 +
1.2413 + // clone the calendar so we don't mess with the real one, and set it to
1.2414 + // accept anything for the field values
1.2415 + Calendar work = (Calendar)this.clone();
1.2416 + work.setLenient(true);
1.2417 +
1.2418 + // now try each value from getLeastMaximum() to getMaximum() one by one until
1.2419 + // we get a value that normalizes to another value. The last value that
1.2420 + // normalizes to itself is the actual minimum for the current date
1.2421 + int result = fieldValue;
1.2422 +
1.2423 + do {
1.2424 + work.set(field, fieldValue);
1.2425 + if (work.get(field) != fieldValue) {
1.2426 + break;
1.2427 + } else {
1.2428 + result = fieldValue;
1.2429 + fieldValue--;
1.2430 + }
1.2431 + } while (fieldValue >= endValue);
1.2432 +
1.2433 + return result;
1.2434 + }
1.2435 +
1.2436 + /**
1.2437 + * Returns the maximum value that the specified calendar field
1.2438 + * could have, given the time value of this
1.2439 + * <code>Calendar</code>. For example, the actual maximum value of
1.2440 + * the <code>MONTH</code> field is 12 in some years, and 13 in
1.2441 + * other years in the Hebrew calendar system.
1.2442 + *
1.2443 + * <p>The default implementation of this method uses an iterative
1.2444 + * algorithm to determine the actual maximum value for the
1.2445 + * calendar field. Subclasses should, if possible, override this
1.2446 + * with a more efficient implementation.
1.2447 + *
1.2448 + * @param field the calendar field
1.2449 + * @return the maximum of the given calendar field for the time
1.2450 + * value of this <code>Calendar</code>
1.2451 + * @see #getMinimum(int)
1.2452 + * @see #getMaximum(int)
1.2453 + * @see #getGreatestMinimum(int)
1.2454 + * @see #getLeastMaximum(int)
1.2455 + * @see #getActualMinimum(int)
1.2456 + * @since 1.2
1.2457 + */
1.2458 + public int getActualMaximum(int field) {
1.2459 + int fieldValue = getLeastMaximum(field);
1.2460 + int endValue = getMaximum(field);
1.2461 +
1.2462 + // if we know that the maximum value is always the same, just return it.
1.2463 + if (fieldValue == endValue) {
1.2464 + return fieldValue;
1.2465 + }
1.2466 +
1.2467 + // clone the calendar so we don't mess with the real one, and set it to
1.2468 + // accept anything for the field values.
1.2469 + Calendar work = (Calendar)this.clone();
1.2470 + work.setLenient(true);
1.2471 +
1.2472 + // if we're counting weeks, set the day of the week to Sunday. We know the
1.2473 + // last week of a month or year will contain the first day of the week.
1.2474 + if (field == WEEK_OF_YEAR || field == WEEK_OF_MONTH)
1.2475 + work.set(DAY_OF_WEEK, firstDayOfWeek);
1.2476 +
1.2477 + // now try each value from getLeastMaximum() to getMaximum() one by one until
1.2478 + // we get a value that normalizes to another value. The last value that
1.2479 + // normalizes to itself is the actual maximum for the current date
1.2480 + int result = fieldValue;
1.2481 +
1.2482 + do {
1.2483 + work.set(field, fieldValue);
1.2484 + if (work.get(field) != fieldValue) {
1.2485 + break;
1.2486 + } else {
1.2487 + result = fieldValue;
1.2488 + fieldValue++;
1.2489 + }
1.2490 + } while (fieldValue <= endValue);
1.2491 +
1.2492 + return result;
1.2493 + }
1.2494 +
1.2495 + /**
1.2496 + * Creates and returns a copy of this object.
1.2497 + *
1.2498 + * @return a copy of this object.
1.2499 + */
1.2500 + public Object clone()
1.2501 + {
1.2502 + try {
1.2503 + Calendar other = (Calendar) super.clone();
1.2504 +
1.2505 + other.fields = new int[FIELD_COUNT];
1.2506 + other.isSet = new boolean[FIELD_COUNT];
1.2507 + other.stamp = new int[FIELD_COUNT];
1.2508 + for (int i = 0; i < FIELD_COUNT; i++) {
1.2509 + other.fields[i] = fields[i];
1.2510 + other.stamp[i] = stamp[i];
1.2511 + other.isSet[i] = isSet[i];
1.2512 + }
1.2513 + other.zone = (TimeZone) zone.clone();
1.2514 + return other;
1.2515 + }
1.2516 + catch (CloneNotSupportedException e) {
1.2517 + // this shouldn't happen, since we are Cloneable
1.2518 + throw new InternalError();
1.2519 + }
1.2520 + }
1.2521 +
1.2522 + private static final String[] FIELD_NAME = {
1.2523 + "ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH",
1.2524 + "DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR",
1.2525 + "HOUR_OF_DAY", "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
1.2526 + "DST_OFFSET"
1.2527 + };
1.2528 +
1.2529 + /**
1.2530 + * Returns the name of the specified calendar field.
1.2531 + *
1.2532 + * @param field the calendar field
1.2533 + * @return the calendar field name
1.2534 + * @exception IndexOutOfBoundsException if <code>field</code> is negative,
1.2535 + * equal to or greater then <code>FIELD_COUNT</code>.
1.2536 + */
1.2537 + static final String getFieldName(int field) {
1.2538 + return FIELD_NAME[field];
1.2539 + }
1.2540 +
1.2541 + /**
1.2542 + * Return a string representation of this calendar. This method
1.2543 + * is intended to be used only for debugging purposes, and the
1.2544 + * format of the returned string may vary between implementations.
1.2545 + * The returned string may be empty but may not be <code>null</code>.
1.2546 + *
1.2547 + * @return a string representation of this calendar.
1.2548 + */
1.2549 + public String toString() {
1.2550 + // NOTE: BuddhistCalendar.toString() interprets the string
1.2551 + // produced by this method so that the Gregorian year number
1.2552 + // is substituted by its B.E. year value. It relies on
1.2553 + // "...,YEAR=<year>,..." or "...,YEAR=?,...".
1.2554 + StringBuilder buffer = new StringBuilder(800);
1.2555 + buffer.append(getClass().getName()).append('[');
1.2556 + appendValue(buffer, "time", isTimeSet, time);
1.2557 + buffer.append(",areFieldsSet=").append(areFieldsSet);
1.2558 + buffer.append(",areAllFieldsSet=").append(areAllFieldsSet);
1.2559 + buffer.append(",lenient=").append(lenient);
1.2560 + buffer.append(",zone=").append(zone);
1.2561 + appendValue(buffer, ",firstDayOfWeek", true, (long) firstDayOfWeek);
1.2562 + appendValue(buffer, ",minimalDaysInFirstWeek", true, (long) minimalDaysInFirstWeek);
1.2563 + for (int i = 0; i < FIELD_COUNT; ++i) {
1.2564 + buffer.append(',');
1.2565 + appendValue(buffer, FIELD_NAME[i], isSet(i), (long) fields[i]);
1.2566 + }
1.2567 + buffer.append(']');
1.2568 + return buffer.toString();
1.2569 + }
1.2570 +
1.2571 + // =======================privates===============================
1.2572 +
1.2573 + private static final void appendValue(StringBuilder sb, String item, boolean valid, long value) {
1.2574 + sb.append(item).append('=');
1.2575 + if (valid) {
1.2576 + sb.append(value);
1.2577 + } else {
1.2578 + sb.append('?');
1.2579 + }
1.2580 + }
1.2581 +
1.2582 + /**
1.2583 + * Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent.
1.2584 + * They are used to figure out the week count for a specific date for
1.2585 + * a given locale. These must be set when a Calendar is constructed.
1.2586 + * @param desiredLocale the given locale.
1.2587 + */
1.2588 + private void setWeekCountData(Locale desiredLocale)
1.2589 + {
1.2590 + /* try to get the Locale data from the cache */
1.2591 + int[] data = cachedLocaleData.get(desiredLocale);
1.2592 + if (data == null) { /* cache miss */
1.2593 + ResourceBundle bundle = LocaleData.getCalendarData(desiredLocale);
1.2594 + data = new int[2];
1.2595 + data[0] = Integer.parseInt(bundle.getString("firstDayOfWeek"));
1.2596 + data[1] = Integer.parseInt(bundle.getString("minimalDaysInFirstWeek"));
1.2597 + cachedLocaleData.putIfAbsent(desiredLocale, data);
1.2598 + }
1.2599 + firstDayOfWeek = data[0];
1.2600 + minimalDaysInFirstWeek = data[1];
1.2601 + }
1.2602 +
1.2603 + /**
1.2604 + * Recomputes the time and updates the status fields isTimeSet
1.2605 + * and areFieldsSet. Callers should check isTimeSet and only
1.2606 + * call this method if isTimeSet is false.
1.2607 + */
1.2608 + private void updateTime() {
1.2609 + computeTime();
1.2610 + // The areFieldsSet and areAllFieldsSet values are no longer
1.2611 + // controlled here (as of 1.5).
1.2612 + isTimeSet = true;
1.2613 + }
1.2614 +
1.2615 + private int compareTo(long t) {
1.2616 + long thisTime = getMillisOf(this);
1.2617 + return (thisTime > t) ? 1 : (thisTime == t) ? 0 : -1;
1.2618 + }
1.2619 +
1.2620 + private static final long getMillisOf(Calendar calendar) {
1.2621 + if (calendar.isTimeSet) {
1.2622 + return calendar.time;
1.2623 + }
1.2624 + Calendar cal = (Calendar) calendar.clone();
1.2625 + cal.setLenient(true);
1.2626 + return cal.getTimeInMillis();
1.2627 + }
1.2628 +
1.2629 + /**
1.2630 + * Adjusts the stamp[] values before nextStamp overflow. nextStamp
1.2631 + * is set to the next stamp value upon the return.
1.2632 + */
1.2633 + private final void adjustStamp() {
1.2634 + int max = MINIMUM_USER_STAMP;
1.2635 + int newStamp = MINIMUM_USER_STAMP;
1.2636 +
1.2637 + for (;;) {
1.2638 + int min = Integer.MAX_VALUE;
1.2639 + for (int i = 0; i < stamp.length; i++) {
1.2640 + int v = stamp[i];
1.2641 + if (v >= newStamp && min > v) {
1.2642 + min = v;
1.2643 + }
1.2644 + if (max < v) {
1.2645 + max = v;
1.2646 + }
1.2647 + }
1.2648 + if (max != min && min == Integer.MAX_VALUE) {
1.2649 + break;
1.2650 + }
1.2651 + for (int i = 0; i < stamp.length; i++) {
1.2652 + if (stamp[i] == min) {
1.2653 + stamp[i] = newStamp;
1.2654 + }
1.2655 + }
1.2656 + newStamp++;
1.2657 + if (min == max) {
1.2658 + break;
1.2659 + }
1.2660 + }
1.2661 + nextStamp = newStamp;
1.2662 + }
1.2663 +
1.2664 + /**
1.2665 + * Sets the WEEK_OF_MONTH and WEEK_OF_YEAR fields to new values with the
1.2666 + * new parameter value if they have been calculated internally.
1.2667 + */
1.2668 + private void invalidateWeekFields()
1.2669 + {
1.2670 + if (stamp[WEEK_OF_MONTH] != COMPUTED &&
1.2671 + stamp[WEEK_OF_YEAR] != COMPUTED) {
1.2672 + return;
1.2673 + }
1.2674 +
1.2675 + // We have to check the new values of these fields after changing
1.2676 + // firstDayOfWeek and/or minimalDaysInFirstWeek. If the field values
1.2677 + // have been changed, then set the new values. (4822110)
1.2678 + Calendar cal = (Calendar) clone();
1.2679 + cal.setLenient(true);
1.2680 + cal.clear(WEEK_OF_MONTH);
1.2681 + cal.clear(WEEK_OF_YEAR);
1.2682 +
1.2683 + if (stamp[WEEK_OF_MONTH] == COMPUTED) {
1.2684 + int weekOfMonth = cal.get(WEEK_OF_MONTH);
1.2685 + if (fields[WEEK_OF_MONTH] != weekOfMonth) {
1.2686 + fields[WEEK_OF_MONTH] = weekOfMonth;
1.2687 + }
1.2688 + }
1.2689 +
1.2690 + if (stamp[WEEK_OF_YEAR] == COMPUTED) {
1.2691 + int weekOfYear = cal.get(WEEK_OF_YEAR);
1.2692 + if (fields[WEEK_OF_YEAR] != weekOfYear) {
1.2693 + fields[WEEK_OF_YEAR] = weekOfYear;
1.2694 + }
1.2695 + }
1.2696 + }
1.2697 +
1.2698 + /**
1.2699 + * Save the state of this object to a stream (i.e., serialize it).
1.2700 + *
1.2701 + * Ideally, <code>Calendar</code> would only write out its state data and
1.2702 + * the current time, and not write any field data out, such as
1.2703 + * <code>fields[]</code>, <code>isTimeSet</code>, <code>areFieldsSet</code>,
1.2704 + * and <code>isSet[]</code>. <code>nextStamp</code> also should not be part
1.2705 + * of the persistent state. Unfortunately, this didn't happen before JDK 1.1
1.2706 + * shipped. To be compatible with JDK 1.1, we will always have to write out
1.2707 + * the field values and state flags. However, <code>nextStamp</code> can be
1.2708 + * removed from the serialization stream; this will probably happen in the
1.2709 + * near future.
1.2710 + */
1.2711 + private void writeObject(ObjectOutputStream stream)
1.2712 + throws IOException
1.2713 + {
1.2714 + // Try to compute the time correctly, for the future (stream
1.2715 + // version 2) in which we don't write out fields[] or isSet[].
1.2716 + if (!isTimeSet) {
1.2717 + try {
1.2718 + updateTime();
1.2719 + }
1.2720 + catch (IllegalArgumentException e) {}
1.2721 + }
1.2722 +
1.2723 + // If this Calendar has a ZoneInfo, save it and set a
1.2724 + // SimpleTimeZone equivalent (as a single DST schedule) for
1.2725 + // backward compatibility.
1.2726 + TimeZone savedZone = null;
1.2727 + if (zone instanceof ZoneInfo) {
1.2728 + SimpleTimeZone stz = ((ZoneInfo)zone).getLastRuleInstance();
1.2729 + if (stz == null) {
1.2730 + stz = new SimpleTimeZone(zone.getRawOffset(), zone.getID());
1.2731 + }
1.2732 + savedZone = zone;
1.2733 + zone = stz;
1.2734 + }
1.2735 +
1.2736 + // Write out the 1.1 FCS object.
1.2737 + stream.defaultWriteObject();
1.2738 +
1.2739 + // Write out the ZoneInfo object
1.2740 + // 4802409: we write out even if it is null, a temporary workaround
1.2741 + // the real fix for bug 4844924 in corba-iiop
1.2742 + stream.writeObject(savedZone);
1.2743 + if (savedZone != null) {
1.2744 + zone = savedZone;
1.2745 + }
1.2746 + }
1.2747 +
1.2748 + private static class CalendarAccessControlContext {
1.2749 + private static final AccessControlContext INSTANCE;
1.2750 + static {
1.2751 + RuntimePermission perm = new RuntimePermission("accessClassInPackage.sun.util.calendar");
1.2752 + PermissionCollection perms = perm.newPermissionCollection();
1.2753 + perms.add(perm);
1.2754 + INSTANCE = new AccessControlContext(new ProtectionDomain[] {
1.2755 + new ProtectionDomain(null, perms)
1.2756 + });
1.2757 + }
1.2758 + }
1.2759 +
1.2760 + /**
1.2761 + * Reconstitutes this object from a stream (i.e., deserialize it).
1.2762 + */
1.2763 + private void readObject(ObjectInputStream stream)
1.2764 + throws IOException, ClassNotFoundException
1.2765 + {
1.2766 + final ObjectInputStream input = stream;
1.2767 + input.defaultReadObject();
1.2768 +
1.2769 + stamp = new int[FIELD_COUNT];
1.2770 +
1.2771 + // Starting with version 2 (not implemented yet), we expect that
1.2772 + // fields[], isSet[], isTimeSet, and areFieldsSet may not be
1.2773 + // streamed out anymore. We expect 'time' to be correct.
1.2774 + if (serialVersionOnStream >= 2)
1.2775 + {
1.2776 + isTimeSet = true;
1.2777 + if (fields == null) fields = new int[FIELD_COUNT];
1.2778 + if (isSet == null) isSet = new boolean[FIELD_COUNT];
1.2779 + }
1.2780 + else if (serialVersionOnStream >= 0)
1.2781 + {
1.2782 + for (int i=0; i<FIELD_COUNT; ++i)
1.2783 + stamp[i] = isSet[i] ? COMPUTED : UNSET;
1.2784 + }
1.2785 +
1.2786 + serialVersionOnStream = currentSerialVersion;
1.2787 +
1.2788 + // If there's a ZoneInfo object, use it for zone.
1.2789 + ZoneInfo zi = null;
1.2790 + try {
1.2791 + zi = AccessController.doPrivileged(
1.2792 + new PrivilegedExceptionAction<ZoneInfo>() {
1.2793 + public ZoneInfo run() throws Exception {
1.2794 + return (ZoneInfo) input.readObject();
1.2795 + }
1.2796 + },
1.2797 + CalendarAccessControlContext.INSTANCE);
1.2798 + } catch (PrivilegedActionException pae) {
1.2799 + Exception e = pae.getException();
1.2800 + if (!(e instanceof OptionalDataException)) {
1.2801 + if (e instanceof RuntimeException) {
1.2802 + throw (RuntimeException) e;
1.2803 + } else if (e instanceof IOException) {
1.2804 + throw (IOException) e;
1.2805 + } else if (e instanceof ClassNotFoundException) {
1.2806 + throw (ClassNotFoundException) e;
1.2807 + }
1.2808 + throw new RuntimeException(e);
1.2809 + }
1.2810 + }
1.2811 + if (zi != null) {
1.2812 + zone = zi;
1.2813 + }
1.2814 +
1.2815 + // If the deserialized object has a SimpleTimeZone, try to
1.2816 + // replace it with a ZoneInfo equivalent (as of 1.4) in order
1.2817 + // to be compatible with the SimpleTimeZone-based
1.2818 + // implementation as much as possible.
1.2819 + if (zone instanceof SimpleTimeZone) {
1.2820 + String id = zone.getID();
1.2821 + TimeZone tz = TimeZone.getTimeZone(id);
1.2822 + if (tz != null && tz.hasSameRules(zone) && tz.getID().equals(id)) {
1.2823 + zone = tz;
1.2824 + }
1.2825 + }
1.2826 + }
1.2827 +}