?? gregoriancalendar.java
字號:
* gregorianCutover. This is a pure date value with no time of day or * timezone component. */ private transient long normalizedGregorianCutover = gregorianCutover; /** * The year of the gregorianCutover, with 0 representing * 1 BC, -1 representing 2 BC, etc. */ private transient int gregorianCutoverYear = 1582; // Proclaim serialization compatibility with JDK 1.1 static final long serialVersionUID = -8125100834729963327L;///////////////// Constructors/////////////// /** * Constructs a default GregorianCalendar using the current time * in the default time zone with the default locale. */ public GregorianCalendar() { this(TimeZone.getDefault(), Locale.getDefault()); } /** * Constructs a GregorianCalendar based on the current time * in the given time zone with the default locale. * @param zone the given time zone. */ public GregorianCalendar(TimeZone zone) { this(zone, Locale.getDefault()); } /** * Constructs a GregorianCalendar based on the current time * in the default time zone with the given locale. * @param aLocale the given locale. */ public GregorianCalendar(Locale aLocale) { this(TimeZone.getDefault(), aLocale); } /** * Constructs a GregorianCalendar based on the current time * in the given time zone with the given locale. * @param zone the given time zone. * @param aLocale the given locale. */ public GregorianCalendar(TimeZone zone, Locale aLocale) { super(zone, aLocale); setTimeInMillis(System.currentTimeMillis()); } /** * Constructs a GregorianCalendar with the given date set * in the default time zone with the default locale. * @param year the value used to set the YEAR time field in the calendar. * @param month the value used to set the MONTH time field in the calendar. * Month value is 0-based. e.g., 0 for January. * @param date the value used to set the DATE time field in the calendar. */ public GregorianCalendar(int year, int month, int date) { super(TimeZone.getDefault(), Locale.getDefault()); this.set(YEAR, year); this.set(MONTH, month); this.set(DATE, date); } /** * Constructs a GregorianCalendar with the given date * and time set for the default time zone with the default locale. * @param year the value used to set the YEAR time field in the calendar. * @param month the value used to set the MONTH time field in the calendar. * Month value is 0-based. e.g., 0 for January. * @param date the value used to set the DATE time field in the calendar. * @param hour the value used to set the HOUR_OF_DAY time field * in the calendar. * @param minute the value used to set the MINUTE time field * in the calendar. */ public GregorianCalendar(int year, int month, int date, int hour, int minute) { super(TimeZone.getDefault(), Locale.getDefault()); this.set(YEAR, year); this.set(MONTH, month); this.set(DATE, date); this.set(HOUR_OF_DAY, hour); this.set(MINUTE, minute); } /** * Constructs a GregorianCalendar with the given date * and time set for the default time zone with the default locale. * @param year the value used to set the YEAR time field in the calendar. * @param month the value used to set the MONTH time field in the calendar. * Month value is 0-based. e.g., 0 for January. * @param date the value used to set the DATE time field in the calendar. * @param hour the value used to set the HOUR_OF_DAY time field * in the calendar. * @param minute the value used to set the MINUTE time field * in the calendar. * @param second the value used to set the SECOND time field * in the calendar. */ public GregorianCalendar(int year, int month, int date, int hour, int minute, int second) { super(TimeZone.getDefault(), Locale.getDefault()); this.set(YEAR, year); this.set(MONTH, month); this.set(DATE, date); this.set(HOUR_OF_DAY, hour); this.set(MINUTE, minute); this.set(SECOND, second); }/////////////////// Public methods///////////////// /** * Sets the GregorianCalendar change date. This is the point when the switch * from Julian dates to Gregorian dates occurred. Default is October 15, * 1582. Previous to this, dates will be in the Julian calendar. * <p> * To obtain a pure Julian calendar, set the change date to * <code>Date(Long.MAX_VALUE)</code>. To obtain a pure Gregorian calendar, * set the change date to <code>Date(Long.MIN_VALUE)</code>. * * @param date the given Gregorian cutover date. */ public void setGregorianChange(Date date) { gregorianCutover = date.getTime(); // Precompute two internal variables which we use to do the actual // cutover computations. These are the normalized cutover, which is the // midnight at or before the cutover, and the cutover year. The // normalized cutover is in pure date milliseconds; it contains no time // of day or timezone component, and it used to compare against other // pure date values. long cutoverDay = floorDivide(gregorianCutover, ONE_DAY); normalizedGregorianCutover = cutoverDay * ONE_DAY; // Handle the rare case of numeric overflow. If the user specifies a // change of Date(Long.MIN_VALUE), in order to get a pure Gregorian // calendar, then the epoch day is -106751991168, which when multiplied // by ONE_DAY gives 9223372036794351616 -- the negative value is too // large for 64 bits, and overflows into a positive value. We correct // this by using the next day, which for all intents is semantically // equivalent. if (cutoverDay < 0 && normalizedGregorianCutover > 0) { normalizedGregorianCutover = (cutoverDay + 1) * ONE_DAY; } // Normalize the year so BC values are represented as 0 and negative // values. GregorianCalendar cal = new GregorianCalendar(getTimeZone()); cal.setTime(date); gregorianCutoverYear = cal.get(YEAR); if (cal.get(ERA) == BC) { gregorianCutoverYear = 1 - gregorianCutoverYear; } } /** * Gets the Gregorian Calendar change date. This is the point when the * switch from Julian dates to Gregorian dates occurred. Default is * October 15, 1582. Previous to this, dates will be in the Julian * calendar. * @return the Gregorian cutover date for this calendar. */ public final Date getGregorianChange() { return new Date(gregorianCutover); } /** * Determines if the given year is a leap year. Returns true if the * given year is a leap year. * @param year the given year. * @return true if the given year is a leap year; false otherwise. */ public boolean isLeapYear(int year) { return year >= gregorianCutoverYear ? ((year%4 == 0) && ((year%100 != 0) || (year%400 == 0))) : // Gregorian (year%4 == 0); // Julian } /** * Compares this GregorianCalendar to an object reference. * @param obj the object reference with which to compare * @return true if this object is equal to <code>obj</code>; false otherwise */ public boolean equals(Object obj) { return super.equals(obj) && obj instanceof GregorianCalendar && gregorianCutover == ((GregorianCalendar)obj).gregorianCutover; } /** * Override hashCode. * Generates the hash code for the GregorianCalendar object */ public int hashCode() { return super.hashCode() ^ (int)gregorianCutover; } /** * Adds the specified (signed) amount of time to the given time field, * based on the calendar's rules. * <p><em>Add rule 1</em>. The value of <code>field</code> * after the call minus the value of <code>field</code> before the * call is <code>amount</code>, modulo any overflow that has occurred in * <code>field</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><em>Add rule 2</em>. 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 * <code>field</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> * @param field the time field. * @param amount the amount of date or time to be added to the field. * @exception IllegalArgumentException if an unknown field is given. */ public void add(int field, int amount) { if (amount == 0) { return; // Do nothing! } complete(); if (field == YEAR) { int year = this.internalGet(YEAR); if (this.internalGetEra() == AD) { year += amount; if (year > 0) { this.set(YEAR, year); } else { // year <= 0 this.set(YEAR, 1 - year); // if year == 0, you get 1 BC this.set(ERA, BC); } } else { // era == BC year -= amount; if (year > 0) { this.set(YEAR, year); } else { // year <= 0 this.set(YEAR, 1 - year); // if year == 0, you get 1 AD this.set(ERA, AD); } } pinDayOfMonth(); } else if (field == MONTH) { int month = this.internalGet(MONTH) + amount; int year = this.internalGet(YEAR); int y_amount; if (month >= 0) { y_amount = month/12; } else { y_amount = (month+1)/12 - 1; } if (y_amount != 0) { if (this.internalGetEra() == AD) { year += y_amount; if (year > 0) { this.set(YEAR, year); } else { // year <= 0 this.set(YEAR, 1 - year); // if year == 0, you get 1 BC this.set(ERA, BC); } } else { // era == BC year -= y_amount; if (year > 0) { this.set(YEAR, year); } else { // year <= 0 this.set(YEAR, 1 - year); // if year == 0, you get 1 AD this.set(ERA, AD); } } } if (month >= 0) { set(MONTH, (int) (month % 12)); } else { // month < 0 month %= 12; if (month < 0) { month += 12; } set(MONTH, JANUARY + month); } pinDayOfMonth(); } else if (field == ERA) { int era = internalGet(ERA) + amount; if (era < 0) { era = 0; } if (era > 1) { era = 1; } set(ERA, era);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -