Class DateFormat
- All Implemented Interfaces:
Serializable,Cloneable
- Direct Known Subclasses:
SimpleDateFormat
public abstract class DateFormat extends Format
This class provides factories for obtaining instances configured for a specific locale.
The most common subclass is SimpleDateFormat.
Sample Code
This code:
DateFormat[] formats = new DateFormat[] {
DateFormat.getDateInstance(),
DateFormat.getDateTimeInstance(),
DateFormat.getTimeInstance(),
};
for (DateFormat df : formats) {
System.out.println(df.format(new Date(0)));
df.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(df.format(new Date(0)));
}
Produces this output when run on an en_US device in the America/Los_Angeles time zone:
Dec 31, 1969 Jan 1, 1970 Dec 31, 1969 4:00:00 PM Jan 1, 1970 12:00:00 AM 4:00:00 PM 12:00:00 AMAnd will produce similarly appropriate localized human-readable output on any user's system. Notice how the same point in time when formatted can appear to be a different time when rendered for a different time zone. This is one reason why formatting should be left until the data will only be presented to a human. Machines should interchange "Unix time" integers.
- See Also:
- Serialized Form
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDateFormat.FieldThe instances of this inner class are used as attribute keys and values inAttributedCharacterIteratorthat theSimpleDateFormat.formatToCharacterIterator(Object)method returns. -
Field Summary
Fields Modifier and Type Field Description static intAM_PM_FIELDFieldPosition selector for 'a' field alignment, corresponds to theCalendar.AM_PMfield.protected CalendarcalendarThe calendar that thisDateFormatuses to format a number representing a date.static intDATE_FIELDTheFieldPositionselector for 'd' field alignment, corresponds to theCalendar.DATEfield.static intDAY_OF_WEEK_FIELDFieldPosition selector for 'E' field alignment, corresponds to theCalendar.DAY_OF_WEEKfield.static intDAY_OF_WEEK_IN_MONTH_FIELDFieldPosition selector for 'F' field alignment, corresponds to theCalendar.DAY_OF_WEEK_IN_MONTHfield.static intDAY_OF_YEAR_FIELDFieldPosition selector for 'D' field alignment, corresponds to theCalendar.DAY_OF_YEARfield.static intDEFAULTThe format style constant defining the default format style.static intERA_FIELDTheFieldPositionselector for 'G' field alignment, corresponds to theCalendar.ERAfield.static intFULLThe format style constant defining the full style.static intHOUR_OF_DAY0_FIELDTheFieldPositionselector for 'H' field alignment, corresponds to theCalendar.HOUR_OF_DAYfield.static intHOUR_OF_DAY1_FIELDTheFieldPositionselector for 'k' field alignment, corresponds to theCalendar.HOUR_OF_DAYfield.static intHOUR0_FIELDTheFieldPositionselector for 'K' field alignment, corresponding to theCalendar.HOURfield.static intHOUR1_FIELDFieldPosition selector for 'h' field alignment, corresponding to theCalendar.HOURfield.static intLONGThe format style constant defining the long style.static intMEDIUMThe format style constant defining the medium style.static intMILLISECOND_FIELDFieldPosition selector for 'S' field alignment, corresponds to theCalendar.MILLISECONDfield.static intMINUTE_FIELDFieldPosition selector for 'm' field alignment, corresponds to theCalendar.MINUTEfield.static intMONTH_FIELDTheFieldPositionselector for 'M' field alignment, corresponds to theCalendar.MONTHfield.protected NumberFormatnumberFormatThe number format used to format a number.static intSECOND_FIELDFieldPosition selector for 's' field alignment, corresponds to theCalendar.SECONDfield.static intSHORTThe format style constant defining the short style.static intTIMEZONE_FIELDTheFieldPositionselector for 'z' field alignment, corresponds to theCalendar.ZONE_OFFSETandCalendar.DST_OFFSETfields.static intWEEK_OF_MONTH_FIELDFieldPosition selector for 'W' field alignment, corresponds to theCalendar.WEEK_OF_MONTHfield.static intWEEK_OF_YEAR_FIELDFieldPosition selector for 'w' field alignment, corresponds to theCalendar.WEEK_OF_YEARfield.static intYEAR_FIELDTheFieldPositionselector for 'y' field alignment, corresponds to theCalendar.YEARfield. -
Constructor Summary
Constructors Modifier Constructor Description protectedDateFormat()Constructs a new instance ofDateFormat. -
Method Summary
Modifier and Type Method Description Objectclone()Returns a new instance ofDateFormatwith the same properties.booleanequals(Object object)Compares this date format with the specified object and indicates if they are equal.StringBufferformat(Object object, StringBuffer buffer, 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.Stringformat(Date date)Formats the specified date using the rules of this date format.abstract StringBufferformat(Date date, StringBuffer buffer, 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.static Locale[]getAvailableLocales()Returns an array of locales for which customDateFormatinstances are available.CalendargetCalendar()Returns the calendar used by thisDateFormat.static DateFormatgetDateInstance()Returns aDateFormatinstance for formatting and parsing dates in the DEFAULT style for the default locale.static DateFormatgetDateInstance(int style)Returns aDateFormatinstance for formatting and parsing dates in the specified style for the user's default locale.static DateFormatgetDateInstance(int style, Locale locale)Returns aDateFormatinstance for formatting and parsing dates in the specified style for the specified locale.static DateFormatgetDateTimeInstance()Returns aDateFormatinstance for formatting and parsing dates and time values in the DEFAULT style for the default locale.static DateFormatgetDateTimeInstance(int dateStyle, int timeStyle)Returns aDateFormatinstance for formatting and parsing of both dates and time values in the manner appropriate for the user's default locale.static DateFormatgetDateTimeInstance(int dateStyle, int timeStyle, Locale locale)Returns aDateFormatinstance for formatting and parsing dates and time values in the specified styles for the specified locale.static DateFormatgetInstance()Returns aDateFormatinstance for formatting and parsing dates and times in the SHORT style for the default locale.NumberFormatgetNumberFormat()Returns theNumberFormatused by thisDateFormat.static DateFormatgetTimeInstance()Returns aDateFormatinstance for formatting and parsing time values in the DEFAULT style for the default locale.static DateFormatgetTimeInstance(int style)Returns aDateFormatinstance for formatting and parsing time values in the specified style for the user's default locale.static DateFormatgetTimeInstance(int style, Locale locale)Returns aDateFormatinstance for formatting and parsing time values in the specified style for the specified locale.TimeZonegetTimeZone()Returns the time zone of this date format's calendar.inthashCode()Returns an integer hash code for this object.booleanisLenient()Indicates whether the calendar used by this date format is lenient.Dateparse(String string)Parses a date from the specified string using the rules of this date format.abstract Dateparse(String string, ParsePosition position)Parses a date from the specified string starting at the index specified byposition.ObjectparseObject(String string, ParsePosition position)Parses a date from the specified string starting at the index specified byposition.voidsetCalendar(Calendar cal)Sets the calendar used by this date format.voidsetLenient(boolean value)Specifies whether or not date/time parsing shall be lenient.voidsetNumberFormat(NumberFormat format)Sets theNumberFormatused by this date format.voidsetTimeZone(TimeZone timezone)Sets the time zone of the calendar used by this date format.Methods inherited from class java.text.Format
format, formatToCharacterIterator, parseObject
-
Field Details
-
calendar
The calendar that thisDateFormatuses to format a number representing a date. -
numberFormat
The number format used to format a number. -
DEFAULT
public static final int DEFAULTThe format style constant defining the default format style. The default is MEDIUM.- See Also:
- Constant Field Values
-
FULL
public static final int FULLThe format style constant defining the full style.- See Also:
- Constant Field Values
-
LONG
public static final int LONGThe format style constant defining the long style.- See Also:
- Constant Field Values
-
MEDIUM
public static final int MEDIUMThe format style constant defining the medium style.- See Also:
- Constant Field Values
-
SHORT
public static final int SHORTThe format style constant defining the short style.- See Also:
- Constant Field Values
-
ERA_FIELD
public static final int ERA_FIELDTheFieldPositionselector for 'G' field alignment, corresponds to theCalendar.ERAfield.- See Also:
- Constant Field Values
-
YEAR_FIELD
public static final int YEAR_FIELDTheFieldPositionselector for 'y' field alignment, corresponds to theCalendar.YEARfield.- See Also:
- Constant Field Values
-
MONTH_FIELD
public static final int MONTH_FIELDTheFieldPositionselector for 'M' field alignment, corresponds to theCalendar.MONTHfield.- See Also:
- Constant Field Values
-
DATE_FIELD
public static final int DATE_FIELDTheFieldPositionselector for 'd' field alignment, corresponds to theCalendar.DATEfield.- See Also:
- Constant Field Values
-
HOUR_OF_DAY1_FIELD
public static final int HOUR_OF_DAY1_FIELDTheFieldPositionselector for 'k' field alignment, corresponds to theCalendar.HOUR_OF_DAYfield.HOUR_OF_DAY1_FIELDis used for the one-based 24-hour clock. For example, 23:59 + 01:00 results in 24:59.- See Also:
- Constant Field Values
-
HOUR_OF_DAY0_FIELD
public static final int HOUR_OF_DAY0_FIELDTheFieldPositionselector for 'H' field alignment, corresponds to theCalendar.HOUR_OF_DAYfield.HOUR_OF_DAY0_FIELDis used for the zero-based 24-hour clock. For example, 23:59 + 01:00 results in 00:59.- See Also:
- Constant Field Values
-
MINUTE_FIELD
public static final int MINUTE_FIELDFieldPosition selector for 'm' field alignment, corresponds to theCalendar.MINUTEfield.- See Also:
- Constant Field Values
-
SECOND_FIELD
public static final int SECOND_FIELDFieldPosition selector for 's' field alignment, corresponds to theCalendar.SECONDfield.- See Also:
- Constant Field Values
-
MILLISECOND_FIELD
public static final int MILLISECOND_FIELDFieldPosition selector for 'S' field alignment, corresponds to theCalendar.MILLISECONDfield.- See Also:
- Constant Field Values
-
DAY_OF_WEEK_FIELD
public static final int DAY_OF_WEEK_FIELDFieldPosition selector for 'E' field alignment, corresponds to theCalendar.DAY_OF_WEEKfield.- See Also:
- Constant Field Values
-
DAY_OF_YEAR_FIELD
public static final int DAY_OF_YEAR_FIELDFieldPosition selector for 'D' field alignment, corresponds to theCalendar.DAY_OF_YEARfield.- See Also:
- Constant Field Values
-
DAY_OF_WEEK_IN_MONTH_FIELD
public static final int DAY_OF_WEEK_IN_MONTH_FIELDFieldPosition selector for 'F' field alignment, corresponds to theCalendar.DAY_OF_WEEK_IN_MONTHfield.- See Also:
- Constant Field Values
-
WEEK_OF_YEAR_FIELD
public static final int WEEK_OF_YEAR_FIELDFieldPosition selector for 'w' field alignment, corresponds to theCalendar.WEEK_OF_YEARfield.- See Also:
- Constant Field Values
-
WEEK_OF_MONTH_FIELD
public static final int WEEK_OF_MONTH_FIELDFieldPosition selector for 'W' field alignment, corresponds to theCalendar.WEEK_OF_MONTHfield.- See Also:
- Constant Field Values
-
AM_PM_FIELD
public static final int AM_PM_FIELDFieldPosition selector for 'a' field alignment, corresponds to theCalendar.AM_PMfield.- See Also:
- Constant Field Values
-
HOUR1_FIELD
public static final int HOUR1_FIELDFieldPosition selector for 'h' field alignment, corresponding to theCalendar.HOURfield.- See Also:
- Constant Field Values
-
HOUR0_FIELD
public static final int HOUR0_FIELDTheFieldPositionselector for 'K' field alignment, corresponding to theCalendar.HOURfield.- See Also:
- Constant Field Values
-
TIMEZONE_FIELD
public static final int TIMEZONE_FIELDTheFieldPositionselector for 'z' field alignment, corresponds to theCalendar.ZONE_OFFSETandCalendar.DST_OFFSETfields.- See Also:
- Constant Field Values
-
-
Constructor Details
-
DateFormat
protected DateFormat()Constructs a new instance ofDateFormat.
-
-
Method Details
-
clone
Returns a new instance ofDateFormatwith the same properties. -
equals
Compares this date format with the specified object and indicates if they are equal.- Overrides:
equalsin classObject- Parameters:
object- the object to compare with this date format.- Returns:
trueifobjectis aDateFormatobject and it has the same properties as this date format;falseotherwise.- See Also:
hashCode()
-
format
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
fieldmember offieldcontains a value specifying a format field, then itsbeginIndexandendIndexmembers will be updated with the position of the first occurrence of this field in the formatted text.- Specified by:
formatin classFormat- Parameters:
object- the source object to format, must be aDateor aNumber. Ifobjectis a number then a date is constructed using thelongValue()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.- Returns:
- the string buffer.
- Throws:
IllegalArgumentException- ifobjectis neither aDatenor aNumberinstance.
-
format
Formats the specified date using the rules of this date format.- Parameters:
date- the date to format.- Returns:
- the formatted string.
-
format
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
fieldmember offieldcontains a value specifying a format field, then itsbeginIndexandendIndexmembers will be updated with the position of the first occurrence of this field in the formatted text.- Parameters:
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.- Returns:
- the string buffer.
-
getAvailableLocales
Returns an array of locales for which customDateFormatinstances are available.Note that Android does not support user-supplied locale service providers.
-
getCalendar
Returns the calendar used by thisDateFormat.- Returns:
- the calendar used by this date format.
-
getDateInstance
Returns aDateFormatinstance for formatting and parsing dates in the DEFAULT style for the default locale.- Returns:
- the
DateFormatinstance for the default style and locale.
-
getDateInstance
Returns aDateFormatinstance for formatting and parsing dates in the specified style for the user's default locale. See "Be wary of the default locale".- Parameters:
style- one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.- Returns:
- the
DateFormatinstance forstyleand the default locale. - Throws:
IllegalArgumentException- ifstyleis not one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.
-
getDateInstance
Returns aDateFormatinstance for formatting and parsing dates in the specified style for the specified locale.- Parameters:
style- one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.locale- the locale.- Returns:
- the
DateFormatinstance forstyleandlocale. - Throws:
IllegalArgumentException- ifstyleis not one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.
-
getDateTimeInstance
Returns aDateFormatinstance for formatting and parsing dates and time values in the DEFAULT style for the default locale.- Returns:
- the
DateFormatinstance for the default style and locale.
-
getDateTimeInstance
Returns aDateFormatinstance 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".- Parameters:
dateStyle- one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.timeStyle- one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.- Returns:
- the
DateFormatinstance fordateStyle,timeStyleand the default locale. - Throws:
IllegalArgumentException- ifdateStyleortimeStyleis not one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.
-
getDateTimeInstance
Returns aDateFormatinstance for formatting and parsing dates and time values in the specified styles for the specified locale.- Parameters:
dateStyle- one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.timeStyle- one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.locale- the locale.- Returns:
- the
DateFormatinstance fordateStyle,timeStyleandlocale. - Throws:
IllegalArgumentException- ifdateStyleortimeStyleis not one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.
-
getInstance
Returns aDateFormatinstance for formatting and parsing dates and times in the SHORT style for the default locale.- Returns:
- the
DateFormatinstance for the SHORT style and default locale.
-
getNumberFormat
Returns theNumberFormatused by thisDateFormat.- Returns:
- the
NumberFormatused by this date format.
-
getTimeInstance
Returns aDateFormatinstance for formatting and parsing time values in the DEFAULT style for the default locale.- Returns:
- the
DateFormatinstance for the default style and locale.
-
getTimeInstance
Returns aDateFormatinstance for formatting and parsing time values in the specified style for the user's default locale. See "Be wary of the default locale".- Parameters:
style- one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.- Returns:
- the
DateFormatinstance forstyleand the default locale. - Throws:
IllegalArgumentException- ifstyleis not one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.
-
getTimeInstance
Returns aDateFormatinstance for formatting and parsing time values in the specified style for the specified locale.- Parameters:
style- one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.locale- the locale.- Returns:
- the
DateFormatinstance forstyleandlocale. - Throws:
IllegalArgumentException- ifstyleis not one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.
-
getTimeZone
Returns the time zone of this date format's calendar.- Returns:
- the time zone of the calendar used by this date format.
-
hashCode
public int hashCode()Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually 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
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
isLenient
public boolean isLenient()Indicates whether the calendar used by this date format is lenient.- Returns:
trueif the calendar is lenient;falseotherwise.
-
parse
Parses a date from the specified string using the rules of this date format.- Parameters:
string- the string to parse.- Returns:
- the
Dateresulting from the parsing. - Throws:
ParseException- if an error occurs during parsing.
-
parse
Parses a date from the specified string starting at the index specified byposition. If the string is successfully parsed then the index of theParsePositionis updated to the index following the parsed text. On error, the index is unchanged and the error index ofParsePositionis 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).- Parameters:
string- the string to parse.position- input/output parameter, specifies the start index instringfrom 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.- Returns:
- the date resulting from the parse, or
nullif there is an error.
-
parseObject
Parses a date from the specified string starting at the index specified byposition. If the string is successfully parsed then the index of theParsePositionis updated to the index following the parsed text. On error, the index is unchanged and the error index ofParsePositionis 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).- Specified by:
parseObjectin classFormat- Parameters:
string- the string to parse.position- input/output parameter, specifies the start index instringfrom 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.- Returns:
- the date resulting from the parsing, or
nullif there is an error.
-
setCalendar
Sets the calendar used by this date format.- Parameters:
cal- the new calendar.
-
setLenient
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.- Parameters:
value-trueto set the calendar to be lenient,falseotherwise.
-
setNumberFormat
Sets theNumberFormatused by this date format.- Parameters:
format- the new number format.
-
setTimeZone
Sets the time zone of the calendar used by this date format.- Parameters:
timezone- the new time zone.
-