dateformat.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 622 行 · 第 1/2 页
JAVA
622 行
/**
* 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 text 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 text, ParsePosition pos);
/**
* Parse a date/time string into an Object. This convenience method simply
* calls parse(String, ParsePosition).
*
* @see #parse(String, ParsePosition)
*/
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.
*/
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, -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, -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 inLocale the given locale.
* @return a time formatter.
*/
public final static DateFormat getTimeInstance(int style,
Locale aLocale)
{
return get(style, -1, aLocale);
}
/**
* Gets the date formatter with the default formatting style
* for the default locale.
* @return a date formatter.
*/
public final static DateFormat getDateInstance()
{
// +4 to set the correct index for getting data out of
// LocaleElements.
return get(-1, DEFAULT + 4, 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(-1, style + 4, 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 inLocale the given locale.
* @return a date formatter.
*/
public final static DateFormat getDateInstance(int style,
Locale aLocale)
{
return get(-1, style + 4, 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 + 4, 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 + 4, 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 inLocale the given locale.
* @return a date/time formatter.
*/
public final static DateFormat
getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
{
return get(timeStyle, dateStyle + 4, 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.
*/
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.
* @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;
}
private static DateFormat get(int timeStyle, /* -1 for no time */
int dateStyle, /* -1 for no date */
Locale loc) {
try {
ResourceBundle resource
= ResourceBundle.getBundle
("java.text.resources.LocaleElements", loc);
return new SimpleDateFormat(timeStyle, dateStyle, loc);
} catch (MissingResourceException e) {
return new SimpleDateFormat("M/d/yy h:mm a");
}
}
protected DateFormat() { }
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?