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

📄 gregoriancalendar.java

📁 this gcc-g++-3.3.1.tar.gz is a source file of gcc, you can learn more about gcc through this codes f
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* java.util.GregorianCalendar   Copyright (C) 1998, 1999, 2001, 2002 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., 59 Temple Place, Suite 330, Boston, MA02111-1307 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;/** * 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. * This change date is different from country to country, you can set it with * <code>setGregorianChange</code> * * 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).  The non existing days that were omited when the change took * place are interpreted as gregorian date * * There are to eras available for the Gregorian calendar, namely BC and AD. * * @see Calendar * @see TimeZone */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).   */  private long gregorianCutover;  static final long serialVersionUID = -8125100834729963327L;  /**   * The name of the resource bundle.   */  private static final String bundleName = "gnu.java.locale.Calendar";  /**   * 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)  {    super(zone, locale);    ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);    gregorianCutover = ((Date) rb.getObject("gregorianCutOver")).getTime();    setTimeInMillis(System.currentTimeMillis());  }  /**   * 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)  {    super();    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)  {    super();    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)  {    super();    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);  }  /**   * 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 skiped and you give that year   * (well...).<br>   *   * The year should be positive and you can't give an ERA.  But   * remember that before 4 BC there wasn't a consistent leap year   * rule, so who cares.   *   * @param year a year use nonnegative value for BC.   * @return true, if the given year is a leap year, false otherwise.  */  public boolean isLeapYear(int year)  {    if ((year & 3) != 0)      // Only years divisible by 4 can be leap years      return false;    // compute the linear day of the 29. February of that year.    // The 13 is the number of days, that were omitted in the Gregorian    // Calender until the epoch.    int julianDay = (((year-1) * (365*4+1)) >> 2) + (31+29 -         (((1970-1) * (365*4+1)) / 4 + 1 - 13));        // If that day is smaller than the gregorianChange the julian    // rule applies:  This is a leap year since it is divisible by 4.    if (julianDay * (24 * 60 * 60 * 1000L) < gregorianCutover)      return true;    return ((year % 100) != 0 || (year % 400) == 0);  }  /**   * Get the linear time in milliseconds since the epoch.  If you   * specify a nonpositive year it is interpreted as BC as   * following: 0 is 1 BC, -1 is 2 BC and so on.  The date is   * interpreted as gregorian if the change occurred before that date.   *   * @param year the year of the date.   * @param dayOfYear the day of year of the date; 1 based.   * @param millis the millisecond in that day.   * @return the days since the epoch, may be negative.  */  private long getLinearTime(int year, int dayOfYear, int millis)  {    // The 13 is the number of days, that were omitted in the Gregorian    // Calender until the epoch.    // We shift right by 2 instead of dividing by 4, to get correct    // results for negative years (and this is even more efficient).    int julianDay = ((year * (365 * 4 + 1)) >> 2) + dayOfYear -      ((1970 * (365 * 4 + 1)) / 4 + 1 - 13);    long time = julianDay * (24 * 60 * 60 * 1000L) + millis;    if (time >= gregorianCutover)      {	// subtract the days that are missing in gregorian calendar	// with respect to julian calendar.	//	// Okay, here we rely on the fact that the gregorian	// calendar was introduced in the AD era.  This doesn't work	// with negative years.	//	// The additional leap year factor accounts for the fact that	// a leap day is not seen on Jan 1 of the leap year.	int gregOffset = (year / 400) - (year / 100) + 2;	if (isLeapYear (year, true) && dayOfYear < 31 + 29)	  --gregOffset;	time += gregOffset * (24 * 60 * 60 * 1000L);      }    return time;  }  private int getWeekDay(int year, int dayOfYear)  {    int day =      (int) (getLinearTime(year, dayOfYear, 0) / (24 * 60 * 60 * 1000L));    // The epoch was a thursday.    int weekday = (day + THURSDAY) % 7;    if (weekday <= 0)      weekday += 7;    return weekday;  }  /**   * Calculate the dayOfYear from the fields array.     * The relativeDays is used, to account for weeks that begin before   * the gregorian change and end after it.<br>   *   * We return two values, the first is used to determine, if we   * should use Gregorian calendar or Julian calendar, in case of   * the change year, the second is a relative day after the given   * day.  This is necessary for week calculation in the year in   * which gregorian change occurs. <br>   *   * @param year the year, negative for BC.   * @return an array of two int values, the first containing a reference   * day of current year, the second a relative count since this reference   * day.  */  private int[] getDayOfYear(int year)  {    if (isSet[MONTH])      {	int dayOfYear;	if (fields[MONTH] > FEBRUARY)	  {	    // The months after February are regular:	    // 9 is an offset found by try and error.	    dayOfYear = (fields[MONTH] * (31 + 30 + 31 + 30 + 31) - 9) / 5;	    if (isLeapYear(year))	      dayOfYear++;	  }	else	    dayOfYear = 31 * fields[MONTH];	if (isSet[DAY_OF_MONTH])	  {	    return new int[]	    {	    dayOfYear + fields[DAY_OF_MONTH], 0};	  }	if (isSet[WEEK_OF_MONTH] && isSet[DAY_OF_WEEK])	  {	    // the weekday of the first day in that month is:	    int weekday = getWeekDay(year, ++dayOfYear);	    return new int[]	    {	      dayOfYear,		// the day of week in the first week		// (weeks starting on sunday) is:	      fields[DAY_OF_WEEK] - weekday +		// Now jump to the right week and correct the possible		// error made by assuming sunday is the first week day.	      7 * (fields[WEEK_OF_MONTH]		   + (fields[DAY_OF_WEEK] < getFirstDayOfWeek()? 0 : -1)		   + (weekday < getFirstDayOfWeek()? -1 : 0))};	  }	if (isSet[DAY_OF_WEEK] && isSet[DAY_OF_WEEK_IN_MONTH])	  {	    // the weekday of the first day in that month is:	    int weekday = getWeekDay(year, ++dayOfYear);	    return new int[] { 		  dayOfYear,		  fields[DAY_OF_WEEK] - weekday +		  7 * (fields[DAY_OF_WEEK_IN_MONTH]		       + (fields[DAY_OF_WEEK] < weekday ? 0 : -1))};	  }      }    // MONTH + something did not succeed.    if (isSet[DAY_OF_YEAR])      {	return new int[] {0, fields[DAY_OF_YEAR]};      }          if (isSet[DAY_OF_WEEK] && isSet[WEEK_OF_YEAR])      {	int dayOfYear = getMinimalDaysInFirstWeek();	// the weekday of the day, that begins the first week 	// in that year is:	int weekday = getWeekDay(year, dayOfYear);	return new int[] { 	    dayOfYear,	      // the day of week in the first week	      // (weeks starting on sunday) is:	    fields[DAY_OF_WEEK] - weekday	      // Now jump to the right week and correct the possible	      // error made by assuming sunday is the first week day.

⌨️ 快捷键说明

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