calendar.java
来自「java源代码 请看看啊 提点宝贵的意见」· Java 代码 · 共 1,730 行 · 第 1/5 页
JAVA
1,730 行
* @param field the field to determine the minimum of * @return the minimum of the given field for the current date of this Calendar * @since 1.2 */ public int getActualMinimum(int field) { int fieldValue = getGreatestMinimum(field); int endValue = getMinimum(field); // if we know that the minimum value is always the same, just return it if (fieldValue == endValue) { return fieldValue; } // clone the calendar so we don't mess with the real one, and set it to // accept anything for the field values Calendar work = (Calendar)this.clone(); work.setLenient(true); // now try each value from getLeastMaximum() to getMaximum() one by one until // we get a value that normalizes to another value. The last value that // normalizes to itself is the actual minimum for the current date int result = fieldValue; do { work.set(field, fieldValue); if (work.get(field) != fieldValue) { break; } else { result = fieldValue; fieldValue--; } } while (fieldValue >= endValue); return result; } /** * Return the maximum value that this field could have, given the current date. * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar, * for some years the actual maximum for MONTH is 12, and for others 13. * * The version of this function on Calendar uses an iterative algorithm to determine the * actual maximum value for the field. There is almost always a more efficient way to * accomplish this (in most cases, you can simply return getMaximum()). GregorianCalendar * overrides this function with a more efficient implementation. * * @param field the field to determine the maximum of * @return the maximum of the given field for the current date of this Calendar * @since 1.2 */ public int getActualMaximum(int field) { int fieldValue = getLeastMaximum(field); int endValue = getMaximum(field); // if we know that the maximum value is always the same, just return it if (fieldValue == endValue) { return fieldValue; } // clone the calendar so we don't mess with the real one, and set it to // accept anything for the field values Calendar work = (Calendar)this.clone(); work.setLenient(true); // if we're counting weeks, set the day of the week to Sunday. We know the // last week of a month or year will contain the first day of the week. if (field == WEEK_OF_YEAR || field == WEEK_OF_MONTH) work.set(DAY_OF_WEEK, firstDayOfWeek); // now try each value from getLeastMaximum() to getMaximum() one by one until // we get a value that normalizes to another value. The last value that // normalizes to itself is the actual maximum for the current date int result = fieldValue; do { work.set(field, fieldValue); if (work.get(field) != fieldValue) { break; } else { result = fieldValue; fieldValue++; } } while (fieldValue <= endValue); return result; } /** * Overrides Cloneable */ public Object clone() { try { Calendar other = (Calendar) super.clone(); other.fields = new int[FIELD_COUNT]; other.isSet = new boolean[FIELD_COUNT]; other.stamp = new int[FIELD_COUNT]; System.arraycopy(this.fields, 0, other.fields, 0, FIELD_COUNT); System.arraycopy(this.isSet, 0, other.isSet, 0, FIELD_COUNT); System.arraycopy(this.stamp, 0, other.stamp, 0, FIELD_COUNT); other.zone = (TimeZone) zone.clone(); return other; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(); } } private static final String[] FIELD_NAME = { ",ERA=", ",YEAR=", ",MONTH=", ",WEEK_OF_YEAR=", ",WEEK_OF_MONTH=", ",DAY_OF_MONTH=", ",DAY_OF_YEAR=", ",DAY_OF_WEEK=", ",DAY_OF_WEEK_IN_MONTH=", ",AM_PM=", ",HOUR=", ",HOUR_OF_DAY=", ",MINUTE=", ",SECOND=", ",MILLISECOND=", ",ZONE_OFFSET=", ",DST_OFFSET=" }; /** * Return a string representation of this calendar. This method * is intended to be used only for debugging purposes, and the * format of the returned string may vary between implementations. * The returned string may be empty but may not be <code>null</code>. * * @return a string representation of this calendar. */ public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append(getClass().getName()); buffer.append("[time="); buffer.append(isTimeSet ? String.valueOf(time) : "?"); buffer.append(",areFieldsSet="); buffer.append(areFieldsSet); buffer.append(",areAllFieldsSet="); buffer.append(areAllFieldsSet); buffer.append(",lenient="); buffer.append(lenient); buffer.append(",zone="); buffer.append(zone); buffer.append(",firstDayOfWeek="); buffer.append(firstDayOfWeek); buffer.append(",minimalDaysInFirstWeek="); buffer.append(minimalDaysInFirstWeek); for (int i=0; i<FIELD_COUNT; ++i) { buffer.append(FIELD_NAME[i]); buffer.append(isSet(i) ? String.valueOf(fields[i]) : "?"); } buffer.append(']'); return buffer.toString(); } // =======================privates=============================== /** * Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent. * They are used to figure out the week count for a specific date for * a given locale. These must be set when a Calendar is constructed. * @param desiredLocale the given locale. */ private void setWeekCountData(Locale desiredLocale) { /* try to get the Locale data from the cache */ int[] data = (int[]) cachedLocaleData.get(desiredLocale); if (data == null) { /* cache miss */ ResourceBundle resource = LocaleData.getLocaleElements(desiredLocale); String[] dateTimePatterns = resource.getStringArray("DateTimeElements"); data = new int[2]; data[0] = Integer.parseInt(dateTimePatterns[0]); data[1] = Integer.parseInt(dateTimePatterns[1]); /* cache update */ cachedLocaleData.put(desiredLocale, data); } firstDayOfWeek = data[0]; minimalDaysInFirstWeek = data[1]; } /** * Recompute the time and update the status fields isTimeSet * and areFieldsSet. Callers should check isTimeSet and only * call this method if isTimeSet is false. */ private void updateTime() { computeTime(); // If we are lenient, we need to recompute the fields to normalize // the values. Also, if we haven't set all the fields yet (i.e., // in a newly-created object), we need to fill in the fields. [LIU] if (isLenient() || !areAllFieldsSet) areFieldsSet = false; isTimeSet = true; } /** * Adjusts the stamp[] values before nextStamp overflow. nextStamp * is set to the next stamp value upon the return. */ private final void adjustStamp() { int max = MINIMUM_USER_STAMP; int newStamp = MINIMUM_USER_STAMP; for (;;) { int min = Integer.MAX_VALUE; for (int i = 0; i < stamp.length; i++) { int v = stamp[i]; if (v >= newStamp && min > v) { min = v; } if (max < v) { max = v; } } if (max != min && min == Integer.MAX_VALUE) { break; } for (int i = 0; i < stamp.length; i++) { if (stamp[i] == min) { stamp[i] = newStamp; } } newStamp++; if (min == max) { break; } } nextStamp = newStamp; } /** * Invalidates the WEEK_OF_MONTH and WEEK_OF_YEAR fields if they * have been calculated internally. */ private void invalidateWeekFields() { if (stamp[WEEK_OF_MONTH] == INTERNALLY_SET) { stamp[WEEK_OF_MONTH] = UNSET; isSet[WEEK_OF_MONTH] = false; areFieldsSet = false; } if (stamp[WEEK_OF_YEAR] == INTERNALLY_SET) { stamp[WEEK_OF_YEAR] = UNSET; isSet[WEEK_OF_YEAR] = false; areFieldsSet = false; } } /** * Save the state of this object to a stream (i.e., serialize it). * * Ideally, <code>Calendar</code> would only write out its state data and * the current time, and not write any field data out, such as * <code>fields[]</code>, <code>isTimeSet</code>, <code>areFieldsSet</code>, * and <code>isSet[]</code>. <code>nextStamp</code> also should not be part * of the persistent state. Unfortunately, this didn't happen before JDK 1.1 * shipped. To be compatible with JDK 1.1, we will always have to write out * the field values and state flags. However, <code>nextStamp</code> can be * removed from the serialization stream; this will probably happen in the * near future. */ private void writeObject(ObjectOutputStream stream) throws IOException { // Try to compute the time correctly, for the future (stream // version 2) in which we don't write out fields[] or isSet[]. if (!isTimeSet) { try { updateTime(); } catch (IllegalArgumentException e) {} } // If this Calendar has a ZoneInfo, save it and set a // SimpleTimeZone equvalent (as a single DST schedule) for // backward compatibility. TimeZone savedZone = null; if (zone instanceof ZoneInfo) { SimpleTimeZone stz = ((ZoneInfo)zone).getLastRuleInstance(); if (stz == null) { stz = new SimpleTimeZone(zone.getRawOffset(), zone.getID()); } savedZone = zone; zone = stz; } // Write out the 1.1 FCS object. stream.defaultWriteObject(); // Write out the ZoneInfo object // 4802409: we write out even if it is null, a temporary workaround // the real fix for bug 4844924 in corba-iiop stream.writeObject(savedZone); if (savedZone != null) { zone = savedZone; } } /** * Reconstitute this object from a stream (i.e., deserialize it). */ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); stamp = new int[FIELD_COUNT]; // Starting with version 2 (not implemented yet), we expect that // fields[], isSet[], isTimeSet, and areFieldsSet may not be // streamed out anymore. We expect 'time' to be correct. if (serialVersionOnStream >= 2) { isTimeSet = true; if (fields == null) fields = new int[FIELD_COUNT]; if (isSet == null) isSet = new boolean[FIELD_COUNT]; } else if (serialVersionOnStream >= 0) { for (int i=0; i<FIELD_COUNT; ++i) stamp[i] = isSet[i] ? INTERNALLY_SET : UNSET; } serialVersionOnStream = currentSerialVersion; // If there's a ZoneInfo object, use it for zone. try { ZoneInfo zi = (ZoneInfo) stream.readObject(); if (zi != null) { zone = zi; } } catch (Exception e) { } // If the deserialized object has a SimpleTimeZone, try to // replace it with a ZoneInfo equivalent (as of 1.4) in order // to be compatible with the SimpleTimeZone-based // implementation as much as possible. TimeZone tz = getTimeZone(); if (tz instanceof SimpleTimeZone) { String id = tz.getID(); TimeZone zi = TimeZone.getTimeZone(id); if (zi != null && zi.hasSameRules(tz) && zi.getID().equals(id)) { setTimeZone(zi); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?