Added in API level 24

DateFormat


abstract class DateFormat : UFormat
kotlin.Any
   ↳ java.text.Format
   ↳ android.icu.text.UFormat
   ↳ android.icu.text.DateFormat

[icu enhancement] ICU's replacement for java.text.DateFormat. Methods, fields, and other functionality specific to ICU are labeled '[icu]'.

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 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. It provides many class methods for obtaining default date/time formatters based on the default for a given locale and a number of formatting styles or arbitrary "skeletons".

  1. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More detail and examples of using these styles are provided in the method descriptions.
  2. The formatting styles only cover a fraction of the necessary usage. You often need to have just certain combinations of fields, like Month and Year, but have it to be formatted appropriate to a given locale. This is done using the (misnamed) getPatternInstance() method, supplying a skeleton. There are a number of constants that have common pre-defined skeletons, such as MINUTE_SECOND for something like "13:45" or YEAR_ABBR_MONTH for something like "Sept 2012".

To format a date for the current Locale, use one of the static factory methods:

myString = DateFormat.getDateInstance().format(myDate);
  myString = DateFormat.getPatternInstance(DateFormat.YEAR_ABBR_MONTH).format(myDate);
  

If you are formatting multiple numbers, 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 date for a different Locale, specify it in the call to getDateInstance().

DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
  

You can use a DateFormat to parse also.

myDate = df.parse(myString);
  

There are many static factory methods available. Use getDateInstance to get the normal date format for that country. 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 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.

Use getPatternInstance to format with a skeleton. Typically this is with a predefined skeleton, like YEAR_ABBR_MONTH for something like "Sept 2012". If you don't want to use one of the predefined skeletons, you can supply your own. The skeletons are like the patterns in SimpleDateFormat, except they:

  1. only keep the field pattern letter and ignore all other parts in a pattern, such as space, punctuation, and string literals.
  2. are independent of the order of fields.
  3. ignore certain differences in the field's pattern letter length:
    1. For those non-digit calendar fields, the pattern letter length is important, such as MMM, MMMM, and MMMMM; E and EEEE, and the field's pattern letter length is honored.
    2. For the digit calendar fields, such as M or MM, d or dd, yy or yyyy, the field pattern length is ignored and the best match, which is defined in date time patterns, will be returned without honor the field pattern letter length in skeleton.

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

  • progressively parse through pieces of a string.
  • align any particular field, or find out where it is for selection on the screen.

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.

Summary

Nested classes

boolean attributes

open

The instances of this inner class are used as attribute keys and values in AttributedCharacterIterator that DateFormat.

Hour Cycle

Constants
static String

[icu] Constant for generic non-location format, abbreviated if possible, such as PT; used in combinations date + time + zone, or time + zone.

static String

[icu] Constant for date skeleton with abbreviated month.

static String

[icu] Constant for date skeleton with abbreviated month and day.

static String

[icu] Constant for date skeleton with abbreviated month, weekday, and day.

static String

[icu] Constant for date skeleton with abbreviated quarter.

static String

[icu] Constant for specific non-location format, abbreviated if possible, such as PDT; used in combinations date + time + zone, or time + zone.

static String

[icu] Constant for localized GMT/UTC format, such as GMT+8:00 or HPG-8:00; used in combinations date + time + zone, or time + zone.

static String

[icu] Constant for date skeleton with abbreviated weekday.

static Int

FieldPosition selector for 'a' field alignment, corresponding to the Calendar.AM_PM field.

static Int

[icu] FieldPosition selector for 'b' field alignment.

static Int

FieldPosition selector for 'd' field alignment, corresponding to the Calendar.DATE field.

static String

[icu] Constant for date skeleton with day.

static Int

FieldPosition selector for 'E' field alignment, corresponding to the Calendar.DAY_OF_WEEK field.

static Int

FieldPosition selector for 'F' field alignment, corresponding to the Calendar.DAY_OF_WEEK_IN_MONTH field.

static Int

FieldPosition selector for 'D' field alignment, corresponding to the Calendar.DAY_OF_YEAR field.

static Int

Constant for default style pattern.

static Int

[icu] FieldPosition selector for 'e' field alignment, corresponding to the Calendar.DOW_LOCAL field.

static Int

FieldPosition selector for 'G' field alignment, corresponding to the Calendar.ERA field.

static Int

[icu] FieldPosition selector for 'u' field alignment, corresponding to the Calendar.EXTENDED_YEAR field.

static Int

[icu] FieldPosition selector for 'B' field alignment.

static Int

[icu] FieldPosition selector for 'S' field alignment, corresponding to the Calendar.MILLISECOND field.

static Int

Constant for full style pattern.

static String

[icu] Constant for generic non-location format, such as Pacific Time; used in combinations date + time + zone, or time + zone.

static String

[icu] Constant for date skeleton with hour, with the locale's preferred hour format (12 or 24).

static Int

FieldPosition selector for 'K' field alignment, corresponding to the Calendar.HOUR field.

static Int

FieldPosition selector for 'h' field alignment, corresponding to the Calendar.HOUR field.

static String

[icu] Constant for date skeleton with hour in 24-hour presentation.

static String

[icu] Constant for date skeleton with hour and minute in 24-hour presentation.

static String

[icu] Constant for date skeleton with hour, minute, and second in 24-hour presentation.

static String

[icu] Constant for date skeleton with hour and minute, with the locale's preferred hour format (12 or 24).

static String

[icu] Constant for date skeleton with hour, minute, and second, with the locale's preferred hour format (12 or 24).

static Int

FieldPosition selector for 'H' field alignment, corresponding to the Calendar.HOUR_OF_DAY field.

static Int

FieldPosition selector for 'k' field alignment, corresponding to the Calendar.HOUR_OF_DAY field.

static Int

[icu] FieldPosition selector for 'g' field alignment, corresponding to the Calendar.JULIAN_DAY field.

static String

[icu] Constant for generic location format, such as Los Angeles Time; used in combinations date + time + zone, or time + zone.

static Int

Constant for long style pattern.

static Int

Constant for medium style pattern.

static Int

[icu] FieldPosition selector for 'A' field alignment, corresponding to the Calendar.MILLISECONDS_IN_DAY field.

static Int

Alias for FRACTIONAL_SECOND_FIELD.

static String

[icu] Constant for date skeleton with minute.

static Int

FieldPosition selector for 'm' field alignment, corresponding to the Calendar.MINUTE field.

static String

[icu] Constant for date skeleton with minute and second.

static String

[icu] Constant for date skeleton with month.

static String

[icu] Constant for date skeleton with long month and day.

static Int

FieldPosition selector for 'M' field alignment, corresponding to the Calendar.MONTH field.

static String

[icu] Constant for date skeleton with month, weekday, and day.

static Int

[icu] Constant for empty style pattern.

static String

[icu] Constant for date skeleton with numeric month.

static String

[icu] Constant for date skeleton with numeric month and day.

static String

[icu] Constant for date skeleton with numeric month, weekday, and day.

static String

[icu] Constant for date skeleton with quarter.

static Int

[icu] FieldPosition selector for 'Q' field alignment, corresponding to the Calendar.MONTH field.

static Int

[icu] Constant for relative style mask.

static Int

[icu] Constant for relative default style pattern.

static Int

[icu] Constant for relative full style pattern.

static Int

[icu] Constant for relative style pattern.

static Int

[icu] Constant for relative style pattern.

static Int

[icu] Constant for relative style pattern.

static String

[icu] Constant for date skeleton with second.

static Int

FieldPosition selector for 's' field alignment, corresponding to the Calendar.SECOND field.

static Int

Constant for short style pattern.

static String

[icu] Constant for specific non-location format, such as Pacific Daylight Time; used in combinations date + time + zone, or time + zone.

static Int

[icu] FieldPosition selector for 'c' field alignment, corresponding to the Calendar.DAY_OF_WEEK field.

static Int

[icu] FieldPosition selector for 'L' field alignment, corresponding to the Calendar.MONTH field.

static Int

[icu] FieldPosition selector for 'q' field alignment, corresponding to the Calendar.MONTH field.

static Int

FieldPosition selector for 'z' field alignment, corresponding to the Calendar.ZONE_OFFSET and Calendar.DST_OFFSET fields.

static Int

[icu] FieldPosition selector for 'v' field alignment, corresponding to the Calendar.ZONE_OFFSET and Calendar.DST_OFFSET fields.

static Int

[icu] FieldPosition selector for 'X' field alignment, corresponding to the Calendar.ZONE_OFFSET and Calendar.DST_OFFSET fields.

static Int

[icu] FieldPosition selector for 'x' field alignment, corresponding to the Calendar.ZONE_OFFSET and Calendar.DST_OFFSET fields.

static Int

[icu] FieldPosition selector for 'O' field alignment, corresponding to the Calendar.ZONE_OFFSET and Calendar.DST_OFFSET fields.

static Int

[icu] FieldPosition selector for 'Z' field alignment, corresponding to the Calendar.ZONE_OFFSET and Calendar.DST_OFFSET fields.

static Int

[icu] FieldPosition selector for 'V' field alignment, corresponding to the Calendar.ZONE_OFFSET and Calendar.DST_OFFSET fields.

static String

[icu] Constant for date skeleton with weekday.

static Int

FieldPosition selector for 'W' field alignment, corresponding to the Calendar.WEEK_OF_MONTH field.

static Int

FieldPosition selector for 'w' field alignment, corresponding to the Calendar.WEEK_OF_YEAR field.

static String

[icu] Constant for date skeleton with year.

static String

[icu] Constant for date skeleton with year and abbreviated month.

static String

[icu] Constant for date skeleton with year, abbreviated month, and day.

static String

[icu] Constant for date skeleton with year, abbreviated month, weekday, and day.

static String

[icu] Constant for date skeleton with year and abbreviated quarter.

static Int

FieldPosition selector for 'y' field alignment, corresponding to the Calendar.YEAR field.

static String

[icu] Constant for date skeleton with year and month.

static String

[icu] Constant for date skeleton with year, month, and day.

static String

[icu] Constant for date skeleton with year, month, weekday, and day.

static Int

[icu] FieldPosition selector for 'U' field alignment, corresponding to the Calendar.YEAR field.

static String

[icu] Constant for date skeleton with year and numeric month.

static String

[icu] Constant for date skeleton with year, numeric month, and day.

static String

[icu] Constant for date skeleton with year, numeric month, weekday, and day.

static String

[icu] Constant for date skeleton with year and quarter.

static Int

[icu] FieldPosition selector for 'Y' field alignment, corresponding to the Calendar.YEAR_WOY field.

Protected constructors

Creates a new date format.

Public methods
open Any

Overrides clone.

open Boolean
equals(other: Any?)

Overrides equals.

abstract StringBuffer!
format(cal: Calendar!, toAppendTo: StringBuffer!, fieldPosition: FieldPosition!)

Formats a date into a date/time string.

StringBuffer!
format(obj: Any!, toAppendTo: StringBuffer!, fieldPosition: FieldPosition!)

Formats a time object into a time string.

String!
format(date: Temporal!)

Formats a Temporal into a date/time string.

open StringBuffer!
format(date: Temporal!, toAppendTo: StringBuffer!, fieldPosition: FieldPosition!)

Formats a Temporal into a date/time string.

String!
format(date: Date!)

Formats a Date into a date/time string.

open StringBuffer!
format(date: Date!, toAppendTo: StringBuffer!, fieldPosition: FieldPosition!)

Formats a Date into a date/time string.

open static Array<Locale!>!

Returns the set of locales for which DateFormats are installed.

open Boolean

Returns the current value for the specified BooleanAttribute for this instance if attribute is missing false is returned.

open Calendar!

Returns the calendar associated with this date/time formatter.

open DisplayContext!

[icu] Get the formatter's DisplayContext value for the specified DisplayContext.

static DateFormat!

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

static DateFormat!
getDateInstance(cal: Calendar!, dateStyle: Int)

Creates a DateFormat object for the default locale that can be used to format dates in the calendar system specified by cal.

static DateFormat!
getDateInstance(cal: Calendar!, dateStyle: Int, locale: ULocale!)

Creates a DateFormat object that can be used to format dates in the calendar system specified by cal.

static DateFormat!
getDateInstance(cal: Calendar!, dateStyle: Int, locale: Locale!)

Creates a DateFormat object that can be used to format dates in the calendar system specified by cal.

static DateFormat!

Returns the date formatter with the given formatting style for the default FORMAT locale.

