java.lang.Object | ||
↳ | java.text.Format | |
↳ | java.text.DateFormat |
Known Direct Subclasses |
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 SimpleDateFormat
, allows for formatting
(i.e., date -> text), parsing (text -> date), and normalization. The date is
represented as a 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.
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
getDateInstance
:
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
DateFormat
can also be used to parse strings:
myDate = df.parse(myString);
Use getDateInstance
to get the normal date format for a country.
Other static factory methods are available: Use getTimeInstance
to
get the time format for a country. Use 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:
If needed, the time zone can be set on the format. For even greater control
over the formatting or parsing, try casting the DateFormat
you get
from the factory methods to a 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
ParsePosition
and FieldPosition
to allow you to
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.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
DateFormat.Field | The instances of this inner class are used as attribute keys and values
in AttributedCharacterIterator that the
formatToCharacterIterator(Object) method returns. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | AM_PM_FIELD | FieldPosition selector for 'a' field alignment, corresponds to the
AM_PM field. |
|||||||||
int | DATE_FIELD | The FieldPosition selector for 'd' field alignment, corresponds
to the DATE field. |
|||||||||
int | DAY_OF_WEEK_FIELD | FieldPosition selector for 'E' field alignment, corresponds to the
DAY_OF_WEEK field. |
|||||||||
int | DAY_OF_WEEK_IN_MONTH_FIELD | FieldPosition selector for 'F' field alignment, corresponds to the
DAY_OF_WEEK_IN_MONTH field. |
|||||||||
int | DAY_OF_YEAR_FIELD | FieldPosition selector for 'D' field alignment, corresponds to the
DAY_OF_YEAR field. |
|||||||||
int | DEFAULT | The format style constant defining the default format style. | |||||||||
int | ERA_FIELD | The FieldPosition selector for 'G' field alignment, corresponds
to the ERA field. |
|||||||||
int | FULL | The format style constant defining the full style. | |||||||||
int | HOUR0_FIELD | The FieldPosition selector for 'K' field alignment, corresponding to the
HOUR field. |
|||||||||
int | HOUR1_FIELD | FieldPosition selector for 'h' field alignment, corresponding to the
HOUR field. |
|||||||||
int | HOUR_OF_DAY0_FIELD | The FieldPosition selector for 'H' field alignment, corresponds
to the HOUR_OF_DAY field. |
|||||||||
int | HOUR_OF_DAY1_FIELD | The FieldPosition selector for 'k' field alignment, corresponds
to the HOUR_OF_DAY field. |
|||||||||
int | LONG | The format style constant defining the long style. | |||||||||
int | MEDIUM | The format style constant defining the medium style. | |||||||||
int | MILLISECOND_FIELD | FieldPosition selector for 'S' field alignment, corresponds to the
MILLISECOND field. |
|||||||||
int | MINUTE_FIELD | FieldPosition selector for 'm' field alignment, corresponds to the
MINUTE field. |
|||||||||
int | MONTH_FIELD | The FieldPosition selector for 'M' field alignment, corresponds
to the MONTH field. |
|||||||||
int | SECOND_FIELD | FieldPosition selector for 's' field alignment, corresponds to the
SECOND field. |
|||||||||
int | SHORT | The format style constant defining the short style. | |||||||||
int | TIMEZONE_FIELD | The FieldPosition selector for 'z' field alignment, corresponds
to the ZONE_OFFSET and DST_OFFSET
fields. |
|||||||||
int | WEEK_OF_MONTH_FIELD | FieldPosition selector for 'W' field alignment, corresponds to the
WEEK_OF_MONTH field. |
|||||||||
int | WEEK_OF_YEAR_FIELD | FieldPosition selector for 'w' field alignment, corresponds to the
WEEK_OF_YEAR field. |
|||||||||
int | YEAR_FIELD | The FieldPosition selector for 'y' field alignment, corresponds
to the YEAR field. |
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
calendar | The calendar that this DateFormat uses to format a number
representing a date. |
||||||||||
numberFormat | The number format used to format a number. |
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new instance of
DateFormat . |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns a new instance of
DateFormat with the same properties. | |||||||||||
Compares this date format with the specified object and indicates if they
are equal.
| |||||||||||
Formats the specified date as a string using the pattern of this date
format and appends the string to the specified string buffer.
| |||||||||||
Formats the specified object as a string using the pattern of this date
format and appends the string to the specified string buffer.
| |||||||||||
Formats the specified date using the rules of this date format.
| |||||||||||
Returns an array of locales for which custom
DateFormat instances
are available. | |||||||||||
Returns the calendar used by this
DateFormat . | |||||||||||
Returns a
DateFormat instance for formatting and parsing dates in
the specified style for the user's default locale. | |||||||||||
Returns a
DateFormat instance for formatting and parsing dates in
the specified style for the specified locale. | |||||||||||
Returns a
DateFormat instance for formatting and parsing dates in
the DEFAULT style for the default locale. | |||||||||||
Returns a
DateFormat instance for formatting and parsing dates
and time values in the specified styles for the specified locale. | |||||||||||
Returns a
DateFormat instance for formatting and parsing of both
dates and time values in the manner appropriate for the user's default locale. | |||||||||||
Returns a
DateFormat instance for formatting and parsing dates
and time values in the DEFAULT style for the default locale. | |||||||||||
Returns a
DateFormat instance for formatting and parsing dates
and times in the SHORT style for the default locale. | |||||||||||
Returns the
NumberFormat used by this DateFormat . | |||||||||||
Returns a
DateFormat instance for formatting and parsing time
values in the specified style for the user's default locale. | |||||||||||
Returns a
DateFormat instance for formatting and parsing time
values in the DEFAULT style for the default locale. | |||||||||||
Returns a
DateFormat instance for formatting and parsing time
values in the specified style for the specified locale. | |||||||||||
Returns the time zone of this date format's calendar.
| |||||||||||
Returns an integer hash code for this object.
| |||||||||||
Indicates whether the calendar used by this date format is lenient.
| |||||||||||
Parses a date from the specified string using the rules of this date
format.
| |||||||||||
Parses a date from the specified string starting at the index specified
by
position . | |||||||||||
Parses a date from the specified string starting at the index specified
by
position . | |||||||||||
Sets the calendar used by this date format.
| |||||||||||
Specifies whether or not date/time parsing shall be lenient.
| |||||||||||
Sets the
NumberFormat used by this date format. | |||||||||||
Sets the time zone of the calendar used by this date format.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.text.Format
| |||||||||||
From class
java.lang.Object
|
FieldPosition selector for 'a' field alignment, corresponds to the
AM_PM
field.
The FieldPosition
selector for 'd' field alignment, corresponds
to the DATE
field.
FieldPosition selector for 'E' field alignment, corresponds to the
DAY_OF_WEEK
field.
FieldPosition selector for 'F' field alignment, corresponds to the
DAY_OF_WEEK_IN_MONTH
field.
FieldPosition selector for 'D' field alignment, corresponds to the
DAY_OF_YEAR
field.
The format style constant defining the default format style. The default is MEDIUM.
The FieldPosition
selector for 'G' field alignment, corresponds
to the ERA
field.
The format style constant defining the full style.
The FieldPosition
selector for 'K' field alignment, corresponding to the
HOUR
field.
FieldPosition selector for 'h' field alignment, corresponding to the
HOUR
field.
The FieldPosition
selector for 'H' field alignment, corresponds
to the HOUR_OF_DAY
field. HOUR_OF_DAY0_FIELD
is
used for the zero-based 24-hour clock. For example, 23:59 + 01:00 results
in 00:59.
The FieldPosition
selector for 'k' field alignment, corresponds
to the HOUR_OF_DAY
field. HOUR_OF_DAY1_FIELD
is
used for the one-based 24-hour clock. For example, 23:59 + 01:00 results
in 24:59.
The format style constant defining the long style.
The format style constant defining the medium style.
FieldPosition selector for 'S' field alignment, corresponds to the
MILLISECOND
field.
FieldPosition selector for 'm' field alignment, corresponds to the
MINUTE
field.
The FieldPosition
selector for 'M' field alignment, corresponds
to the MONTH
field.
FieldPosition selector for 's' field alignment, corresponds to the
SECOND
field.
The format style constant defining the short style.
The FieldPosition
selector for 'z' field alignment, corresponds
to the ZONE_OFFSET
and DST_OFFSET
fields.
FieldPosition selector for 'W' field alignment, corresponds to the
WEEK_OF_MONTH
field.
FieldPosition selector for 'w' field alignment, corresponds to the
WEEK_OF_YEAR
field.
The FieldPosition
selector for 'y' field alignment, corresponds
to the YEAR
field.
The calendar that this DateFormat
uses to format a number
representing a date.
Returns a new instance of DateFormat
with the same properties.
Compares this date format with the specified object and indicates if they are equal.
object | the object to compare with this date format. |
---|
true
if object
is a DateFormat
object and
it has the same properties as this date format; false
otherwise.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 field
member of field
contains a value specifying
a format field, then its beginIndex
and endIndex
members
will be updated with the position of the first occurrence of this field
in the formatted text.
date | the date to format. |
---|---|
buffer | the target string buffer to append the formatted date/time to. |
field | on input: an optional alignment field; on output: the offsets of the alignment field in the formatted text. |
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 field
member of field
contains a value specifying
a format field, then its beginIndex
and endIndex
members
will be updated with the position of the first occurrence of this field
in the formatted text.
object | the source object to format, must be a Date or a
Number . If object is a number then a date is
constructed using the longValue() of the number. |
---|---|
buffer | the target string buffer to append the formatted date/time to. |
field | on input: an optional alignment field; on output: the offsets of the alignment field in the formatted text. |
IllegalArgumentException | if object is neither a Date nor a
Number instance.
|
---|
Formats the specified date using the rules of this date format.
date | the date to format. |
---|
Returns an array of locales for which custom DateFormat
instances
are available.
Note that Android does not support user-supplied locale service providers.
Returns the calendar used by this DateFormat
.
Returns a DateFormat
instance for formatting and parsing dates in
the specified style for the user's default locale.
See "Be wary of the default locale".
style | one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. |
---|
DateFormat
instance for style
and the default
locale.IllegalArgumentException | if style is not one of SHORT, MEDIUM, LONG, FULL, or
DEFAULT.
|
---|
Returns a DateFormat
instance for formatting and parsing dates in
the specified style for the specified locale.
style | one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. |
---|---|
locale | the locale. |
DateFormat
instance for style
and
locale
.
IllegalArgumentException | if style is not one of SHORT, MEDIUM, LONG, FULL, or
DEFAULT. |
---|
Returns a DateFormat
instance for formatting and parsing dates in
the DEFAULT style for the default locale.
DateFormat
instance for the default style and locale.
Returns a DateFormat
instance for formatting and parsing dates
and time values in the specified styles for the specified locale.
dateStyle | one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. |
---|---|
timeStyle | one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. |
locale | the locale. |
DateFormat
instance for dateStyle
,
timeStyle
and locale
.IllegalArgumentException | if dateStyle or timeStyle is not one of
SHORT, MEDIUM, LONG, FULL, or DEFAULT.
|
---|
Returns a DateFormat
instance for formatting and parsing of both
dates and time values in the manner appropriate for the user's default locale.
See "Be wary of the default locale".
dateStyle | one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. |
---|---|
timeStyle | one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. |
DateFormat
instance for dateStyle
,
timeStyle
and the default locale.IllegalArgumentException | if dateStyle or timeStyle is not one of
SHORT, MEDIUM, LONG, FULL, or DEFAULT.
|
---|
Returns a DateFormat
instance for formatting and parsing dates
and time values in the DEFAULT style for the default locale.
DateFormat
instance for the default style and locale.
Returns a DateFormat
instance for formatting and parsing dates
and times in the SHORT style for the default locale.
DateFormat
instance for the SHORT style and default
locale.
Returns the NumberFormat
used by this DateFormat
.
NumberFormat
used by this date format.
Returns a DateFormat
instance for formatting and parsing time
values in the specified style for the user's default locale.
See "Be wary of the default locale".
style | one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. |
---|
DateFormat
instance for style
and the default
locale.IllegalArgumentException | if style is not one of SHORT, MEDIUM, LONG, FULL, or
DEFAULT.
|
---|
Returns a DateFormat
instance for formatting and parsing time
values in the DEFAULT style for the default locale.
DateFormat
instance for the default style and locale.
Returns a DateFormat
instance for formatting and parsing time
values in the specified style for the specified locale.
style | one of SHORT, MEDIUM, LONG, FULL, or DEFAULT. |
---|---|
locale | the locale. |
DateFormat
instance for style
and
locale
.
IllegalArgumentException | if style is not one of SHORT, MEDIUM, LONG, FULL, or
DEFAULT. |
---|
Returns the time zone of this date format's calendar.
Returns an integer hash code for this object. By contract, any two
objects for which equals(Object)
returns true
must return
the same hash code value. This means that subclasses of Object
usually override both methods or neither method.
Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCode
method
if you intend implementing your own hashCode
method.
Indicates whether the calendar used by this date format is lenient.
true
if the calendar is lenient; false
otherwise.
Parses a date from the specified string using the rules of this date format.
string | the string to parse. |
---|
Date
resulting from the parsing.ParseException | if an error occurs during parsing. |
---|
Parses a date from the specified string starting at the index specified
by position
. If the string is successfully parsed then the index
of the ParsePosition
is updated to the index following the parsed
text. On error, the index is unchanged and the error index of 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 setLenient(false)
.
string | the string to parse. |
---|---|
position | input/output parameter, specifies the start index in string from where to start parsing. If parsing is successful,
it is updated with the index following the parsed text; on
error, the index is unchanged and the error index is set to
the index where the error occurred. |
null
if there is an
error.
Parses a date from the specified string starting at the index specified
by position
. If the string is successfully parsed then the index
of the ParsePosition
is updated to the index following the parsed
text. On error, the index is unchanged and the error index of
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 setLenient(false)
.
string | the string to parse. |
---|---|
position | input/output parameter, specifies the start index in
string from where to start parsing. If parsing is
successful, it is updated with the index following the parsed
text; on error, the index is unchanged and the error index
is set to the index where the error occurred. |
null
if there is
an error.
Sets the calendar used by this date format.
cal | the new calendar. |
---|
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.
value | true to set the calendar to be lenient, false
otherwise.
|
---|
Sets the NumberFormat
used by this date format.
format | the new number format. |
---|
Sets the time zone of the calendar used by this date format.
timezone | the new time zone. |
---|