APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.text

public abstract Class DateFormat

extends Format
Class Inheritance
Known Direct Subclasses
java.text.SimpleDateFormat
Imports
java.io.InvalidObjectException, java.text.spi.DateFormatProvider, java.util.Calendar, .Date, .GregorianCalendar, .HashMap, .Locale, .Map, .MissingResourceException, .ResourceBundle, .TimeZone, java.util.spi.LocaleServiceProvider, sun.util.locale.provider.LocaleProviderAdapter, .LocaleServiceProviderPool

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);
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) + "; "); }
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);
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);
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:

For those fields with text, typically abbreviated text form is used with 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

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.

API Note

Consider using java.time.format.DateTimeFormatter as an immutable and thread-safe alternative.

Implementation Specification

Author
Mark Davis, Chen-Lieh Huang, Alan Liu
Since
1.1
See Also
Format, NumberFormat, SimpleDateFormat, java.util.Calendar, java.util.GregorianCalendar, java.util.TimeZone, java.time.format.DateTimeFormatter

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
DateFormat.Field

Defines constants that are used as attribute keys in the AttributedCharacterIterator returned from DateFormat.formatToCharacterIterator and as field identifiers in FieldPosition.

pack-priv static enum
DateFormat.Style

A DateFormat style.

Field Summary

Modifier and TypeField and Description
public static final int
AM_PM_FIELD

Useful constant for AM_PM field alignment.

protected Calendar
calendar

The Calendar instance used for calculating the date-time fields and the instant of time.

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 DateFormat uses to format numbers in dates and times.

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.

Constructor Summary

AccessConstructor and Description
protected
DateFormat()

Create a new date format.

Method Summary

Modifier and TypeMethod and Description
public Object
clone()

Overrides java.text.Format.clone.

Overrides Cloneable

public boolean

Returns:

true if the specified object is equal to this DateFormat
equals
(Object
object to be compared for equality
obj
)

Overrides java.lang.Object.equals.

Compares the specified object with this DateFormat for equality.

public final StringBuffer

Returns:

the string buffer passed in as toAppendTo, with formatted text appended.
format
(Object
Must be a Date or a Number representing a millisecond offset from the Epoch.
obj
,
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 "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'.
fieldPosition
)

Implements abstract java.text.Format.format.

Formats the given Object into a date-time string.

pack-priv final Format.StringBuf
format(Object obj, Format.StringBuf toAppendTo, FieldPosition fieldPosition)

Overrides java.text.Format.format.

public abstract StringBuffer

Returns:

the string buffer passed in as toAppendTo, with formatted text appended.
format
(Date
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 "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'.
fieldPosition
)

Formats a Date into a date-time string.

pack-priv Format.StringBuf
format(Date date, Format.StringBuf toAppendTo, FieldPosition fieldPosition)

public final String

Returns:

the formatted date-time string.
format
(Date
the time value to be formatted into a date-time string.
date
)

Formats a Date into a date-time 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
get(LocaleProviderAdapter adapter, int timeStyle, int dateStyle, Locale loc)

public static Locale[]

Returns:

An array of locales for which localized DateFormat instances are available.
getAvailableLocales
()

Returns an array of all locales for which the get*Instance methods of this class can return localized instances.

public Calendar

Returns:

the calendar associated with this date/time formatter.
getCalendar
()

Gets the calendar associated with this date/time formatter.

public static final DateFormat

Returns:

a date formatter.
getDateInstance
()

Gets the date formatter with the default formatting style for the default FORMAT locale.

public static final DateFormat

Returns:

a date formatter.
getDateInstance
(int
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 FORMAT locale.

public static final DateFormat

Returns:

a date formatter.
getDateInstance
(int
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.
getDateTimeInstance
()

Gets the date/time formatter with the default formatting style for the default FORMAT locale.

public static final DateFormat

Returns:

a date/time formatter.
getDateTimeInstance
(int
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 FORMAT locale.

public static final DateFormat

Returns:

a date/time formatter.
getDateTimeInstance
(int
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 formatter
getInstance
()

Get 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.
getNumberFormat
()

Gets the number formatter which this date/time formatter uses to format and parse a time.

public static final DateFormat

Returns:

a time formatter.
getTimeInstance
()

Gets the time formatter with the default formatting style for the default FORMAT locale.

public static final DateFormat

Returns:

a time formatter.
getTimeInstance
(int
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 FORMAT locale.

public static final DateFormat

Returns:

a time formatter.
getTimeInstance
(int
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

Returns:

the time zone associated with the calendar of DateFormat.
getTimeZone
()

Gets the time zone.

public int

Returns:

the hash code for this DateFormat
hashCode
()

Overrides java.lang.Object.hashCode.

Returns the hash code for this DateFormat.

public boolean

Returns:

true if the calendar is lenient; false otherwise.
isLenient
()

Tell whether date/time parsing is to be lenient.

public Date

Returns:

A Date parsed from the string.
parse
(String
A String whose beginning should be parsed.
source
)

Parses text from the beginning of the given string to produce a date.

public abstract Date

Returns:

A Date, or null if the input could not be parsed
parse
(String
The 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:

A Date parsed from the string. In case of error, returns null.
parseObject
(String
A String, part of which should be parsed.
source
,
ParsePosition
A ParsePosition object with index and error index information as described above.
pos
)

Implements abstract java.text.Format.parseObject.

Parses text from a string to produce a Date.

public void
setCalendar(Calendar
the new Calendar to be used by the date format
newCalendar
)

Set the calendar to be used by this date format.

public void
setLenient(boolean
when true, parsing is lenient
lenient
)

Specify 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 DateFormat object.

Inherited from java.text.Format:
createAttributedCharacterIteratorcreateAttributedCharacterIteratorcreateAttributedCharacterIteratorcreateAttributedCharacterIteratorformatformatToCharacterIteratorparseObject