calendar.java
来自「java源代码 请看看啊 提点宝贵的意见」· Java 代码 · 共 1,730 行 · 第 1/5 页
JAVA
1,730 行
* The first day of the week, with possible values <code>SUNDAY</code>, * <code>MONDAY</code>, etc. This is a locale-dependent value. * @serial */ private int firstDayOfWeek; /** * The number of days required for the first week in a month or year, * with possible values from 1 to 7. This is a locale-dependent value. * @serial */ private int minimalDaysInFirstWeek; /** * Cache to hold the firstDayOfWeek and minimalDaysInFirstWeek * of a Locale. */ private static Hashtable cachedLocaleData = new Hashtable(3); // Special values of stamp[] static final int UNSET = 0; static final int INTERNALLY_SET = 1; static final int MINIMUM_USER_STAMP = 2; /** * The next available value for <code>stamp[]</code>, an internal array. * This actually should not be written out to the stream, and will probably * be removed from the stream in the near future. In the meantime, * a value of <code>MINIMUM_USER_STAMP</code> should be used. * @serial */ private int nextStamp = MINIMUM_USER_STAMP; // the internal serial version which says which version was written // - 0 (default) for version up to JDK 1.1.5 // - 1 for version from JDK 1.1.6, which writes a correct 'time' value // as well as compatible values for other fields. This is a // transitional format. // - 2 (not implemented yet) a future version, in which fields[], // areFieldsSet, and isTimeSet become transient, and isSet[] is // removed. In JDK 1.1.6 we write a format compatible with version 2. static final int currentSerialVersion = 1; /** * The version of the serialized data on the stream. Possible values: * <dl> * <dt><b>0</b> or not present on stream</dt> * <dd> * JDK 1.1.5 or earlier. * </dd> * <dt><b>1</b></dt> * <dd> * JDK 1.1.6 or later. Writes a correct 'time' value * as well as compatible values for other fields. This is a * transitional format. * </dd> * </dl> * When streaming out this class, the most recent format * and the highest allowable <code>serialVersionOnStream</code> * is written. * @serial * @since JDK1.1.6 */ private int serialVersionOnStream = currentSerialVersion; // Proclaim serialization compatibility with JDK 1.1 static final long serialVersionUID = -1807547505821590642L; /** * Constructs a Calendar with the default time zone * and locale. * @see TimeZone#getDefault */ protected Calendar() { this(TimeZone.getDefault(), Locale.getDefault()); } /** * Constructs a calendar with the specified time zone and locale. * @param zone the time zone to use * @param aLocale the locale for the week data */ protected Calendar(TimeZone zone, Locale aLocale) { fields = new int[FIELD_COUNT]; isSet = new boolean[FIELD_COUNT]; stamp = new int[FIELD_COUNT]; this.zone = zone; setWeekCountData(aLocale); } /** * Gets a calendar using the default time zone and locale. The * <code>Calendar</code> returned is based on the current time * in the default time zone with the default locale. * * @return a Calendar. */ public static Calendar getInstance() { return createCalendar(TimeZone.getDefault(), Locale.getDefault()); } /** * Gets a calendar using the specified time zone and default locale. * The <code>Calendar</code> returned is based on the current time * in the given time zone with the default locale. * * @param zone the time zone to use * @return a Calendar. */ public static Calendar getInstance(TimeZone zone) { return createCalendar(zone, Locale.getDefault()); } /** * Gets a calendar using the default time zone and specified locale. * The <code>Calendar</code> returned is based on the current time * in the default time zone with the given locale. * * @param aLocale the locale for the week data * @return a Calendar. */ public static Calendar getInstance(Locale aLocale) { return createCalendar(TimeZone.getDefault(), aLocale); } /** * Gets a calendar with the specified time zone and locale. * The <code>Calendar</code> returned is based on the current time * in the given time zone with the given locale. * * @param zone the time zone to use * @param aLocale the locale for the week data * @return a Calendar. */ public static Calendar getInstance(TimeZone zone, Locale aLocale) { return createCalendar(zone, aLocale); } private static Calendar createCalendar(TimeZone zone, Locale aLocale) { if (aLocale.getLanguage().compareTo("th") == 0) { if (aLocale.getCountry().compareTo("TH") == 0) { return new sun.util.BuddhistCalendar(zone, aLocale); } } // else create the default calendar return new GregorianCalendar(zone, aLocale); } /** * Gets the list of locales for which Calendars are installed. * @return the list of locales for which Calendars are installed. */ public static synchronized Locale[] getAvailableLocales() { return DateFormat.getAvailableLocales(); } /** * Converts the current field values in <code>fields[]</code> * to the millisecond time value * <code>time</code>. */ protected abstract void computeTime(); /** * Converts * the current millisecond time value * <code>time</code> * to field values in <code>fields[]</code>. * This allows you to sync up the time field values with * a new time that is set for the calendar. The time is <em>not</em> * recomputed first; to recompute the time, then the fields, call the * <code>complete</code> method. * @see #complete */ protected abstract void computeFields(); /** * Gets this Calendar's current time. * @return the current time. * @see #setTime * @see #getTimeInMillis */ public final Date getTime() { return new Date( getTimeInMillis() ); } /** * Sets this Calendar's current time with the given Date. * <p> * Note: Calling <code>setTime()</code> with * <code>Date(Long.MAX_VALUE)</code> or <code>Date(Long.MIN_VALUE)</code> * may yield incorrect field values from <code>get()</code>. * @param date the given Date. * @see #getTime * @see #setTimeInMillis */ public final void setTime(Date date) { setTimeInMillis( date.getTime() ); } /** * Gets this Calendar's current time as a long. * @return the current time as UTC milliseconds from the epoch. * @see #getTime * @see #setTimeInMillis */ public long getTimeInMillis() { if (!isTimeSet) updateTime(); return time; } /** * Sets this Calendar's current time from the given long value. * @param millis the new time in UTC milliseconds from the epoch. * @see #setTime * @see #getTimeInMillis */ public void setTimeInMillis(long millis) { isTimeSet = true; time = millis; areFieldsSet = false; if (!areFieldsSet) { computeFields(); areFieldsSet = true; areAllFieldsSet = true; } } /** * Gets the value for a given time field. * @param field the given time field. * @return the value for the given time field. * @throws ArrayIndexOutOfBoundsException if specified field is out of range * (<tt>field < 0 || field >= FIELD_COUNT</tt>). */ public int get(int field) { complete(); return internalGet(field); //return fields[field]; } /** * Gets the value for a given time field. This is an internal * fast time field value getter for the subclasses. * @param field the given time field. * @return the value for the given time field. */ protected final int internalGet(int field) { return fields[field]; } /** * Sets the value for the given time field. This is an internal * fast setter for subclasses. It does not affect the areFieldsSet, isTimeSet, * or areAllFieldsSet flags. */ final void internalSet(int field, int value) { fields[field] = value; } /** * Clears the value of the given calendar field and resets the * field status flags only. The difference from clear(int) is that * this method doesn't reset isTimeSet. * * @param field the given calendar field. * @throws ArrayIndexOutOfBoundsException if specified field is out of range * (<tt>field < 0 || field >= FIELD_COUNT</tt>). */ final void internalClear(int field) { fields[field] = 0; stamp[field] = UNSET; areFieldsSet = false; areAllFieldsSet = false; isSet[field] = false; } /** * Sets the time field with the given value. * @param field the given time field. * @param value the value to be set for the given time field. * @throws ArrayIndexOutOfBoundsException if specified field is out of range * (<tt>field < 0 || field >= FIELD_COUNT</tt>). */ public void set(int field, int value) { isTimeSet = false; internalSet(field, value); //fields[field] = value; stamp[field] = nextStamp++; if (nextStamp == Integer.MAX_VALUE) { adjustStamp(); } areFieldsSet = false; isSet[field] = true; // Remove later } /** * Sets the values for the fields year, month, and date. * Previous values of other fields are retained. If this is not desired, * call <code>clear</code> first. * @param year the value used to set the YEAR time field. * @param month the value used to set the MONTH time field. * Month value is 0-based. e.g., 0 for January. * @param date the value used to set the DATE time field. */ public final void set(int year, int month, int date) { set(YEAR, year); set(MONTH, month); set(DATE, date); } /** * Sets the values for the fields year, month, date, hour, and minute. * Previous values of other fields are retained. If this is not desired, * call <code>clear</code> first. * @param year the value used to set the YEAR time field. * @param month the value used to set the MONTH time field. * Month value is 0-based. e.g., 0 for January. * @param date the value used to set the DATE time field. * @param hour the value used to set the HOUR_OF_DAY time field. * @param minute the value used to set the MINUTE time field. */ public final void set(int year, int month, int date, int hour, int minute) { set(YEAR, year); set(MONTH, month); set(DATE, date); set(HOUR_OF_DAY, hour); set(MINUTE, minute);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?