📄 dateformat.java
字号:
* pattern character 'z'. * @return the formatted date/time string. */ public abstract StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition); /** * Formats a Date into a date/time string. * @param date the time value to be formatted into a time string. * @return the formatted time string. */ public final String format(Date date) { return format(date, new StringBuffer(), DontCareFieldPosition.INSTANCE).toString(); } /** * Parses text from the beginning of the given string to produce a date. * The method may not use the entire text of the given string. * <p> * See the {@link #parse(String, ParsePosition)} method for more information * on date parsing. * * @param source A <code>String</code> whose beginning should be parsed. * @return A <code>Date</code> parsed from the string. * @exception ParseException if the beginning of the specified string * cannot be parsed. */ public Date parse(String source) throws ParseException { ParsePosition pos = new ParsePosition(0); Date result = parse(source, pos); if (pos.index == 0) throw new ParseException("Unparseable date: \"" + source + "\"" , pos.errorIndex); return result; } /** * Parse a date/time string according to the given parse position. For * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date * that is equivalent to Date(837039928046). * * <p> By default, parsing is lenient: If the input is not in the form used * by this object's format method but can still be parsed as a date, then * the parse succeeds. Clients may insist on strict adherence to the * format by calling setLenient(false). * * @see java.text.DateFormat#setLenient(boolean) * * @param source The date/time string to be parsed * * @param pos On input, the position at which to start parsing; on * output, the position at which parsing terminated, or the * start position if the parse failed. * * @return A Date, or null if the input could not be parsed */ public abstract Date parse(String source, ParsePosition pos); /** * Parses text from a string to produce a <code>Date</code>. * <p> * The method attempts to parse text starting at the index given by * <code>pos</code>. * If parsing succeeds, then the index of <code>pos</code> is updated * to the index after the last character used (parsing does not necessarily * use all characters up to the end of the string), and the parsed * date is returned. The updated <code>pos</code> can be used to * indicate the starting point for the next call to this method. * If an error occurs, then the index of <code>pos</code> is not * changed, the error index of <code>pos</code> is set to the index of * the character where the error occurred, and null is returned. * <p> * See the {@link #parse(String, ParsePosition)} method for more information * on date parsing. * * @param source A <code>String</code>, part of which should be parsed. * @param pos A <code>ParsePosition</code> object with index and error * index information as described above. * @return A <code>Date</code> parsed from the string. In case of * error, returns null. * @exception NullPointerException if <code>pos</code> is null. */ public Object parseObject(String source, ParsePosition pos) { return parse(source, pos); } /** * Constant for full style pattern. */ public static final int FULL = 0; /** * Constant for long style pattern. */ public static final int LONG = 1; /** * Constant for medium style pattern. */ public static final int MEDIUM = 2; /** * Constant for short style pattern. */ public static final int SHORT = 3; /** * Constant for default style pattern. Its value is MEDIUM. */ public static final int DEFAULT = MEDIUM; /** * Gets the time formatter with the default formatting style * for the default locale. * @return a time formatter. */ public final static DateFormat getTimeInstance() { return get(DEFAULT, 0, 1, Locale.getDefault()); } /** * Gets the time formatter with the given formatting style * for the default locale. * @param style the given formatting style. For example, * SHORT for "h:mm a" in the US locale. * @return a time formatter. */ public final static DateFormat getTimeInstance(int style) { return get(style, 0, 1, Locale.getDefault()); } /** * Gets the time formatter with the given formatting style * for the given locale. * @param style the given formatting style. For example, * SHORT for "h:mm a" in the US locale. * @param aLocale the given locale. * @return a time formatter. */ public final static DateFormat getTimeInstance(int style, Locale aLocale) { return get(style, 0, 1, aLocale); } /** * Gets the date formatter with the default formatting style * for the default locale. * @return a date formatter. */ public final static DateFormat getDateInstance() { return get(0, DEFAULT, 2, Locale.getDefault()); } /** * Gets the date formatter with the given formatting style * for the default locale. * @param style the given formatting style. For example, * SHORT for "M/d/yy" in the US locale. * @return a date formatter. */ public final static DateFormat getDateInstance(int style) { return get(0, style, 2, Locale.getDefault()); } /** * Gets the date formatter with the given formatting style * for the given locale. * @param style the given formatting style. For example, * SHORT for "M/d/yy" in the US locale. * @param aLocale the given locale. * @return a date formatter. */ public final static DateFormat getDateInstance(int style, Locale aLocale) { return get(0, style, 2, aLocale); } /** * Gets the date/time formatter with the default formatting style * for the default locale. * @return a date/time formatter. */ public final static DateFormat getDateTimeInstance() { return get(DEFAULT, DEFAULT, 3, Locale.getDefault()); } /** * Gets the date/time formatter with the given date and time * formatting styles for the default locale. * @param dateStyle the given date formatting style. For example, * SHORT for "M/d/yy" in the US locale. * @param timeStyle the given time formatting style. For example, * SHORT for "h:mm a" in the US locale. * @return a date/time formatter. */ public final static DateFormat getDateTimeInstance(int dateStyle, int timeStyle) { return get(timeStyle, dateStyle, 3, Locale.getDefault()); } /** * Gets the date/time formatter with the given formatting styles * for the given locale. * @param dateStyle the given date formatting style. * @param timeStyle the given time formatting style. * @param aLocale the given locale. * @return a date/time formatter. */ public final static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) { return get(timeStyle, dateStyle, 3, aLocale); } /** * Get a default date/time formatter that uses the SHORT style for both the * date and the time. */ public final static DateFormat getInstance() { return getDateTimeInstance(SHORT, SHORT); } /** * Gets the set of locales for which DateFormats are installed. * @return the set of locales for which DateFormats are installed. */ public static Locale[] getAvailableLocales() { return LocaleData.getAvailableLocales("DateTimePatterns"); } /** * Set the calendar to be used by this date format. Initially, the default * calendar for the specified or default locale is used. * @param newCalendar the new Calendar to be used by the date format */ public void setCalendar(Calendar newCalendar) { this.calendar = newCalendar; } /** * Gets the calendar associated with this date/time formatter. * @return the calendar associated with this date/time formatter. */ public Calendar getCalendar() { return calendar; } /** * Allows you to set the number formatter. * @param newNumberFormat the given new NumberFormat. */ public void setNumberFormat(NumberFormat newNumberFormat) { this.numberFormat = newNumberFormat; } /** * Gets the number formatter which this date/time formatter uses to * format and parse a time. * @return the number formatter which this date/time formatter uses. */ public NumberFormat getNumberFormat() { return numberFormat; } /** * Sets the time zone for the calendar of this DateFormat object. * @param zone the given new time zone. */ public void setTimeZone(TimeZone zone) { calendar.setTimeZone(zone); } /** * Gets the time zone. * @return the time zone associated with the calendar of DateFormat. */ public TimeZone getTimeZone() { return calendar.getTimeZone(); } /** * Specify whether or not date/time parsing is to be lenient. With * lenient parsing, the parser may use heuristics to interpret inputs that * do not precisely match this object's format. With strict parsing, * inputs must match this object's format. * @param lenient when true, parsing is lenient
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -