DateFormatpublic abstract class DateFormat extends Format An abstract class for date/time formatting subclasses which formats and
parses dates or time in a language-independent manner. The date/time
formatting subclass, such as {@link SimpleDateFormat}, allows for formatting
(i.e., date -> text), parsing (text -> date), and normalization. The date is
represented as a {@code Date} object or as the milliseconds since January 1,
1970, 00:00:00 GMT.
DateFormat provides many class methods for obtaining default date/time
formatters based on the default or a given locale and a number of formatting
styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More
details and examples for using these styles are provided in the method
descriptions.
{@code DateFormat} helps you to format and parse dates for any locale. Your
code can be completely independent of the locale conventions for months, days
of the week, or even the calendar format: lunar vs. solar.
To format a date for the current Locale, use one of the static factory
methods:
myString = DateFormat.getDateInstance().format(myDate);
If you are formatting multiple dates, it is more efficient to get the format
and use it multiple times so that the system doesn't have to fetch the
information about the local language and country conventions multiple times.
DateFormat df = DateFormat.getDateInstance();
for (int i = 0; i < a.length; ++i) {
output.println(df.format(myDate[i]) + "; ");
}
To format a number for a different locale, specify it in the call to
{@code getDateInstance}:
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
{@code DateFormat} can also be used to parse strings:
myDate = df.parse(myString);
Use {@code getDateInstance} to get the normal date format for a country.
Other static factory methods are available: Use {@code getTimeInstance} to
get the time format for a country. Use {@code getDateTimeInstance} to get the
date and time format. You can pass in different options to these factory
methods to control the length of the result; from SHORT to MEDIUM to LONG to
FULL. The exact result depends on the locale, but generally:
- SHORT is completely numeric, such as 12.13.52 or 3:30pm
- MEDIUM is longer, such as Jan 12, 1952
- LONG is longer, such as January 12, 1952 or 3:30:32pm
- FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD
or 3:30:42pm PST.
If needed, the time zone can be set on the format. For even greater control
over the formatting or parsing, try casting the {@code DateFormat} you get
from the factory methods to a {@code SimpleDateFormat}. This will work for
the majority of countries; just remember to put it in a try block in case you
encounter an unusual one.
There are versions of the parse and format methods which use
{@code ParsePosition} and {@code FieldPosition} to allow you to
- progressively parse through pieces of a string;
- align any particular field.
Synchronization
Date formats are not synchronized. It is recommended to create separate
format instances for each thread. If multiple threads access a format
concurrently, it must be synchronized externally.
|
Fields Summary |
---|
private static final long | serialVersionUID | protected Calendar | calendarThe calendar that this {@code DateFormat} uses to format a number
representing a date. | protected NumberFormat | numberFormatThe number format used to format a number. | public static final int | DEFAULTThe format style constant defining the default format style. The default
is MEDIUM. | public static final int | FULLThe format style constant defining the full style. | public static final int | LONGThe format style constant defining the long style. | public static final int | MEDIUMThe format style constant defining the medium style. | public static final int | SHORTThe format style constant defining the short style. | public static final int | ERA_FIELDThe {@code FieldPosition} selector for 'G' field alignment, corresponds
to the {@link Calendar#ERA} field. | public static final int | YEAR_FIELDThe {@code FieldPosition} selector for 'y' field alignment, corresponds
to the {@link Calendar#YEAR} field. | public static final int | MONTH_FIELDThe {@code FieldPosition} selector for 'M' field alignment, corresponds
to the {@link Calendar#MONTH} field. | public static final int | DATE_FIELDThe {@code FieldPosition} selector for 'd' field alignment, corresponds
to the {@link Calendar#DATE} field. | public static final int | HOUR_OF_DAY1_FIELDThe {@code FieldPosition} selector for 'k' field alignment, corresponds
to the {@link Calendar#HOUR_OF_DAY} field. {@code HOUR_OF_DAY1_FIELD} is
used for the one-based 24-hour clock. For example, 23:59 + 01:00 results
in 24:59. | public static final int | HOUR_OF_DAY0_FIELDThe {@code FieldPosition} selector for 'H' field alignment, corresponds
to the {@link Calendar#HOUR_OF_DAY} field. {@code HOUR_OF_DAY0_FIELD} is
used for the zero-based 24-hour clock. For example, 23:59 + 01:00 results
in 00:59. | public static final int | MINUTE_FIELDFieldPosition selector for 'm' field alignment, corresponds to the
{@link Calendar#MINUTE} field. | public static final int | SECOND_FIELDFieldPosition selector for 's' field alignment, corresponds to the
{@link Calendar#SECOND} field. | public static final int | MILLISECOND_FIELDFieldPosition selector for 'S' field alignment, corresponds to the
{@link Calendar#MILLISECOND} field. | public static final int | DAY_OF_WEEK_FIELDFieldPosition selector for 'E' field alignment, corresponds to the
{@link Calendar#DAY_OF_WEEK} field. | public static final int | DAY_OF_YEAR_FIELDFieldPosition selector for 'D' field alignment, corresponds to the
{@link Calendar#DAY_OF_YEAR} field. | public static final int | DAY_OF_WEEK_IN_MONTH_FIELDFieldPosition selector for 'F' field alignment, corresponds to the
{@link Calendar#DAY_OF_WEEK_IN_MONTH} field. | public static final int | WEEK_OF_YEAR_FIELDFieldPosition selector for 'w' field alignment, corresponds to the
{@link Calendar#WEEK_OF_YEAR} field. | public static final int | WEEK_OF_MONTH_FIELDFieldPosition selector for 'W' field alignment, corresponds to the
{@link Calendar#WEEK_OF_MONTH} field. | public static final int | AM_PM_FIELDFieldPosition selector for 'a' field alignment, corresponds to the
{@link Calendar#AM_PM} field. | public static final int | HOUR1_FIELDFieldPosition selector for 'h' field alignment, corresponding to the
{@link Calendar#HOUR} field. {@code HOUR1_FIELD} is used for the
one-based 12-hour clock. For example, 11:30 PM + 1 hour results in 12:30
AM. | public static final int | HOUR0_FIELDThe {@code FieldPosition} selector for 'z' field alignment, corresponds
to the {@link Calendar#ZONE_OFFSET} and {@link Calendar#DST_OFFSET}
fields. | public static final int | TIMEZONE_FIELDThe {@code FieldPosition} selector for 'z' field alignment, corresponds
to the {@link Calendar#ZONE_OFFSET} and {@link Calendar#DST_OFFSET}
fields. |
Constructors Summary |
---|
protected DateFormat()Constructs a new instance of {@code DateFormat}.
|
Methods Summary |
---|
private static void | checkDateStyle(int style)
if (!(style == SHORT || style == MEDIUM || style == LONG
|| style == FULL || style == DEFAULT)) {
// text.0E=Illegal date style: {0}
throw new IllegalArgumentException(Messages.getString(
"text.0E", style)); //$NON-NLS-1$
}
| private static void | checkTimeStyle(int style)
if (!(style == SHORT || style == MEDIUM || style == LONG
|| style == FULL || style == DEFAULT)) {
// text.0F=Illegal time style: {0}
throw new IllegalArgumentException(Messages.getString(
"text.0F", style)); //$NON-NLS-1$
}
| public java.lang.Object | clone()Returns a new instance of {@code DateFormat} with the same properties.
DateFormat clone = (DateFormat) super.clone();
clone.calendar = (Calendar) calendar.clone();
clone.numberFormat = (NumberFormat) numberFormat.clone();
return clone;
| public boolean | equals(java.lang.Object object)Compares this date format with the specified object and indicates if they
are equal.
if (this == object) {
return true;
}
if (!(object instanceof DateFormat)) {
return false;
}
DateFormat dateFormat = (DateFormat) object;
return numberFormat.equals(dateFormat.numberFormat)
&& calendar.getTimeZone().equals(
dateFormat.calendar.getTimeZone())
&& calendar.getFirstDayOfWeek() == dateFormat.calendar
.getFirstDayOfWeek()
&& calendar.getMinimalDaysInFirstWeek() == dateFormat.calendar
.getMinimalDaysInFirstWeek()
&& calendar.isLenient() == dateFormat.calendar.isLenient();
| public final java.lang.StringBuffer | format(java.lang.Object object, java.lang.StringBuffer buffer, java.text.FieldPosition field)Formats the specified object as a string using the pattern of this date
format and appends the string to the specified string buffer.
If the {@code field} member of {@code field} contains a value specifying
a format field, then its {@code beginIndex} and {@code endIndex} members
will be updated with the position of the first occurrence of this field
in the formatted text.
if (object instanceof Date) {
return format((Date) object, buffer, field);
}
if (object instanceof Number) {
return format(new Date(((Number) object).longValue()), buffer,
field);
}
throw new IllegalArgumentException();
| public final java.lang.String | format(java.util.Date date)Formats the specified date using the rules of this date format.
return format(date, new StringBuffer(), new FieldPosition(0))
.toString();
| public abstract java.lang.StringBuffer | format(java.util.Date date, java.lang.StringBuffer buffer, java.text.FieldPosition field)Formats the specified date as a string using the pattern of this date
format and appends the string to the specified string buffer.
If the {@code field} member of {@code field} contains a value specifying
a format field, then its {@code beginIndex} and {@code endIndex} members
will be updated with the position of the first occurrence of this field
in the formatted text.
| public static java.util.Locale[] | getAvailableLocales()Gets the list of installed locales which support {@code DateFormat}.
return Locale.getAvailableLocales();
| public java.util.Calendar | getCalendar()Returns the calendar used by this {@code DateFormat}.
return calendar;
| public static final java.text.DateFormat | getDateInstance(int style)Returns a {@code DateFormat} instance for formatting and parsing dates in
the specified style for the default locale.
checkDateStyle(style);
return getDateInstance(style, Locale.getDefault());
| public static final java.text.DateFormat | getDateInstance(int style, java.util.Locale locale)Returns a {@code DateFormat} instance for formatting and parsing dates in
the specified style for the specified locale.
checkDateStyle(style);
ResourceBundle bundle = getBundle(locale);
String pattern = bundle.getString("Date_" + getStyleName(style)); //$NON-NLS-1$
return new SimpleDateFormat(pattern, locale);
| public static final java.text.DateFormat | getDateInstance()Returns a {@code DateFormat} instance for formatting and parsing dates in
the DEFAULT style for the default locale.
return getDateInstance(DEFAULT);
| public static final java.text.DateFormat | getDateTimeInstance()Returns a {@code DateFormat} instance for formatting and parsing dates
and time values in the DEFAULT style for the default locale.
return getDateTimeInstance(DEFAULT, DEFAULT);
| public static final java.text.DateFormat | getDateTimeInstance(int dateStyle, int timeStyle)Returns a {@code DateFormat} instance for formatting and parsing of both
dates and time values in the manner appropriate for the default locale.
checkTimeStyle(timeStyle);
checkDateStyle(dateStyle);
return getDateTimeInstance(dateStyle, timeStyle, Locale.getDefault());
| public static final java.text.DateFormat | getDateTimeInstance(int dateStyle, int timeStyle, java.util.Locale locale)Returns a {@code DateFormat} instance for formatting and parsing dates
and time values in the specified styles for the specified locale.
checkTimeStyle(timeStyle);
checkDateStyle(dateStyle);
ResourceBundle bundle = getBundle(locale);
String pattern = bundle.getString("Date_" + getStyleName(dateStyle)) //$NON-NLS-1$
+ " " + bundle.getString("Time_" + getStyleName(timeStyle)); //$NON-NLS-1$ //$NON-NLS-2$
return new SimpleDateFormat(pattern, locale);
| public static final java.text.DateFormat | getInstance()Returns a {@code DateFormat} instance for formatting and parsing dates
and times in the SHORT style for the default locale.
return getDateTimeInstance(SHORT, SHORT);
| public java.text.NumberFormat | getNumberFormat()Returns the {@code NumberFormat} used by this {@code DateFormat}.
return numberFormat;
| static java.lang.String | getStyleName(int style)
String styleName;
switch (style) {
case SHORT:
styleName = "SHORT"; //$NON-NLS-1$
break;
case MEDIUM:
styleName = "MEDIUM"; //$NON-NLS-1$
break;
case LONG:
styleName = "LONG"; //$NON-NLS-1$
break;
case FULL:
styleName = "FULL"; //$NON-NLS-1$
break;
default:
styleName = ""; //$NON-NLS-1$
}
return styleName;
| public static final java.text.DateFormat | getTimeInstance()Returns a {@code DateFormat} instance for formatting and parsing time
values in the DEFAULT style for the default locale.
return getTimeInstance(DEFAULT);
| public static final java.text.DateFormat | getTimeInstance(int style)Returns a {@code DateFormat} instance for formatting and parsing time
values in the specified style for the default locale.
checkTimeStyle(style);
return getTimeInstance(style, Locale.getDefault());
| public static final java.text.DateFormat | getTimeInstance(int style, java.util.Locale locale)Returns a {@code DateFormat} instance for formatting and parsing time
values in the specified style for the specified locale.
checkTimeStyle(style);
ResourceBundle bundle = getBundle(locale);
String pattern = bundle.getString("Time_" + getStyleName(style)); //$NON-NLS-1$
return new SimpleDateFormat(pattern, locale);
| public java.util.TimeZone | getTimeZone()Returns the time zone of this date format's calendar.
return calendar.getTimeZone();
| public int | hashCode()
return calendar.getFirstDayOfWeek()
+ calendar.getMinimalDaysInFirstWeek()
+ calendar.getTimeZone().hashCode()
+ (calendar.isLenient() ? 1231 : 1237)
+ numberFormat.hashCode();
| public boolean | isLenient()Indicates whether the calendar used by this date format is lenient.
return calendar.isLenient();
| public java.util.Date | parse(java.lang.String string)Parses a date from the specified string using the rules of this date
format.
ParsePosition position = new ParsePosition(0);
Date date = parse(string, position);
if (position.getErrorIndex() != -1 || position.getIndex() == 0) {
// text.19=Unparseable date: {0}
throw new ParseException(
Messages.getString("text.19", string), position.getErrorIndex()); //$NON-NLS-1$
}
return date;
| public abstract java.util.Date | parse(java.lang.String string, java.text.ParsePosition position)Parses a date from the specified string starting at the index specified
by {@code position}. If the string is successfully parsed then the index
of the {@code ParsePosition} is updated to the index following the parsed
text. On error, the index is unchanged and the error index of {@code
ParsePosition} is set to the index where the error occurred.
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 {@code setLenient(false)}.
| public java.lang.Object | parseObject(java.lang.String string, java.text.ParsePosition position)Parses a date from the specified string starting at the index specified
by {@code position}. If the string is successfully parsed then the index
of the {@code ParsePosition} is updated to the index following the parsed
text. On error, the index is unchanged and the error index of
{@code ParsePosition} is set to the index where the error occurred.
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 {@code setLenient(false)}.
return parse(string, position);
| public void | setCalendar(java.util.Calendar cal)Sets the calendar used by this date format.
calendar = cal;
| public void | setLenient(boolean value)Specifies whether or not date/time parsing shall 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.
calendar.setLenient(value);
| public void | setNumberFormat(java.text.NumberFormat format)Sets the {@code NumberFormat} used by this date format.
numberFormat = format;
| public void | setTimeZone(java.util.TimeZone timezone)Sets the time zone of the calendar used by this date format.
calendar.setTimeZone(timezone);
|
|