static DateFormat!
getDateInstance(style: Int, locale: ULocale!)

Returns the date formatter with the given formatting style for the given locale.

static DateFormat!
getDateInstance(style: Int, aLocale: Locale!)

Returns the date formatter with the given formatting style for the given locale.

static DateFormat!

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

static DateFormat!
getDateTimeInstance(cal: Calendar!, dateStyle: Int, timeStyle: Int)

Creates a DateFormat object for the default locale that can be used to format dates and times in the calendar system specified by cal.

static DateFormat!
getDateTimeInstance(cal: Calendar!, dateStyle: Int, timeStyle: Int, locale: ULocale!)

Creates a DateFormat object that can be used to format dates and times in the calendar system specified by cal.

static DateFormat!
getDateTimeInstance(cal: Calendar!, dateStyle: Int, timeStyle: Int, locale: Locale!)

Creates a DateFormat object that can be used to format dates and times in the calendar system specified by cal.

static DateFormat!
getDateTimeInstance(dateStyle: Int, timeStyle: Int)

Returns the date/time formatter with the given date and time formatting styles for the default FORMAT locale.

static DateFormat!
getDateTimeInstance(dateStyle: Int, timeStyle: Int, locale: ULocale!)

Returns the date/time formatter with the given formatting styles for the given locale.

static DateFormat!
getDateTimeInstance(dateStyle: Int, timeStyle: Int, aLocale: Locale!)

Returns the date/time formatter with the given formatting styles for the given locale.

static DateFormat!

Returns a default date/time formatter that uses the SHORT style for both the date and the time.

static DateFormat!

Returns a default date/time formatter that uses the SHORT style for both the date and the time.

static DateFormat!
getInstance(cal: Calendar!, locale: ULocale!)

Returns a date/time formatter that uses the SHORT style for both the date and the time.

static DateFormat!
getInstance(cal: Calendar!, locale: Locale!)

Returns a date/time formatter that uses the SHORT style for both the date and the time.

static DateFormat!
getInstanceForSkeleton(cal: Calendar!, skeleton: String!, locale: ULocale!)

[icu] Creates a DateFormat object that can be used to format dates and times in the calendar system specified by cal.

static DateFormat!
getInstanceForSkeleton(cal: Calendar!, skeleton: String!, locale: Locale!)

[icu] Creates a DateFormat object that can be used to format dates and times in the calendar system specified by cal.

static DateFormat!

[icu] Returns a DateFormat object that can be used to format dates and times in the default locale.

static DateFormat!
getInstanceForSkeleton(skeleton: String!, locale: ULocale!)

[icu] Returns a DateFormat object that can be used to format dates and times in the given locale.

static DateFormat!
getInstanceForSkeleton(skeleton: String!, locale: Locale!)

[icu] Returns a DateFormat object that can be used to format dates and times in the given locale.

open NumberFormat!

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

static DateFormat!
getPatternInstance(cal: Calendar!, skeleton: String!, locale: ULocale!)

[icu] Creates a DateFormat object that can be used to format dates and times in the calendar system specified by cal.

static DateFormat!
getPatternInstance(cal: Calendar!, skeleton: String!, locale: Locale!)

[icu] Creates a DateFormat object that can be used to format dates and times in the calendar system specified by cal.

static DateFormat!

[icu] Returns a DateFormat object that can be used to format dates and times in the default locale.

static DateFormat!
getPatternInstance(skeleton: String!, locale: ULocale!)

[icu] Returns a DateFormat object that can be used to format dates and times in the given locale.

static DateFormat!
getPatternInstance(skeleton: String!, locale: Locale!)

[icu] Returns a DateFormat object that can be used to format dates and times in the given locale.

static DateFormat!

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

static DateFormat!
getTimeInstance(cal: Calendar!, timeStyle: Int)

Creates a DateFormat object that can be used to format times in the calendar system specified by cal.

static DateFormat!
getTimeInstance(cal: Calendar!, timeStyle: Int, locale: ULocale!)

Creates a DateFormat object that can be used to format times in the calendar system specified by cal.

static DateFormat!
getTimeInstance(cal: Calendar!, timeStyle: Int, locale: Locale!)

Creates a DateFormat object that can be used to format times in the calendar system specified by cal.

static DateFormat!