📄 dateformat.java
字号:
} /** * This method formats the specified <code>Object</code> into a date string * and appends it to the specified <code>StringBuffer</code>. * The specified object must be an instance of <code>Number</code> or * <code>Date</code> or an <code>IllegalArgumentException</code> will be * thrown. * * @param obj The <code>Object</code> to format. * @param buf The <code>StringBuffer</code> to append the resultant * <code>String</code> to. * @param pos Is updated to the start and end index of the * specified field. * * @return The <code>StringBuffer</code> supplied on input, with the * formatted date/time appended. */ public final StringBuffer format (Object obj, StringBuffer buf, FieldPosition pos) { if (obj instanceof Number) obj = new Date(((Number) obj).longValue()); else if (! (obj instanceof Date)) throw new IllegalArgumentException ("Cannot format given Object as a Date"); return format ((Date) obj, buf, pos); } /** * Formats the date argument according to the pattern specified. * * @param date The formatted date. */ public final String format (Date date) { StringBuffer sb = new StringBuffer (); format (date, sb, new FieldPosition (MONTH_FIELD)); return sb.toString(); } /** * This method formats a <code>Date</code> into a string and appends it * to the specified <code>StringBuffer</code>. * * @param date The <code>Date</code> value to format. * @param buf The <code>StringBuffer</code> to append the resultant * <code>String</code> to. * @param pos Is updated to the start and end index of the * specified field. * * @return The <code>StringBuffer</code> supplied on input, with the * formatted date/time appended. */ public abstract StringBuffer format (Date date, StringBuffer buf, FieldPosition pos); /** * This method returns a list of available locales supported by this * class. */ public static Locale[] getAvailableLocales() { // FIXME Locale[] l = new Locale[1]; l[0] = Locale.US; return l; } /** * This method returns the <code>Calendar</code> object being used by * this object to parse/format datetimes. * * @return The <code>Calendar</code> being used by this object. * * @see java.util.Calendar */ public Calendar getCalendar () { return calendar; } private static DateFormat computeInstance (int style, Locale loc, boolean use_date, boolean use_time) { return computeInstance (style, style, loc, use_date, use_time); } private static DateFormat computeInstance (int dateStyle, int timeStyle, Locale loc, boolean use_date, boolean use_time) { ResourceBundle res; try { res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", loc, ClassLoader.getSystemClassLoader()); } catch (MissingResourceException x) { res = null; } String pattern = null; if (use_date) { String name, def; switch (dateStyle) { case FULL: name = "fullDateFormat"; def = "EEEE MMMM d, yyyy G"; break; case LONG: name = "longDateFormat"; def = "MMMM d, yyyy"; break; case MEDIUM: name = "mediumDateFormat"; def = "d-MMM-yy"; break; case SHORT: name = "shortDateFormat"; def = "M/d/yy"; break; default: throw new IllegalArgumentException (); } try { pattern = res == null ? def : res.getString(name); } catch (MissingResourceException x) { pattern = def; } } if (use_time) { if (pattern == null) pattern = ""; else pattern += " "; String name, def; switch (timeStyle) { case FULL: name = "fullTimeFormat"; def = "h:mm:ss;S 'o''clock' a z"; break; case LONG: name = "longTimeFormat"; def = "h:mm:ss a z"; break; case MEDIUM: name = "mediumTimeFormat"; def = "h:mm:ss a"; break; case SHORT: name = "shortTimeFormat"; def = "h:mm a"; break; default: throw new IllegalArgumentException (); } String s; try { s = res == null ? def : res.getString(name); } catch (MissingResourceException x) { s = def; } pattern += s; } return new SimpleDateFormat (pattern, loc); } /** * This method returns an instance of <code>DateFormat</code> that will * format using the default formatting style for dates. * * @return A new <code>DateFormat</code> instance. */ public static final DateFormat getDateInstance () { return getDateInstance (DEFAULT, Locale.getDefault()); } /** * This method returns an instance of <code>DateFormat</code> that will * format using the specified formatting style for dates. * * @param style The type of formatting to perform. * * @return A new <code>DateFormat</code> instance. */ public static final DateFormat getDateInstance (int style) { return getDateInstance (style, Locale.getDefault()); } /** * This method returns an instance of <code>DateFormat</code> that will * format using the specified formatting style for dates. The specified * localed will be used in place of the default. * * @param style The type of formatting to perform. * @param loc The desired locale. * * @return A new <code>DateFormat</code> instance. */ public static final DateFormat getDateInstance (int style, Locale loc) { return computeInstance (style, loc, true, false); } /** * This method returns a new instance of <code>DateFormat</code> that * formats both dates and times using the <code>SHORT</code> style. * * @return A new <code>DateFormat</code>instance. */ public static final DateFormat getDateTimeInstance () { return getDateTimeInstance (DEFAULT, DEFAULT, Locale.getDefault()); } /** * This method returns a new instance of <code>DateFormat</code> that * formats both dates and times using the <code>DEFAULT</code> style. * * @return A new <code>DateFormat</code>instance. */ public static final DateFormat getDateTimeInstance (int dateStyle, int timeStyle) { return getDateTimeInstance (dateStyle, timeStyle, Locale.getDefault()); } /** * This method returns a new instance of <code>DateFormat</code> that * formats both dates and times using the specified styles. * * @param dateStyle The desired style for date formatting. * @param timeStyle The desired style for time formatting * * @return A new <code>DateFormat</code>instance. */ public static final DateFormat getDateTimeInstance (int dateStyle, int timeStyle, Locale loc) { return computeInstance (dateStyle, timeStyle, loc, true, true); } /** * This method returns a new instance of <code>DateFormat</code> that * formats both dates and times using the <code>SHORT</code> style. * * @return A new <code>DateFormat</code>instance. */ public static final DateFormat getInstance () { // JCL book says SHORT. return getDateTimeInstance (SHORT, SHORT, Locale.getDefault()); } /** * This method returns the <code>NumberFormat</code> object being used * by this object to parse/format time values. * * @return The <code>NumberFormat</code> in use by this object. */ public NumberFormat getNumberFormat () { return numberFormat; } /** * This method returns an instance of <code>DateFormat</code> that will * format using the default formatting style for times. * * @return A new <code>DateFormat</code> instance. */ public static final DateFormat getTimeInstance () { return getTimeInstance (DEFAULT, Locale.getDefault()); } /** * This method returns an instance of <code>DateFormat</code> that will * format using the specified formatting style for times. * * @param style The type of formatting to perform. * * @return A new <code>DateFormat</code> instance. */ public static final DateFormat getTimeInstance (int style) { return getTimeInstance (style, Locale.getDefault()); } /** * This method returns an instance of <code>DateFormat</code> that will * format using the specified formatting style for times. The specified * localed will be used in place of the default. * * @param style The type of formatting to perform. * @param loc The desired locale. * * @return A new <code>DateFormat</code> instance. */ public static final DateFormat getTimeInstance (int style, Locale loc) { return computeInstance (style, loc, false, true); } /** * This method returns the <code>TimeZone</code> object being used by * this instance. * * @return The time zone in use. */ public TimeZone getTimeZone () { return calendar.getTimeZone(); } /** * This method returns a hash value for this object. * * @return A hash value for this object. */ public int hashCode () { if (numberFormat != null) return numberFormat.hashCode(); else return 0; } /** * This method indicates whether or not the parsing of date and time * values should be done in a lenient value. * * @return <code>true</code> if date/time parsing is lenient, * <code>false</code> otherwise. */ public boolean isLenient () { return calendar.isLenient(); } /** * This method parses the specified date/time string. * * @param source The string to parse. * @return The resultant date. * * @exception ParseException If the specified string cannot be parsed. */ public Date parse (String source) throws ParseException { ParsePosition pos = new ParsePosition(0); Date result = parse (source, pos); if (result == null) { int index = pos.getErrorIndex(); if (index < 0) index = pos.getIndex(); throw new ParseException("invalid Date syntax in \"" + source + '\"', index); } return result; } /** * This method parses the specified <code>String</code> into a * <code>Date</code>. The <code>pos</code> argument contains the * starting parse position on method entry and the ending parse * position on method exit. * * @param source The string to parse. * @param pos The starting parse position in entry, the ending parse * position on exit. * * @return The parsed date, or <code>null</code> if the string cannot * be parsed. */ public abstract Date parse (String source, ParsePosition pos); /** * This method is identical to <code>parse(String, ParsePosition)</code>, * but returns its result as an <code>Object</code> instead of a * <code>Date</code>. * * @param source The string to parse. * @param pos The starting parse position in entry, the ending parse * position on exit. * * @return The parsed date, or <code>null</code> if the string cannot * be parsed. */ public Object parseObject (String source, ParsePosition pos) { return parse(source, pos); } /** * This method specified the <code>Calendar</code> that should be used * by this object to parse/format datetimes. * * @param calendar The new <code>Calendar</code> for this object. * * @see java.util.Calendar */ public void setCalendar (Calendar calendar) { this.calendar = calendar; } /** * This method specifies whether or not this object should be lenient in * the syntax it accepts while parsing date/time values. * * @param lenient <code>true</code> if parsing should be lenient, * <code>false</code> otherwise. */ public void setLenient (boolean lenient) { calendar.setLenient(lenient); } /** * This method specifies the <code>NumberFormat</code> object that should * be used by this object to parse/format times. * * @param numberFormat The <code>NumberFormat</code> in use by this object. */ public void setNumberFormat (NumberFormat numberFormat) { this.numberFormat = numberFormat; } /** * This method sets the time zone that should be used by this object. * * @param timeZone The new time zone. */ public void setTimeZone (TimeZone timeZone) { calendar.setTimeZone(timeZone); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -