📄 dateformat.java
字号:
* @see java.util.Calendar#setLenient */ public void setLenient(boolean lenient) { calendar.setLenient(lenient); } /** * Tell whether date/time parsing is to be lenient. */ public boolean isLenient() { return calendar.isLenient(); } /** * Overrides hashCode */ public int hashCode() { return numberFormat.hashCode(); // just enough fields for a reasonable distribution } /** * Overrides equals */ public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; DateFormat other = (DateFormat) obj; return (// calendar.equivalentTo(other.calendar) // THIS API DOESN'T EXIST YET! calendar.getFirstDayOfWeek() == other.calendar.getFirstDayOfWeek() && calendar.getMinimalDaysInFirstWeek() == other.calendar.getMinimalDaysInFirstWeek() && calendar.isLenient() == other.calendar.isLenient() && calendar.getTimeZone().equals(other.calendar.getTimeZone()) && numberFormat.equals(other.numberFormat)); } /** * Overrides Cloneable */ public Object clone() { DateFormat other = (DateFormat) super.clone(); other.calendar = (Calendar) calendar.clone(); other.numberFormat = (NumberFormat) numberFormat.clone(); return other; } /** * Creates a DateFormat with the given time and/or date style in the given * locale. * @param timeStyle a value from 0 to 3 indicating the time format, * ignored if flags is 2 * @param dateStyle a value from 0 to 3 indicating the time format, * ignored if flags is 1 * @param flags either 1 for a time format, 2 for a date format, * or 3 for a date/time format * @param loc the locale for the format */ private static DateFormat get(int timeStyle, int dateStyle, int flags, Locale loc) { if ((flags & 1) != 0) { if (timeStyle < 0 || timeStyle > 3) { throw new IllegalArgumentException("Illegal time style " + timeStyle); } } else { timeStyle = -1; } if ((flags & 2) != 0) { if (dateStyle < 0 || dateStyle > 3) { throw new IllegalArgumentException("Illegal date style " + dateStyle); } } else { dateStyle = -1; } try { return new SimpleDateFormat(timeStyle, dateStyle, loc); } catch (MissingResourceException e) { return new SimpleDateFormat("M/d/yy h:mm a"); } } /** * Create a new date format. */ protected DateFormat() {} /** * Defines constants that are used as attribute keys in the * <code>AttributedCharacterIterator</code> returned * from <code>DateFormat.formatToCharacterIterator</code> and as * field identifiers in <code>FieldPosition</code>. * <p> * The class also provides two methods to map * between its constants and the corresponding Calendar constants. * * @since 1.4 * @see java.util.Calendar */ public static class Field extends Format.Field { // table of all instances in this class, used by readResolve private static final Map instanceMap = new HashMap(18); // Maps from Calendar constant (such as Calendar.ERA) to Field // constant (such as Field.ERA). private static final Field[] calendarToFieldMapping = new Field[Calendar.FIELD_COUNT]; /** Calendar field. */ private int calendarField; /** * Returns the <code>Field</code> constant that corresponds to * the <code>Calendar</code> constant <code>calendarField</code>. * If there is no direct mapping between the <code>Calendar</code> * constant and a <code>Field</code>, null is returned. * * @throws IllegalArgumentException if <code>calendarField</code> is * not the value of a <code>Calendar</code> field constant. * @param calendarField Calendar field constant * @return Field instance representing calendarField. * @see java.util.Calendar */ public static Field ofCalendarField(int calendarField) { if (calendarField < 0 || calendarField >= calendarToFieldMapping.length) { throw new IllegalArgumentException("Unknown Calendar constant " + calendarField); } return calendarToFieldMapping[calendarField]; } /** * Creates a Field with the specified name. * calendarField is used to identify the <code>Calendar</code> * field this attribute represents. Use -1 if this field does * not have a corresponding <code>Calendar</code> value. * * @param name Name of the attribute * @param calendarField Calendar constant */ protected Field(String name, int calendarField) { super(name); this.calendarField = calendarField; if (this.getClass() == DateFormat.Field.class) { instanceMap.put(name, this); if (calendarField >= 0) { // assert(calendarField < Calendar.FIELD_COUNT); calendarToFieldMapping[calendarField] = this; } } } /** * Returns the <code>Calendar</code> field associated with this * attribute. For example, if this represents the hours field of * a <code>Calendar</code>, this would return * <code>Calendar.HOUR</code>. If there is no corresponding * <code>Calendar</code> constant, this will return -1. * * @return Calendar constant for this field * @see java.util.Calendar */ public int getCalendarField() { return calendarField; } /** * Resolves instances being deserialized to the predefined constants. * * @throws InvalidObjectException if the constant could not be * resolved. * @return resolved DateFormat.Field constant */ protected Object readResolve() throws InvalidObjectException { if (this.getClass() != DateFormat.Field.class) { throw new InvalidObjectException("subclass didn't correctly implement readResolve"); } Object instance = instanceMap.get(getName()); if (instance != null) { return instance; } else { throw new InvalidObjectException("unknown attribute name"); } } // // The constants // /** * Constant identifying the era field. */ public final static Field ERA = new Field("era", Calendar.ERA); /** * Constant identifying the year field. */ public final static Field YEAR = new Field("year", Calendar.YEAR); /** * Constant identifying the month field. */ public final static Field MONTH = new Field("month", Calendar.MONTH); /** * Constant identifying the day of month field. */ public final static Field DAY_OF_MONTH = new Field("day of month", Calendar.DAY_OF_MONTH); /** * Constant identifying the hour of day field, where the legal values * are 1 to 24. */ public final static Field HOUR_OF_DAY1 = new Field("hour of day 1",-1); /** * Constant identifying the hour of day field, where the legal values * are 0 to 23. */ public final static Field HOUR_OF_DAY0 = new Field("hour of day", Calendar.HOUR_OF_DAY); /** * Constant identifying the minute field. */ public final static Field MINUTE =new Field("minute", Calendar.MINUTE); /** * Constant identifying the second field. */ public final static Field SECOND =new Field("second", Calendar.SECOND); /** * Constant identifying the millisecond field. */ public final static Field MILLISECOND = new Field("millisecond", Calendar.MILLISECOND); /** * Constant identifying the day of week field. */ public final static Field DAY_OF_WEEK = new Field("day of week", Calendar.DAY_OF_WEEK); /** * Constant identifying the day of year field. */ public final static Field DAY_OF_YEAR = new Field("day of year", Calendar.DAY_OF_YEAR); /** * Constant identifying the day of week field. */ public final static Field DAY_OF_WEEK_IN_MONTH = new Field("day of week in month", Calendar.DAY_OF_WEEK_IN_MONTH); /** * Constant identifying the week of year field. */ public final static Field WEEK_OF_YEAR = new Field("week of year", Calendar.WEEK_OF_YEAR); /** * Constant identifying the week of month field. */ public final static Field WEEK_OF_MONTH = new Field("week of month", Calendar.WEEK_OF_MONTH); /** * Constant identifying the time of day indicator * (e.g. "a.m." or "p.m.") field. */ public final static Field AM_PM = new Field("am pm", Calendar.AM_PM); /** * Constant identifying the hour field, where the legal values are * 1 to 12. */ public final static Field HOUR1 = new Field("hour 1", -1); /** * Constant identifying the hour field, where the legal values are * 0 to 11. */ public final static Field HOUR0 = new Field("hour", Calendar.HOUR); /** * Constant identifying the time zone field. */ public final static Field TIME_ZONE = new Field("time zone", -1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -