📄 calendar.java
字号:
{ return ResourceBundle.getBundle(bundleName, locale, ClassLoader.getSystemClassLoader()); } /** * Constructs a new Calendar with the default time zone and the default * locale. */ protected Calendar() { this(TimeZone.getDefault(), Locale.getDefault()); } /** * Constructs a new Calendar with the given time zone and the given * locale. * @param zone a time zone. * @param locale a locale. */ protected Calendar(TimeZone zone, Locale locale) { this.zone = zone; lenient = true; ResourceBundle rb = getBundle(locale); firstDayOfWeek = ((Integer) rb.getObject("firstDayOfWeek")).intValue(); minimalDaysInFirstWeek = ((Integer) rb.getObject("minimalDaysInFirstWeek")) .intValue(); clear(); } /** * Creates a calendar representing the actual time, using the default * time zone and locale. */ public static synchronized Calendar getInstance() { return getInstance(TimeZone.getDefault(), Locale.getDefault()); } /** * Creates a calendar representing the actual time, using the given * time zone and the default locale. * @param zone a time zone. */ public static synchronized Calendar getInstance(TimeZone zone) { return getInstance(zone, Locale.getDefault()); } /** * Creates a calendar representing the actual time, using the default * time zone and the given locale. * @param locale a locale. */ public static synchronized Calendar getInstance(Locale locale) { return getInstance(TimeZone.getDefault(), locale); } /** * Cache of locale->calendar-class mappings. This avoids having to do a ResourceBundle * lookup for every getInstance call. */ private static HashMap cache = new HashMap(); /** Preset argument types for calendar-class constructor lookup. */ private static Class[] ctorArgTypes = new Class[] { TimeZone.class, Locale.class }; /** * Creates a calendar representing the actual time, using the given * time zone and locale. * @param zone a time zone. * @param locale a locale. */ public static synchronized Calendar getInstance(TimeZone zone, Locale locale) { Class calendarClass = (Class) cache.get(locale); Throwable exception = null; try { if (calendarClass == null) { ResourceBundle rb = getBundle(locale); String calendarClassName = rb.getString("calendarClass"); if (calendarClassName != null) { calendarClass = Class.forName(calendarClassName); if (Calendar.class.isAssignableFrom(calendarClass)) cache.put(locale, calendarClass); } } // GregorianCalendar is by far the most common case. Optimize by // avoiding reflection. if (calendarClass == GregorianCalendar.class) return new GregorianCalendar(zone, locale); if (Calendar.class.isAssignableFrom(calendarClass)) { Constructor ctor = calendarClass.getConstructor(ctorArgTypes); return (Calendar) ctor.newInstance(new Object[] { zone, locale }); } } catch (ClassNotFoundException ex) { exception = ex; } catch (IllegalAccessException ex) { exception = ex; } catch (NoSuchMethodException ex) { exception = ex; } catch (InstantiationException ex) { exception = ex; } catch (InvocationTargetException ex) { exception = ex; } throw new RuntimeException("Error instantiating calendar for locale " + locale, exception); } /** * Gets the set of locales for which a Calendar is available. * @exception MissingResourceException if locale data couldn't be found. * @return the set of locales. */ public static synchronized Locale[] getAvailableLocales() { ResourceBundle rb = getBundle(new Locale("", "")); return (Locale[]) rb.getObject("availableLocales"); } /** * Converts the time field values (<code>fields</code>) to * milliseconds since the epoch UTC (<code>time</code>). Override * this method if you write your own Calendar. */ protected abstract void computeTime(); /** * Converts the milliseconds since the epoch UTC * (<code>time</code>) to time fields * (<code>fields</code>). Override this method if you write your * own Calendar. */ protected abstract void computeFields(); /** * Converts the time represented by this object to a * <code>Date</code>-Object. * @return the Date. */ public final Date getTime() { if (! isTimeSet) computeTime(); return new Date(time); } /** * Sets this Calendar's time to the given Date. All time fields * are invalidated by this method. */ public final void setTime(Date date) { setTimeInMillis(date.getTime()); } /** * Returns the time represented by this Calendar. * @return the time in milliseconds since the epoch. * @specnote This was made public in 1.4. */ public long getTimeInMillis() { if (! isTimeSet) computeTime(); return time; } /** * Sets this Calendar's time to the given Time. All time fields * are invalidated by this method. * @param time the time in milliseconds since the epoch * @specnote This was made public in 1.4. */ public void setTimeInMillis(long time) { clear(); this.time = time; isTimeSet = true; computeFields(); } /** * Gets the value of the specified field. They are recomputed * if they are invalid. * @param field the time field. One of the time field constants. * @return the value of the specified field * @throws ArrayIndexOutOfBoundsException if the field is outside * the valid range. The value of field must be >= 0 and * <= <code>FIELD_COUNT</code>. * @specnote Not final since JDK 1.4 */ public int get(int field) { // If the requested field is invalid, force all fields to be recomputed. if (! isSet[field]) areFieldsSet = false; complete(); return fields[field]; } /** * Gets the value of the specified field. This method doesn't * recompute the fields, if they are invalid. * @param field the time field. One of the time field constants. * @return the value of the specified field, undefined if * <code>areFieldsSet</code> or <code>isSet[field]</code> is false. * @throws ArrayIndexOutOfBoundsException if the field is outside * the valid range. The value of field must be >= 0 and * <= <code>FIELD_COUNT</code>. */ protected final int internalGet(int field) { return fields[field]; } /** * Sets the time field with the given value. This does invalidate * the time in milliseconds. * @param field the time field. One of the time field constants * @param value the value to be set. * @throws ArrayIndexOutOfBoundsException if field is outside * the valid range. The value of field must be >= 0 and * <= <code>FIELD_COUNT</code>. * @specnote Not final since JDK 1.4 */ public void set(int field, int value) { if (isTimeSet) for (int i = 0; i < FIELD_COUNT; i++) isSet[i] = false; isTimeSet = false; fields[field] = value; isSet[field] = true; // The five valid date patterns, in order of priority // 1 YEAR + MONTH + DAY_OF_MONTH // 2 YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK // 3 YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK // 4 YEAR + DAY_OF_YEAR // 5 YEAR + DAY_OF_WEEK + WEEK_OF_YEAR switch (field) { case MONTH: // pattern 1,2 or 3 isSet[DAY_OF_YEAR] = false; isSet[WEEK_OF_YEAR] = false; break; case DAY_OF_MONTH: // pattern 1 isSet[YEAR] = true; isSet[MONTH] = true; isSet[WEEK_OF_MONTH] = true; isSet[DAY_OF_WEEK] = false; isSet[DAY_OF_WEEK_IN_MONTH] = false; isSet[DAY_OF_YEAR] = false; isSet[WEEK_OF_YEAR] = false; break; case WEEK_OF_MONTH: // pattern 2 if (! isSet[DAY_OF_WEEK]) fields[DAY_OF_WEEK] = getFirstDayOfWeek(); isSet[YEAR] = true; isSet[MONTH] = true; isSet[DAY_OF_WEEK] = true; isSet[DAY_OF_MONTH] = false; isSet[DAY_OF_WEEK_IN_MONTH] = false; isSet[DAY_OF_YEAR] = false; isSet[WEEK_OF_YEAR] = false; break; case DAY_OF_WEEK_IN_MONTH: // pattern 3 if (! isSet[DAY_OF_WEEK]) fields[DAY_OF_WEEK] = getFirstDayOfWeek(); isSet[YEAR] = true; isSet[MONTH] = true; isSet[DAY_OF_WEEK] = true; isSet[DAY_OF_YEAR] = false; isSet[DAY_OF_MONTH] = false; isSet[WEEK_OF_MONTH] = false; isSet[WEEK_OF_YEAR] = false; break; case DAY_OF_YEAR: // pattern 4 isSet[YEAR] = true; isSet[MONTH] = false; isSet[WEEK_OF_MONTH] = false; isSet[DAY_OF_MONTH] = false; isSet[DAY_OF_WEEK] = false; isSet[WEEK_OF_YEAR] = false; isSet[DAY_OF_WEEK_IN_MONTH] = false; break; case WEEK_OF_YEAR: // pattern 5 if (! isSet[DAY_OF_WEEK]) fields[DAY_OF_WEEK] = getFirstDayOfWeek(); isSet[YEAR] = true; isSet[DAY_OF_WEEK] = true; isSet[MONTH] = false; isSet[DAY_OF_MONTH] = false; isSet[WEEK_OF_MONTH] = false; isSet[DAY_OF_YEAR] = false; isSet[DAY_OF_WEEK_IN_MONTH] = false; break; case AM_PM: isSet[HOUR] = true; isSet[HOUR_OF_DAY] = false; break; case HOUR_OF_DAY: isSet[AM_PM] = false; isSet[HOUR] = false; break; case HOUR: isSet[AM_PM] = true; isSet[HOUR_OF_DAY] = false; break; case DST_OFFSET: explicitDSTOffset = true; } // May have crossed over a DST boundary. if (! explicitDSTOffset && (field != DST_OFFSET && field != ZONE_OFFSET)) isSet[DST_OFFSET] = false; } /** * Sets the fields for year, month, and date * @param year the year. * @param month the month, one of the constants JANUARY..UNDICEMBER. * @param date the day of the month */ public final void set(int year, int month, int date) { isTimeSet = false; fields[YEAR] = year; fields[MONTH] = month; fields[DATE] = date; isSet[YEAR] = isSet[MONTH] = isSet[DATE] = true; isSet[WEEK_OF_YEAR] = false; isSet[DAY_OF_YEAR] = false; isSet[WEEK_OF_MONTH] = false; isSet[DAY_OF_WEEK] = false; isSet[DAY_OF_WEEK_IN_MONTH] = false; isSet[ERA] = false; if (! explicitDSTOffset) isSet[DST_OFFSET] = false; // May have crossed a DST boundary. } /** * Sets the fields for year, month, date, hour, and minute * @param year the year. * @param month the month, one of the constants JANUARY..UNDICEMBER. * @param date the day of the month * @param hour the hour of day. * @param minute the minute. */ public final void set(int year, int month, int date, int hour, int minute) { set(year, month, date); fields[HOUR_OF_DAY] = hour; fields[MINUTE] = minute; isSet[HOUR_OF_DAY] = isSet[MINUTE] = true; isSet[AM_PM] = false; isSet[HOUR] = false; } /** * Sets the fields for year, month, date, hour, and minute * @param year the year. * @param month the month, one of the constants JANUARY..UNDICEMBER. * @param date the day of the month * @param hour the hour of day. * @param minute the minute. * @param second the second. */ public final void set(int year, int month, int date, int hour, int minute, int second) { set(year, month, date, hour, minute); fields[SECOND] = second; isSet[SECOND] = true; } /** * Clears the values of all the time fields. */ public final void clear() { isTimeSet = false; areFieldsSet = false; int zoneOffs = zone.getRawOffset(); int[] tempFields = { 1, 1970, JANUARY, 1, 1, 1, 1, THURSDAY, 1, AM, 0, 0, 0, 0, 0, zoneOffs, 0 }; fields = tempFields; for (int i = 0; i < FIELD_COUNT; i++) isSet[i] = false; } /** * Clears the values of the specified time field. * @param field the time field. One of the time field constants. * @throws ArrayIndexOutOfBoundsException if field is outside
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -