⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gregoriancalendar.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* java.util.GregorianCalendar   Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004   Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.util;/** * <p> * This class represents the Gregorian calendar, that is used in most * countries all over the world.  It does also handle the Julian calendar * for dates smaller than the date of the change to the Gregorian calendar. * The Gregorian calendar differs from the Julian calendar by a different * leap year rule (no leap year every 100 years, except if year is divisible * by 400). * </p> * <p> * This change date is different from country to country, and can be changed with * <code>setGregorianChange</code>.  The first countries to adopt the Gregorian * calendar did so on the 15th of October, 1582.  This date followed October * the 4th, 1582 in the Julian calendar system.  The non-existant days that were * omitted when the change took place are interpreted as Gregorian dates. * </p> * <p> * Prior to the changeover date, New Year's Day occurred on the 25th of March. * However, this class always takes New Year's Day as being the 1st of January. * Client code should manually adapt the year value, if required, for dates * between January the 1st and March the 24th in years prior to the changeover. * </p> * <p> * Any date infinitely forwards or backwards in time can be represented by * this class.  A <em>proleptic</em> calendar system is used, which allows * future dates to be created via the existing rules.  This allows meaningful * and consistent dates to be produced for all years.  However, dates are only * historically accurate following March the 1st, 4AD when the Julian calendar * system was adopted.  Prior to this, leap year rules were applied erraticly. * </p> * <p> * There are two eras available for the Gregorian calendar, namely BC and AD. * </p> * <p> * Weeks are defined as a period of seven days, beginning on the first day * of the week, as returned by <code>getFirstDayOfWeek()</code>, and ending * on the day prior to this. * </p> * <p> * The weeks of the year are numbered from 1 to a possible 53.  The first week * of the year is defined as the first week that contains at least the minimum * number of days of the first week in the new year (retrieved via * <code>getMinimalDaysInFirstWeek()</code>).  All weeks after this are numbered * from 2 onwards. * </p> * <p> * For example, take the year 2004.  It began on a Thursday.  The first week * of 2004 depends both on where a week begins and how long it must minimally * last.  Let's say that the week begins on a Monday and must have a minimum * of 5 days.  In this case, the first week begins on Monday, the 5th of January. * The first 4 days (Thursday to Sunday) are not eligible, as they are too few * to make up the minimum number of days of the first week which must be in * the new year.  If the minimum was lowered to 4 days, then the first week * would instead begin on Monday, the 29th of December, 2003.  This first week * has 4 of its days in the new year, and is now eligible. * </p> * <p> * The weeks of the month are numbered from 0 to a possible 6.  The first week * of the month (numbered 1) is a set of days, prior to the first day of the week, * which number at least the minimum number of days in a week.  Unlike the first * week of the year, the first week of the month only uses days from that particular * month.  As a consequence, it may have a variable number of days (from the minimum * number required up to a full week of 7) and it need not start on the first day of * the week.  It must, however, be following by the first day of the week, as this * marks the beginning of week 2.  Any days of the month which occur prior to the * first week (because the first day of the week occurs before the minimum number * of days is met) are seen as week 0. * </p> * <p> * Again, we will take the example of the year 2004 to demonstrate this.  September * 2004 begins on a Wednesday.  Taking our first day of the week as Monday, and the * minimum length of the first week as 6, we find that week 1 runs from Monday, * the 6th of September to Sunday the 12th.  Prior to the 6th, there are only * 5 days (Wednesday through to Sunday).  This is too small a number to meet the * minimum, so these are classed as being days in week 0.  Week 2 begins on the * 13th, and so on.  This changes if we reduce the minimum to 5.  In this case, * week 1 is a truncated week from Wednesday the 1st to Sunday the 5th, and week * 0 doesn't exist.  The first seven day week is week 2, starting on the 6th. * </p> * <p> * On using the <code>clear()</code> method, the Gregorian calendar returns * to its default value of the 1st of January, 1970 AD 00:00:00 (the epoch). * The day of the week is set to the correct day for that particular time. * The day is also the first of the month, and the date is in week 0. * </p> * * @see Calendar * @see TimeZone * @see Calendar#getFirstDayOfWeek() * @see Calendar#getMinimalDaysInFirstWeek() */public class GregorianCalendar extends Calendar{  /**   * Constant representing the era BC (Before Christ).   */  public static final int BC = 0;  /**   * Constant representing the era AD (Anno Domini).   */  public static final int AD = 1;  /**   * The point at which the Gregorian calendar rules were used.   * This is locale dependent; the default for most catholic   * countries is midnight (UTC) on October 5, 1582 (Julian),   * or October 15, 1582 (Gregorian).   *   * @serial the changeover point from the Julian calendar   *         system to the Gregorian.   */  private long gregorianCutover;  /**   * For compatability with Sun's JDK.   */  static final long serialVersionUID = -8125100834729963327L;  /**   * The name of the resource bundle. Used only by getBundle()   */  private static final String bundleName = "gnu.java.locale.Calendar";  /**   * Days in the epoch. Relative Jan 1, year '0' which is not a leap year.   * (although there is no year zero, this does not matter.)   * This is consistent with the formula:   * = (year-1)*365L + ((year-1) >> 2)   *   * Plus the gregorian correction:   *  Math.floor((year-1) / 400.) - Math.floor((year-1) / 100.);   * For a correct julian date, the correction is -2 instead.   *   * The gregorian cutover in 1582 was 10 days, so by calculating the   * correction from year zero, we have 15 non-leap days (even centuries)   * minus 3 leap days (year 400,800,1200) = 12. Subtracting two corrects   * this to the correct number 10.   */  private static final int EPOCH_DAYS = 719162;  /**   * Constructs a new GregorianCalender representing the current   * time, using the default time zone and the default locale.   */  public GregorianCalendar()  {    this(TimeZone.getDefault(), Locale.getDefault());  }  /**   * Constructs a new GregorianCalender representing the current   * time, using the specified time zone and the default locale.   *   * @param zone a time zone.   */  public GregorianCalendar(TimeZone zone)  {    this(zone, Locale.getDefault());  }  /**   * Constructs a new GregorianCalender representing the current   * time, using the default time zone and the specified locale.   *   * @param locale a locale.   */  public GregorianCalendar(Locale locale)  {    this(TimeZone.getDefault(), locale);  }  /**   * Constructs a new GregorianCalender representing the current   * time with the given time zone and the given locale.   *   * @param zone a time zone.   * @param locale a locale.   */  public GregorianCalendar(TimeZone zone, Locale locale)  {    this(zone, locale, false);    setTimeInMillis(System.currentTimeMillis());    complete();  }  /**   * Common constructor that all constructors should call.   * @param zone a time zone.   * @param locale a locale.   * @param unused unused parameter to make the signature differ from   * the public constructor (TimeZone, Locale).   */  private GregorianCalendar(TimeZone zone, Locale locale, boolean unused)  {    super(zone, locale);    ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale,                                                 ClassLoader                                                 .getSystemClassLoader());    gregorianCutover = ((Date) rb.getObject("gregorianCutOver")).getTime();  }  /**   * Constructs a new GregorianCalendar representing midnight on the   * given date with the default time zone and locale.   * @param year corresponds to the YEAR time field.   * @param month corresponds to the MONTH time field.   * @param day corresponds to the DAY time field.   */  public GregorianCalendar(int year, int month, int day)  {    this(TimeZone.getDefault(), Locale.getDefault(), false);    set(year, month, day);  }  /**   * Constructs a new GregorianCalendar representing midnight on the   * given date with the default time zone and locale.   *   * @param year corresponds to the YEAR time field.   * @param month corresponds to the MONTH time field.   * @param day corresponds to the DAY time field.   * @param hour corresponds to the HOUR_OF_DAY time field.   * @param minute corresponds to the MINUTE time field.   */  public GregorianCalendar(int year, int month, int day, int hour, int minute)  {    this(TimeZone.getDefault(), Locale.getDefault(), false);    set(year, month, day, hour, minute);  }  /**   * Constructs a new GregorianCalendar representing midnight on the   * given date with the default time zone and locale.   *   * @param year corresponds to the YEAR time field.   * @param month corresponds to the MONTH time field.   * @param day corresponds to the DAY time field.   * @param hour corresponds to the HOUR_OF_DAY time field.   * @param minute corresponds to the MINUTE time field.   * @param second corresponds to the SECOND time field.   */  public GregorianCalendar(int year, int month, int day, int hour, int minute,                           int second)  {    this(TimeZone.getDefault(), Locale.getDefault(), false);    set(year, month, day, hour, minute, second);  }  /**   * Sets the date of the switch from Julian dates to Gregorian dates.   * You can use <code>new Date(Long.MAX_VALUE)</code> to use a pure   * Julian calendar, or <code>Long.MIN_VALUE</code> for a pure Gregorian   * calendar.   *   * @param date the date of the change.   */  public void setGregorianChange(Date date)  {    gregorianCutover = date.getTime();  }  /**   * Gets the date of the switch from Julian dates to Gregorian dates.   *   * @return the date of the change.   */  public final Date getGregorianChange()  {    return new Date(gregorianCutover);  }  /**   * <p>   * Determines if the given year is a leap year.  The result is   * undefined if the Gregorian change took place in 1800, so that   * the end of February is skipped, and that year is specified.   * (well...).   * </p>   * <p>   * To specify a year in the BC era, use a negative value calculated   * as 1 - y, where y is the required year in BC.  So, 1 BC is 0,   * 2 BC is -1, 3 BC is -2, etc.   * </p>   *   * @param year a year (use a negative value for BC).   * @return true, if the given year is a leap year, false otherwise.   */  public boolean isLeapYear(int year)  {    // Only years divisible by 4 can be leap years    if ((year & 3) != 0)      return false;    // Is the leap-day a Julian date? Then it's a leap year    if (! isGregorian(year, 31 + 29 - 1))      return true;    // Apply gregorian rules otherwise    return ((year % 100) != 0 || (year % 400) == 0);  }  /**   * Retrieves the day of the week corresponding to the specified   * day of the specified year.   *   * @param year the year in which the dayOfYear occurs.   * @param dayOfYear the day of the year (an integer between 0 and   *        and 366)   */  private int getWeekDay(int year, int dayOfYear)  {    boolean greg = isGregorian(year, dayOfYear);    int day = (int) getLinearDay(year, dayOfYear, greg);    // The epoch was a thursday.    int weekday = (day + THURSDAY) % 7;    if (weekday <= 0)      weekday += 7;    return weekday;  }  /**   * Returns the day of the week for the first day of a given month (0..11)   */  private int getFirstDayOfMonth(int year, int month)  {    int[] dayCount = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };    if (month > 11)      {	year += (month / 12);	month = month % 12;      }    if (month < 0)      {	year += (int) month / 12;	month = month % 12;	if (month < 0)	  {	    month += 12;	    year--;	  }      }    int dayOfYear = dayCount[month] + 1;    if (month > 1)      if (isLeapYear(year))	dayOfYear++;    boolean greg = isGregorian(year, dayOfYear);    int day = (int) getLinearDay(year, dayOfYear, greg);    // The epoch was a thursday.    int weekday = (day + THURSDAY) % 7;    if (weekday <= 0)      weekday += 7;    return weekday;  }  /**   * Takes a year, and a (zero based) day of year and determines   * if it is gregorian or not.   */  private boolean isGregorian(int year, int dayOfYear)  {    int relativeDay = (year - 1) * 365 + ((year - 1) >> 2) + dayOfYear                      - EPOCH_DAYS; // gregorian days from 1 to epoch.    int gregFactor = (int) Math.floor((double) (year - 1) / 400.)                     - (int) Math.floor((double) (year - 1) / 100.);    return ((relativeDay + gregFactor) * 60L * 60L * 24L * 1000L >= gregorianCutover);  }  /**   * Check set fields for validity, without leniency.   *   * @throws IllegalArgumentException if a field is invalid   */  private void nonLeniencyCheck() throws IllegalArgumentException  {    int[] month_days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };    int year = fields[YEAR];    int month = fields[MONTH];    int leap = isLeapYear(year) ? 1 : 0;    if (isSet[ERA] && fields[ERA] != AD && fields[ERA] != BC)      throw new IllegalArgumentException("Illegal ERA.");    if (isSet[YEAR] && fields[YEAR] < 1)      throw new IllegalArgumentException("Illegal YEAR.");    if (isSet[MONTH] && (month < 0 || month > 11))      throw new IllegalArgumentException("Illegal MONTH.");    if (isSet[WEEK_OF_YEAR])      {	int daysInYear = 365 + leap;	daysInYear += (getFirstDayOfMonth(year, 0) - 1); // pad first week	int last = getFirstDayOfMonth(year, 11) + 4;	if (last > 7)	  last -= 7;	daysInYear += 7 - last;	int weeks = daysInYear / 7;	if (fields[WEEK_OF_YEAR] < 1 || fields[WEEK_OF_YEAR] > weeks)	  throw new IllegalArgumentException("Illegal WEEK_OF_YEAR.");      }    if (isSet[WEEK_OF_MONTH])      {	int weeks = (month == 1 && leap == 0) ? 4 : 5;	if (fields[WEEK_OF_MONTH] < 1 || fields[WEEK_OF_MONTH] > weeks)	  throw new IllegalArgumentException("Illegal WEEK_OF_MONTH.");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -