📄 calendar.java
字号:
/* * @(#)Calendar.java 1.42 98/09/23 * * (C) Copyright Taligent, Inc. 1996-1998 - All Rights Reserved * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved * * Portions copyright (c) 1996-2002 Sun Microsystems, Inc. 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. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * * Use is subject to license terms. */package java.util;/** * <code>Calendar</code> is an abstract class for getting and setting dates * using a set of integer fields such as * <code>YEAR</code>, <code>MONTH</code>, <code>DAY</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. * * <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. * <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). * * <p> * When computing a <code>Date</code> from time fields, * there may be insufficient information to compute the * <code>Date</code> (such as only year and month but no day in the month). * * <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> In the J2SE calendar, it is * possible to set fields inconsistently. However, in this subset, the * DAY_OF_WEEK field cannot be set, and only a subset of the other J2SE * Calendar fields are included. So it is not possible to set * inconsistent data. * <p> * * <strong>Note:</strong> The ambiguity in interpretation of what day midnight * belongs to, is resolved as so: midnight "belongs" to the following day.<br> * 23:59 on Dec 31, 1969 < 00:00 on Jan 1, 1970.<br> * 12:00 PM is midday, and 12:00 AM is midnight.<br> * 11:59 PM on Jan 1 < 12:00 AM on Jan 2 < 12:01 AM on Jan 2.<br> * 11:59 AM on Mar 10 < 12:00 PM on Mar 10 < 12:01 PM on Mar 10.<br> * 24:00 or greater are invalid. * Hours greater than 12 are invalid in AM/PM mode. * Setting the time will never change the date. * <p> * If equivalent times are entered in AM/PM or 24 hour mode, equality will be * determined by the actual time rather than the entered time. * <p> * * This class is a subset for J2ME of the J2SE Calendar class. * Many methods and variables have been * pruned, and other methods simplified, in an effort to reduce the size * of this class. * * * @see TimeZone * @version 1.0 (J2ME MIDP) * @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu, Brian Modra */public abstract class Calendar { /** * Field number for <code>get</code> and <code>set</code> indicating the * year. This is a calendar-specific value. */ public final static int YEAR = 1; /** * Field number for <code>get</code> and <code>set</code> indicating the * month. This is a calendar-specific value. */ public final static int MONTH = 2; /** * Field number for <code>get</code> and <code>set</code> indicating the * day of the month. This is a synonym for <code>DAY_OF_MONTH</code>. * @see #DAY_OF_MONTH */ public final static int DATE = 5; /** * Field number for <code>get</code> and <code>set</code> indicating the * day of the month. This is a synonym for <code>DATE</code>. * @see #DATE */ public final static int DAY_OF_MONTH = 5; /** * Field number for <code>get</code> and <code>set</code> indicating the * day of the week. */ public final static int DAY_OF_WEEK = 7; /** * Field number for <code>get</code> and <code>set</code> indicating * whether the <code>HOUR</code> is before or after noon. * E.g., at 10:04:15.250 PM the <code>AM_PM</code> is <code>PM</code>. * @see #AM * @see #PM * @see #HOUR */ public final static int AM_PM = 9; /** * Field number for <code>get</code> and <code>set</code> indicating the * hour of the morning or afternoon. <code>HOUR</code> is used for the * 12-hour clock. * E.g., at 10:04:15.250 PM the <code>HOUR</code> is 10. * @see #AM_PM * @see #HOUR_OF_DAY */ public final static int HOUR = 10; /** * Field number for <code>get</code> and <code>set</code> indicating the * hour of the day. <code>HOUR_OF_DAY</code> is used for the 24-hour clock. * E.g., at 10:04:15.250 PM the <code>HOUR_OF_DAY</code> is 22. */ public final static int HOUR_OF_DAY = 11; /** * Field number for <code>get</code> and <code>set</code> indicating the * minute within the hour. * E.g., at 10:04:15.250 PM the <code>MINUTE</code> is 4. */ public final static int MINUTE = 12; /** * Field number for <code>get</code> and <code>set</code> indicating the * second within the minute. * E.g., at 10:04:15.250 PM the <code>SECOND</code> is 15. */ public final static int SECOND = 13; /** * Field number for <code>get</code> and <code>set</code> indicating the * millisecond within the second. * E.g., at 10:04:15.250 PM the <code>MILLISECOND</code> is 250. */ public final static int MILLISECOND = 14; /** * Value of the <code>DAY_OF_WEEK</code> field indicating * Sunday. */ public final static int SUNDAY = 1; /** * Value of the <code>DAY_OF_WEEK</code> field indicating * Monday. */ public final static int MONDAY = 2; /** * Value of the <code>DAY_OF_WEEK</code> field indicating * Tuesday. */ public final static int TUESDAY = 3; /** * Value of the <code>DAY_OF_WEEK</code> field indicating * Wednesday. */ public final static int WEDNESDAY = 4; /** * Value of the <code>DAY_OF_WEEK</code> field indicating * Thursday. */ public final static int THURSDAY = 5; /** * Value of the <code>DAY_OF_WEEK</code> field indicating * Friday. */ public final static int FRIDAY = 6; /** * Value of the <code>DAY_OF_WEEK</code> field indicating * Saturday. */ public final static int SATURDAY = 7; /** * Value of the <code>MONTH</code> field indicating the * first month of the year. */ public final static int JANUARY = 0; /** * Value of the <code>MONTH</code> field indicating the * second month of the year. */ public final static int FEBRUARY = 1; /** * Value of the <code>MONTH</code> field indicating the * third month of the year. */ public final static int MARCH = 2; /** * Value of the <code>MONTH</code> field indicating the * fourth month of the year. */ public final static int APRIL = 3; /** * Value of the <code>MONTH</code> field indicating the * fifth month of the year. */ public final static int MAY = 4; /** * Value of the <code>MONTH</code> field indicating the * sixth month of the year. */ public final static int JUNE = 5; /** * Value of the <code>MONTH</code> field indicating the * seventh month of the year. */ public final static int JULY = 6; /** * Value of the <code>MONTH</code> field indicating the * eighth month of the year. */ public final static int AUGUST = 7; /** * Value of the <code>MONTH</code> field indicating the * ninth month of the year. */ public final static int SEPTEMBER = 8; /** * Value of the <code>MONTH</code> field indicating the * tenth month of the year. */ public final static int OCTOBER = 9; /** * Value of the <code>MONTH</code> field indicating the * eleventh month of the year. */ public final static int NOVEMBER = 10; /** * Value of the <code>MONTH</code> field indicating the * twelfth month of the year. */ public final static int DECEMBER = 11; /** * Value of the <code>AM_PM</code> field indicating the * period of the day from midnight to just before noon. */ public final static int AM = 0; /** * Value of the <code>AM_PM</code> field indicating the * period of the day from noon to just before midnight. */ public final static int PM = 1; // Internal notes: // Calendar contains two kinds of time representations: current "time" in // milliseconds, and a set of time "fields" representing the current time. // The two representations are usually in sync, but can get out of sync // as follows. // 1. Initially, no fields are set, and the time is invalid. // 2. If the time is set, all fields are computed and in sync. // 3. If a single field is set, the time is invalid. // Recomputation of the time and fields happens when the object needs // to return a result to the user, or use a result for a computation. private int packed_time = 0; private int packed_date = 0; private int day_field = 0; private int dstOffset = 0; private boolean dstSet = false; /** * The currently set time for this calendar, expressed in milliseconds after * January 1, 1970, 0:00:00 GMT. */ private long time; /** * True if then the value of <code>time</code> is valid. * The time is made invalid by a change to an item of <code>field[]</code>. * @see #time */ private boolean millisSet; // NOTE: Make transient when possible /** * The <code>TimeZone</code> used by this calendar. </code>Calendar</code> * uses the time zone data to translate between locale and GMT time. */ private TimeZone zone; ////////////////// // Class Variables ////////////////// private static final int JAN_1_1_JULIAN_DAY = 1721426; // January 1, year 1 (Gregorian) private static final int EPOCH_JULIAN_DAY = 2440588; // Jaunary 1, 1970 (Gregorian) private static final int EPOCH_YEAR = 1970; private static final int NUM_DAYS[] = {0,31,59,90,120,151,181,212,243,273,304,334}; // 0-based, for day-in-year private static final int LEAP_NUM_DAYS[] = {0,31,60,91,121,152,182,213,244,274,305,335}; // 0-based, for day-in-year // Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit // into ints, they must be longs in order to prevent arithmetic overflow // when performing (bug 4173516). private static final int ONE_SECOND = 1000; private static final int ONE_MINUTE = 60*ONE_SECOND; private static final int ONE_HOUR = 60*ONE_MINUTE; private static final long ONE_DAY = 24*ONE_HOUR; private static final long ONE_WEEK = 7*ONE_DAY; ///////////////////// // Instance Variables ///////////////////// /** * The point at which the Gregorian calendar rules are used, measured in * milliseconds from the standard epoch. Default is October 15, 1582 * (Gregorian) 00:00:00 UTC or -12219292800000L. For this value, October 4, * 1582 (Julian) is followed by October 15, 1582 (Gregorian). This * corresponds to Julian day number 2299161. */ private static final long gregorianCutover = -12219292800000L; /** * The year of the gregorianCutover, with 0 representing * 1 BC, -1 representing 2 BC, etc. */ private static final int gregorianCutoverYear = 1582; private Date date = null; /** if both of these are set, the set() method will recalculate * the HOUR_OF_DAY using 12hr time. */ private int hour_12hr = -1; private int am_pm_12hr = -1; /** * The platform name */ private static String platform = null; /** * The root of the classes */ private static String classRoot = null; /** * Constructs a Calendar with the default time zone * and default locale. * * @see TimeZone#getDefault */ protected Calendar() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -