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