?? gregoriancalendar.java
字號(hào):
/* * @(#)GregorianCalendar.java 1.73 03/04/27 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* * (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 sun.util.calendar.ZoneInfo;/** * <code>GregorianCalendar</code> is a concrete subclass of * {@link Calendar} * and provides the standard calendar used by most of the world. * * <p> * The standard (Gregorian) calendar has 2 eras, BC and AD. * * <p> * This implementation handles a single discontinuity, which corresponds by * default to the date the Gregorian calendar was instituted (October 15, 1582 * in some countries, later in others). The cutover date may be changed by the * caller by calling <code>setGregorianChange()</code>. * * <p> * Historically, in those countries which adopted the Gregorian calendar first, * October 4, 1582 was thus followed by October 15, 1582. This calendar models * implements the Julian calendar. The only difference between the Gregorian * and the Julian calendar is the leap year rule. The Julian calendar specifies * leap years every four years, whereas the Gregorian calendar omits century * years which are not divisible by 400. * * <p> * <code>GregorianCalendar</code> implements <em>proleptic</em> Gregorian and * Julian calendars. That is, dates are computed by extrapolating the current * rules indefinitely far backward and forward in time. As a result, * <code>GregorianCalendar</code> may be used for all years to generate * meaningful and consistent results. However, dates obtained using * <code>GregorianCalendar</code> are historically accurate only from March 1, 4 * AD onward, when modern Julian calendar rules were adopted. Before this date, * leap year rules were applied irregularly, and before 45 BC the Julian * calendar did not even exist. * * <p> * Prior to the institution of the Gregorian calendar, New Year's Day was * March 25. To avoid confusion, this calendar always uses January 1. A manual * adjustment may be made if desired for dates that are prior to the Gregorian * changeover and which fall between January 1 and March 24. * * <p>Values calculated for the <code>WEEK_OF_YEAR</code> field range from 1 to * 53. Week 1 for a year is the earliest seven day period starting on * <code>getFirstDayOfWeek()</code> that contains at least * <code>getMinimalDaysInFirstWeek()</code> days from that year. It thus * depends on the values of <code>getMinimalDaysInFirstWeek()</code>, * <code>getFirstDayOfWeek()</code>, and the day of the week of January 1. * Weeks between week 1 of one year and week 1 of the following year are * numbered sequentially from 2 to 52 or 53 (as needed). * <p>For example, January 1, 1998 was a Thursday. If * <code>getFirstDayOfWeek()</code> is <code>MONDAY</code> and * <code>getMinimalDaysInFirstWeek()</code> is 4 (these are the values * reflecting ISO 8601 and many national standards), then week 1 of 1998 starts * on December 29, 1997, and ends on January 4, 1998. If, however, * <code>getFirstDayOfWeek()</code> is <code>SUNDAY</code>, then week 1 of 1998 * starts on January 4, 1998, and ends on January 10, 1998; the first three days * of 1998 then are part of week 53 of 1997. * * <p>Values calculated for the <code>WEEK_OF_MONTH</code> field range from 0 * to 6. Week 1 of a month (the days with <code>WEEK_OF_MONTH = * 1</code>) is the earliest set of at least * <code>getMinimalDaysInFirstWeek()</code> contiguous days in that month, * ending on the day before <code>getFirstDayOfWeek()</code>. Unlike * week 1 of a year, week 1 of a month may be shorter than 7 days, need * not start on <code>getFirstDayOfWeek()</code>, and will not include days of * the previous month. Days of a month before week 1 have a * <code>WEEK_OF_MONTH</code> of 0. * * <p>For example, if <code>getFirstDayOfWeek()</code> is <code>SUNDAY</code> * and <code>getMinimalDaysInFirstWeek()</code> is 4, then the first week of * January 1998 is Sunday, January 4 through Saturday, January 10. These days * have a <code>WEEK_OF_MONTH</code> of 1. Thursday, January 1 through * Saturday, January 3 have a <code>WEEK_OF_MONTH</code> of 0. If * <code>getMinimalDaysInFirstWeek()</code> is changed to 3, then January 1 * through January 3 have a <code>WEEK_OF_MONTH</code> of 1. * * <p> * <strong>Example:</strong> * <blockquote> * <pre> * // get the supported ids for GMT-08:00 (Pacific Standard Time) * String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); * // if no ids were returned, something is wrong. get out. * if (ids.length == 0) * System.exit(0); * * // begin output * System.out.println("Current Time"); * * // create a Pacific Standard Time time zone * SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]); * * // set up rules for daylight savings time * pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); * pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); * * // create a GregorianCalendar with the Pacific Daylight time zone * // and the current date and time * Calendar calendar = new GregorianCalendar(pdt); * Date trialTime = new Date(); * calendar.setTime(trialTime); * * // print out a bunch of interesting things * System.out.println("ERA: " + calendar.get(Calendar.ERA)); * System.out.println("YEAR: " + calendar.get(Calendar.YEAR)); * System.out.println("MONTH: " + calendar.get(Calendar.MONTH)); * System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR)); * System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH)); * System.out.println("DATE: " + calendar.get(Calendar.DATE)); * System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH)); * System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR)); * System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK)); * System.out.println("DAY_OF_WEEK_IN_MONTH: " * + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); * System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM)); * System.out.println("HOUR: " + calendar.get(Calendar.HOUR)); * System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY)); * System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE)); * System.out.println("SECOND: " + calendar.get(Calendar.SECOND)); * System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND)); * System.out.println("ZONE_OFFSET: " * + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); * System.out.println("DST_OFFSET: " * + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); * System.out.println("Current Time, with hour reset to 3"); * calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override * calendar.set(Calendar.HOUR, 3); * System.out.println("ERA: " + calendar.get(Calendar.ERA)); * System.out.println("YEAR: " + calendar.get(Calendar.YEAR)); * System.out.println("MONTH: " + calendar.get(Calendar.MONTH)); * System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR)); * System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH)); * System.out.println("DATE: " + calendar.get(Calendar.DATE)); * System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH)); * System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR)); * System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK)); * System.out.println("DAY_OF_WEEK_IN_MONTH: " * + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); * System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM)); * System.out.println("HOUR: " + calendar.get(Calendar.HOUR)); * System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY)); * System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE)); * System.out.println("SECOND: " + calendar.get(Calendar.SECOND)); * System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND)); * System.out.println("ZONE_OFFSET: " * + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours * System.out.println("DST_OFFSET: " * + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours * </pre> * </blockquote> * * @see Calendar * @see TimeZone * @version 1.73 * @author David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu * @since JDK1.1 */public class GregorianCalendar extends Calendar { /* * Implementation Notes * * The Julian day number, as used here, is a modified number which has its * onset at midnight, rather than noon. * * The epoch is the number of days or milliseconds from some defined * starting point. The epoch for java.util.Date is used here; that is, * milliseconds from January 1, 1970 (Gregorian), midnight UTC. Other * epochs which are used are January 1, year 1 (Gregorian), which is day 1 * of the Gregorian calendar, and December 30, year 0 (Gregorian), which is * day 1 of the Julian calendar. * * We implement the proleptic Julian and Gregorian calendars. This means we * implement the modern definition of the calendar even though the * historical usage differs. For example, if the Gregorian change is set * to new Date(Long.MIN_VALUE), we have a pure Gregorian calendar which * labels dates preceding the invention of the Gregorian calendar in 1582 as * if the calendar existed then. * * Likewise, with the Julian calendar, we assume a consistent 4-year leap * rule, even though the historical pattern of leap years is irregular, * being every 3 years from 45 BC through 9 BC, then every 4 years from 8 AD * onwards, with no leap years in-between. Thus date computations and * functions such as isLeapYear() are not intended to be historically * accurate. * * Given that milliseconds are a long, day numbers such as Julian day * numbers, Gregorian or Julian calendar days, or epoch days, are also * longs. Years can fit into an int. *///////////////////// Class Variables////////////////// /** * Value of the <code>ERA</code> field indicating * the period before the common era (before Christ), also known as BCE. * The sequence of years at the transition from <code>BC</code> to <code>AD</code> is * ..., 2 BC, 1 BC, 1 AD, 2 AD,... * @see Calendar#ERA */ public static final int BC = 0; /** * Value of the <code>ERA</code> field indicating * the common era (Anno Domini), also known as CE. * The sequence of years at the transition from <code>BC</code> to <code>AD</code> is * ..., 2 BC, 1 BC, 1 AD, 2 AD,... * @see Calendar#ERA */ public static final int AD = 1; private static final int JAN_1_1_JULIAN_DAY = 1721426; // January 1, year 1 (Gregorian) private static final int EPOCH_JULIAN_DAY = 2440588; // January 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 private static final int MONTH_LENGTH[] = {31,28,31,30,31,30,31,31,30,31,30,31}; // 0-based private static final int LEAP_MONTH_LENGTH[] = {31,29,31,30,31,30,31,31,30,31,30,31}; // 0-based // 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; /* * <pre> * Greatest Least * Field name Minimum Minimum Maximum Maximum * ---------- ------- ------- ------- ------- * ERA 0 0 1 1 * YEAR 1 1 292269054 292278994 * MONTH 0 0 11 11 * WEEK_OF_YEAR 1 1 52 53 * WEEK_OF_MONTH 0 0 4 6 * DAY_OF_MONTH 1 1 28 31 * DAY_OF_YEAR 1 1 365 366 * DAY_OF_WEEK 1 1 7 7 * DAY_OF_WEEK_IN_MONTH -1 -1 4 6 * AM_PM 0 0 1 1 * HOUR 0 0 11 11 * HOUR_OF_DAY 0 0 23 23 * MINUTE 0 0 59 59 * SECOND 0 0 59 59 * MILLISECOND 0 0 999 999 * ZONE_OFFSET -12* -12* 12* 12* * DST_OFFSET 0 0 1* 1* * </pre> * (*) In units of one-hour */ private static final int MIN_VALUES[] = { 0,1,0,1,0,1,1,1,-1,0,0,0,0,0,0,-12*ONE_HOUR,0 }; private static final int LEAST_MAX_VALUES[] = { 1,292269054,11,52,4,28,365,7,4,1,11,23,59,59,999,12*ONE_HOUR,1*ONE_HOUR }; private static final int MAX_VALUES[] = { 1,292278994,11,53,6,31,366,7,6,1,11,23,59,59,999,12*ONE_HOUR,1*ONE_HOUR };/////////////////////// 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. * @serial */ private long gregorianCutover = -12219292800000L; /** * Midnight, local time (using this Calendar's TimeZone) at or before the
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -