DateFormat is 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 static factory 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.
For any of the factory methods with the parameter style, an IllegalArgumentException will be thrown if style is not equal to any
of the defined formatting styles. More detail and examples of 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 (Date myDate : dates) { output.println(df.format(myDate) + "; "); }
To format a date for a different Locale, specify it in the
call to getDateInstance().
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
If the specified locale contains "ca" (calendar), "rg" (region override), and/or "tz" (timezone) Unicode extensions, the calendar, the country and/or the time zone for formatting are overridden. If both "ca" and "rg" are specified, the calendar from the "ca" extension supersedes the implicit one from the "rg" extension.
You can use a DateFormat to parse also.
myDate = df.parse(myString);
Use getDateInstance to get the normal date format for that country.
There are other static factory methods available.
Use getTimeInstance to get the time format for that country.
Use getDateTimeInstance to get a 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 the shortest and mainly numeric, such as 12.13.52 or 3:30pm
MEDIUM is longer, such as Jan 12, 1952
LONG is even longer, such as January 12, 1952 or 3:30:32pm
FULL is the longest, such as
Tuesday, April 12, 1952 AD or 3:30:42pm PST.
MEDIUM option,
and full text form is used with LONG and FULL options.
You can also set the time zone on the format if you wish.
If you want even more control over the format or parsing,
(or want to give your users more control),
you can 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.
You can also use forms of the parse and format methods with
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.
API Note
Consider using java. as an
immutable and thread-safe alternative.
Implementation Specification
format(Date, StringBuffer, FieldPosition) and
parse(String, ParsePosition) methods may throw
NullPointerException, if any of their parameter is null.
The subclass may provide its own implementation and specification about
NullPointerException.setCalendar(Calendar), setNumberFormat(NumberFormat) and setTimeZone(TimeZone) methods
do not throw NullPointerException when their parameter is
null, but any subsequent operations on the same instance may throw
NullPointerException.getCalendar(), getNumberFormat() and
getTimeZone() methods may return null, if the respective
values of this instance is set to null through the corresponding
setter methods. For Example: getTimeZone() may return null,
if the TimeZone value of this instance is set as
setTimeZone(null).Format, NumberFormat, SimpleDateFormat, java.util.Calendar , java.util.GregorianCalendar , java.util.TimeZone , java.time.format.DateTimeFormatter
| Modifier and Type | Class and Description |
|---|---|
| public static class | DateFormat.
Defines constants that are used as attribute keys in the
|
| pack-priv static enum | DateFormat.
A DateFormat style. |
| Modifier and Type | Field and Description |
|---|---|
| public static final int | AM_PM_FIELD
Useful constant for AM_PM field alignment. |
| protected Calendar | |
| public static final int | DATE_FIELD
Useful constant for DATE field alignment. |
| public static final int | DAY_OF_WEEK_FIELD
Useful constant for DAY_OF_WEEK field alignment. |
| public static final int | DAY_OF_WEEK_IN_MONTH_FIELD
Useful constant for DAY_OF_WEEK_IN_MONTH field alignment. |
| public static final int | DAY_OF_YEAR_FIELD
Useful constant for DAY_OF_YEAR field alignment. |
| public static final int | DEFAULT
Constant for default style pattern. |
| public static final int | ERA_FIELD
Useful constant for ERA field alignment. |
| public static final int | FULL
Constant for full style pattern. |
| public static final int | HOUR0_FIELD
Useful constant for zero-based HOUR field alignment. |
| public static final int | HOUR1_FIELD
Useful constant for one-based HOUR field alignment. |
| public static final int | HOUR_OF_DAY0_FIELD
Useful constant for zero-based HOUR_OF_DAY field alignment. |
| public static final int | HOUR_OF_DAY1_FIELD
Useful constant for one-based HOUR_OF_DAY field alignment. |
| public static final int | LONG
Constant for long style pattern. |
| public static final int | MEDIUM
Constant for medium style pattern. |
| public static final int | MILLISECOND_FIELD
Useful constant for MILLISECOND field alignment. |
| public static final int | MINUTE_FIELD
Useful constant for MINUTE field alignment. |
| public static final int | MONTH_FIELD
Useful constant for MONTH field alignment. |
| protected NumberFormat | numberFormat
The number formatter that |
| public static final int | SECOND_FIELD
Useful constant for SECOND field alignment. |
| private static final long | |
| public static final int | SHORT
Constant for short style pattern. |
| public static final int | TIMEZONE_FIELD
Useful constant for TIMEZONE field alignment. |
| public static final int | WEEK_OF_MONTH_FIELD
Useful constant for WEEK_OF_MONTH field alignment. |
| public static final int | WEEK_OF_YEAR_FIELD
Useful constant for WEEK_OF_YEAR field alignment. |
| public static final int | YEAR_FIELD
Useful constant for YEAR field alignment. |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public Object | |
| public boolean | Returns: true if the specified object is equal to this DateFormatobject to be compared for equality obj)Overrides java. Compares the specified object with this |
| public final StringBuffer | Returns: the string buffer passed in astoAppendTo,
with formatted text appended.The string buffer for the returning date-time string. toAppendTo, FieldPosition keeps track on the position of the field within
the returned string. For example, given a date-time text
fieldPosition)"1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition
is DateFormat#YEAR_FIELD, the begin index and end index of
fieldPosition will be set to 0 and 4, respectively.
Notice that if the same date-time field appears more than once in a
pattern, the fieldPosition will be set for the first occurrence
of that date-time field. For instance, formatting a Date to the
date-time string "1 PM PDT (Pacific Daylight Time)" using the
pattern "h a z (zzzz)" and the alignment field
DateFormat#TIMEZONE_FIELD, the begin index and end index of
fieldPosition will be set to 5 and 8, respectively, for the
first occurrence of the timezone pattern character 'z'.Implements abstract java. Formats the given |
| pack-priv final Format. | format(Object obj, Format.
Overrides java.
|
| public abstract StringBuffer | Returns: the string buffer passed in astoAppendTo, with formatted
text appended.a Date to be formatted into a date-time string. date, StringBuffer the string buffer for the returning date-time string. toAppendTo, FieldPosition keeps track on the position of the field within
the returned string. For example, given a date-time text
fieldPosition)"1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition
is DateFormat#YEAR_FIELD, the begin index and end index of
fieldPosition will be set to 0 and 4, respectively.
Notice that if the same date-time field appears more than once in a
pattern, the fieldPosition will be set for the first occurrence
of that date-time field. For instance, formatting a Date to the
date-time string "1 PM PDT (Pacific Daylight Time)" using the
pattern "h a z (zzzz)" and the alignment field
DateFormat#TIMEZONE_FIELD, the begin index and end index of
fieldPosition will be set to 5 and 8, respectively, for the
first occurrence of the timezone pattern character 'z'.Formats a |
| pack-priv Format. | |
| public final String | |
| private static DateFormat | get(int
a value from 0 to 3 indicating the time format,
ignored if flags is 2 timeStyle, int a value from 0 to 3 indicating the time format,
ignored if flags is 1 dateStyle, int either 1 for a time format, 2 for a date format,
or 3 for a date/time format flags, Locale the locale for the format loc)Creates a DateFormat with the given time and/or date style in the given locale. |
| private static DateFormat | |
| public static Locale[] | Returns: An array of locales for which localizedDateFormat instances are available.Returns an array of all locales for which the
|
| public Calendar | Returns: the calendar associated with this date/time formatter.Gets the calendar associated with this date/time formatter. |
| public static final DateFormat | Returns: a date formatter.Gets the date formatter with the default formatting style
for the default |
| public static final DateFormat | Returns: a date formatter.the given formatting style. For example,
SHORT for "M/d/yy" in the US locale. style)Gets the date formatter with the given formatting style
for the default |
| public static final DateFormat | Returns: a date formatter.the given formatting style. For example,
SHORT for "M/d/yy" in the US locale. style, Locale the given locale. aLocale)Gets the date formatter with the given formatting style for the given locale. |
| public static final DateFormat | Returns: a date/time formatter.Gets the date/time formatter with the default formatting style
for the default |
| public static final DateFormat | Returns: a date/time formatter.the given date formatting style. For example,
SHORT for "M/d/yy" in the US locale. dateStyle, int the given time formatting style. For example,
SHORT for "h:mm a" in the US locale. timeStyle)Gets the date/time formatter with the given date and time
formatting styles for the default |
| public static final DateFormat | Returns: a date/time formatter.the given date formatting style. dateStyle, int the given time formatting style. timeStyle, Locale the given locale. aLocale)Gets the date/time formatter with the given formatting styles for the given locale. |
| public static final DateFormat | Returns: a date/time formatterGet a default date/time formatter that uses the SHORT style for both the date and the time. |
| public NumberFormat | Returns: the number formatter which this date/time formatter uses.Gets the number formatter which this date/time formatter uses to format and parse a time. |
| public static final DateFormat | Returns: a time formatter.Gets the time formatter with the default formatting style
for the default |
| public static final DateFormat | Returns: a time formatter.the given formatting style. For example,
SHORT for "h:mm a" in the US locale. style)Gets the time formatter with the given formatting style
for the default |
| public static final DateFormat | Returns: a time formatter.the given formatting style. For example,
SHORT for "h:mm a" in the US locale. style, Locale the given locale. aLocale)Gets the time formatter with the given formatting style for the given locale. |
| public TimeZone | |
| public int | Returns: the hash code for thisDateFormatOverrides java. Returns the hash code for this |
| public boolean | |
| public Date | |
| public abstract Date | Returns: ADate, or null if the input could not be parsedThe date/time string to be parsed source, ParsePosition 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. pos)Parse a date/time string according to the given parse position. |
| public Object | Returns: ADate parsed from the string. In case of
error, returns null.A source, ParsePosition String, part of which should be parsed.A pos)ParsePosition object with index and error
index information as described above.Implements abstract java. Parses text from a string to produce a |
| public void | setCalendar(Calendar
the new newCalendar)Calendar to be used by the date formatSet the calendar to be used by this date format. |
| public void | setLenient(boolean
when lenient)true, parsing is lenientSpecify whether or not date/time parsing is to be lenient. |
| public void | setNumberFormat(NumberFormat
the given new NumberFormat. newNumberFormat)Allows you to set the number formatter. |
| public void | setTimeZone(TimeZone
the given new time zone. zone)Sets the time zone for the calendar of this |