calendar.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,741 行 · 第 1/5 页
JAVA
1,741 行
/* * * @(#)Calendar.java 1.57 06/10/10 * * Portions Copyright 2000-2008 Sun Microsystems, Inc. All Rights * Reserved. Use is subject to license terms. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. *//* * (C) Copyright Taligent, Inc. 1996-1998 - All Rights Reserved * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved * * The original version of this source code and documentation is copyrighted * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These * materials are provided under terms of a License Agreement between Taligent * and Sun. This technology is protected by multiple US and International * patents. This notice and attribution to Taligent may not be removed. * Taligent is a registered trademark of Taligent, Inc. * */package java.util;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.text.DateFormat;import sun.text.resources.LocaleData;import sun.util.BuddhistCalendar;import sun.util.calendar.ZoneInfo;/** * <code>Calendar</code> is an abstract base class for converting between * a <code>Date</code> object and a set of integer fields such as * <code>YEAR</code>, <code>MONTH</code>, <code>DAY</code>, <code>HOUR</code>, * and so on. (A <code>Date</code> object represents a specific instant in * time with millisecond precision. See * {@link Date} * for information about the <code>Date</code> class.) * * <p> * Subclasses of <code>Calendar</code> interpret a <code>Date</code> * according to the rules of a specific calendar system. The platform * provides one concrete subclass of <code>Calendar</code>: * <code>GregorianCalendar</code>. Future subclasses could represent * the various types of lunar calendars in use in many parts of the world. * * <p> * Like other locale-sensitive classes, <code>Calendar</code> provides a * class method, <code>getInstance</code>, for getting a generally useful * object of this type. <code>Calendar</code>'s <code>getInstance</code> method * returns a <code>Calendar</code> object whose * time fields have been initialized with the current date and time: * <blockquote> * <pre> * Calendar rightNow = Calendar.getInstance(); * </pre> * </blockquote> * * <p>A <code>Calendar</code> object can produce all the time field values * needed to implement the date-time formatting for a particular language and * calendar style (for example, Japanese-Gregorian, Japanese-Traditional). * <code>Calendar</code> defines the range of values returned by certain fields, * as well as their meaning. For example, the first month of the year has value * <code>MONTH</code> == <code>JANUARY</code> for all calendars. Other values * are defined by the concrete subclass, such as <code>ERA</code> and * <code>YEAR</code>. See individual field documentation and subclass * documentation for details. * * <p>When a <code>Calendar</code> is <em>lenient</em>, it accepts a wider range * of field values than it produces. For example, a lenient * <code>GregorianCalendar</code> interprets <code>MONTH</code> == * <code>JANUARY</code>, <code>DAY_OF_MONTH</code> == 32 as February 1. A * non-lenient <code>GregorianCalendar</code> throws an exception when given * out-of-range field settings. When calendars recompute field values for * return by <code>get()</code>, they normalize them. For example, a * <code>GregorianCalendar</code> always produces <code>DAY_OF_MONTH</code> * values between 1 and the length of the month. * * <p><code>Calendar</code> defines a locale-specific seven day week using two * parameters: the first day of the week and the minimal days in first week * (from 1 to 7). These numbers are taken from the locale resource data when a * <code>Calendar</code> is constructed. They may also be specified explicitly * through the API. * * <p>When setting or getting the <code>WEEK_OF_MONTH</code> or * <code>WEEK_OF_YEAR</code> fields, <code>Calendar</code> must determine the * first week of the month or year as a reference point. The first week of a * month or year is defined as the earliest seven day period beginning on * <code>getFirstDayOfWeek()</code> and containing at least * <code>getMinimalDaysInFirstWeek()</code> days of that month or year. Weeks * numbered ..., -1, 0 precede the first week; weeks numbered 2, 3,... follow * it. Note that the normalized numbering returned by <code>get()</code> may be * different. For example, a specific <code>Calendar</code> subclass may * designate the week before week 1 of a year as week <em>n</em> of the previous * year. * * <p> When computing a <code>Date</code> from time fields, two special * circumstances may arise: there may be insufficient information to compute the * <code>Date</code> (such as only year and month but no day in the month), or * there may be inconsistent information (such as "Tuesday, July 15, 1996" -- * July 15, 1996 is actually a Monday). * * <p> * <strong>Insufficient information.</strong> The calendar will use default * information to specify the missing fields. This may vary by calendar; for * the Gregorian calendar, the default for a field is the same as that of the * start of the epoch: i.e., YEAR = 1970, MONTH = JANUARY, DATE = 1, etc. * * <p> * <strong>Inconsistent information.</strong> If fields conflict, the calendar * will give preference to fields set more recently. For example, when * determining the day, the calendar will look for one of the following * combinations of fields. The most recent combination, as determined by the * most recently set single field, will be used. * * <blockquote> * <pre> * MONTH + DAY_OF_MONTH * MONTH + WEEK_OF_MONTH + DAY_OF_WEEK * MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK * DAY_OF_YEAR * DAY_OF_WEEK + WEEK_OF_YEAR * </pre> * </blockquote> * * For the time of day: * * <blockquote> * <pre> * HOUR_OF_DAY * AM_PM + HOUR * </pre> * </blockquote> * * <p> * <strong>Note:</strong> for some non-Gregorian calendars, different * fields may be necessary for complete disambiguation. For example, a full * specification of the historical Arabic astronomical calendar requires year, * month, day-of-month <em>and</em> day-of-week in some cases. * * <p> * <strong>Note:</strong> There are certain possible ambiguities in * interpretation of certain singular times, which are resolved in the * following ways: * <ol> * <li> 23:59 is the last minute of the day and 00:00 is the first minute of the * next day. Thus, 23:59 on Dec 31, 1999 < 00:00 on Jan 1, 2000 < 00:01 on * Jan 1, 2000. * * <li> Although historically not precise, midnight also belongs to "am", * and noon belongs to "pm", so on the same day, * 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm * </ol> * * <p> * The date or time format strings are not part of the definition of a * calendar, as those must be modifiable or overridable by the user at * runtime. Use {@link DateFormat} * to format dates. * * <p><strong>Field manipulation methods</strong></p> * * <p><code>Calendar</code> fields can be changed using three methods: * <code>set()</code>, <code>add()</code>, and <code>roll()</code>.</p> * * <p><strong><code>set(f, value)</code></strong> changes field * <code>f</code> to <code>value</code>. In addition, it sets an * internal member variable to indicate that field <code>f</code> has * been changed. Although field <code>f</code> is changed immediately, * the calendar's milliseconds is not recomputed until the next call to * <code>get()</code>, <code>getTime()</code>, or * <code>getTimeInMillis()</code> is made. Thus, multiple calls to * <code>set()</code> do not trigger multiple, unnecessary * computations. As a result of changing a field using * <code>set()</code>, other fields may also change, depending on the * field, the field value, and the calendar system. In addition, * <code>get(f)</code> will not necessarily return <code>value</code> * after the fields have been recomputed. The specifics are determined by * the concrete calendar class.</p> * * <p><em>Example</em>: Consider a <code>GregorianCalendar</code> * originally set to August 31, 1999. Calling <code>set(Calendar.MONTH, * Calendar.SEPTEMBER)</code> sets the calendar to September 31, * 1999. This is a temporary internal representation that resolves to * October 1, 1999 if <code>getTime()</code>is then called. However, a * call to <code>set(Calendar.DAY_OF_MONTH, 30)</code> before the call to * <code>getTime()</code> sets the calendar to September 30, 1999, since * no recomputation occurs after <code>set()</code> itself.</p> * * <p><strong><code>add(f, delta)</code></strong> adds <code>delta</code> * to field <code>f</code>. This is equivalent to calling <code>set(f, * get(f) + delta)</code> with two adjustments:</p> * * <blockquote> * <p><strong>Add rule 1</strong>. The value of field <code>f</code> * after the call minus the value of field <code>f</code> before the * call is <code>delta</code>, modulo any overflow that has occurred in * field <code>f</code>. Overflow occurs when a field value exceeds its * range and, as a result, the next larger field is incremented or * decremented and the field value is adjusted back into its range.</p> * * <p><strong>Add rule 2</strong>. If a smaller field is expected to be * invariant, but it is impossible for it to be equal to its * prior value because of changes in its minimum or maximum after field * <code>f</code> is changed, then its value is adjusted to be as close * as possible to its expected value. A smaller field represents a * smaller unit of time. <code>HOUR</code> is a smaller field than * <code>DAY_OF_MONTH</code>. No adjustment is made to smaller fields * that are not expected to be invariant. The calendar system * determines what fields are expected to be invariant.</p> * </blockquote> * * <p>In addition, unlike <code>set()</code>, <code>add()</code> forces * an immediate recomputation of the calendar's milliseconds and all * fields.</p> * * <p><em>Example</em>: Consider a <code>GregorianCalendar</code> * originally set to August 31, 1999. Calling <code>add(Calendar.MONTH, * 13)</code> sets the calendar to September 30, 2000. <strong>Add rule * 1</strong> sets the <code>MONTH</code> field to September, since * adding 13 months to August gives September of the next year. Since * <code>DAY_OF_MONTH</code> cannot be 31 in September in a * <code>GregorianCalendar</code>, <strong>add rule 2</strong> sets the * <code>DAY_OF_MONTH</code> to 30, the closest possible value. Although * it is a smaller field, <code>DAY_OF_WEEK</code> is not adjusted by * rule 2, since it is expected to change when the month changes in a * <code>GregorianCalendar</code>.</p> * * <p><strong><code>roll(f, delta)</code></strong> adds * <code>delta</code> to field <code>f</code> without changing larger * fields. This is equivalent to calling <code>add(f, delta)</code> with * the following adjustment:</p> * * <blockquote> * <p><strong>Roll rule</strong>. Larger fields are unchanged after the * call. A larger field represents a larger unit of * time. <code>DAY_OF_MONTH</code> is a larger field than * <code>HOUR</code>.</p> * </blockquote> * * <p><em>Example</em>: See {@link java.util.GregorianCalendar#roll(int, int)}. * * <p><strong>Usage model</strong>. To motivate the behavior of * <code>add()</code> and <code>roll()</code>, consider a user interface * component with increment and decrement buttons for the month, day, and * year, and an underlying <code>GregorianCalendar</code>. If the * interface reads January 31, 1999 and the user presses the month * increment button, what should it read? If the underlying * implementation uses <code>set()</code>, it might read March 3, 1999. A * better result would be February 28, 1999. Furthermore, if the user * presses the month increment button again, it should read March 31, * 1999, not March 28, 1999. By saving the original date and using either * <code>add()</code> or <code>roll()</code>, depending on whether larger * fields should be affected, the user interface can behave as most users * will intuitively expect.</p> * * @see Date * @see GregorianCalendar * @see TimeZone * @see java.text.DateFormat * @version 1.51 10/17/00 * @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu * @since JDK1.1 */public abstract class Calendar implements Serializable, Cloneable { // Data flow in Calendar // --------------------- // The current time is represented in two ways by Calendar: as UTC // milliseconds from the epoch start (1 January 1970 0:00 UTC), and as local // fields such as MONTH, HOUR, AM_PM, etc. It is possible to compute the // millis from the fields, and vice versa. The data needed to do this // conversion is encapsulated by a TimeZone object owned by the Calendar. // The data provided by the TimeZone object may also be overridden if the // user sets the ZONE_OFFSET and/or DST_OFFSET fields directly. The class // keeps track of what information was most recently set by the caller, and // uses that to compute any other information as needed. // If the user sets the fields using set(), the data flow is as follows. // This is implemented by the Calendar subclass's computeTime() method. // During this process, certain fields may be ignored. The disambiguation // algorithm for resolving which fields to pay attention to is described // above. // local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.) // | // | Using Calendar-specific algorithm // V // local standard millis // | // | Using TimeZone or user-set ZONE_OFFSET / DST_OFFSET // V // UTC millis (in time data member) // If the user sets the UTC millis using setTime(), the data flow is as // follows. This is implemented by the Calendar subclass's computeFields() // method. // UTC millis (in time data member) // | // | Using TimeZone getOffset() // V // local standard millis // | // | Using Calendar-specific algorithm // V // local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.) // In general, a round trip from fields, through local and UTC millis, and // back out to fields is made when necessary. This is implemented by the // complete() method. Resolving a partial set of fields into a UTC millis // value allows all remaining fields to be generated from that value. If // the Calendar is lenient, the fields are also renormalized to standard // ranges when they are regenerated. /** * Field number for <code>get</code> and <code>set</code> indicating the * era, e.g., AD or BC in the Julian calendar. This is a calendar-specific * value; see subclass documentation. * @see GregorianCalendar#AD * @see GregorianCalendar#BC */ public final static int ERA = 0; /** * Field number for <code>get</code> and <code>set</code> indicating the * year. This is a calendar-specific value; see subclass documentation. */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?