DateFormat
abstract class DateFormat : Format
kotlin.Any | ||
↳ | java.text.Format | |
↳ | java.text.DateFormat |
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 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 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:
<code>myString = DateFormat.getDateInstance().format(myDate); </code>
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.
<code>DateFormat df = DateFormat.getDateInstance(); for (int i = 0; i < myDate.length; ++i) { output.println(df.format(myDate[i]) + "; "); } </code>
To format a date for a different Locale, specify it in the call to getDateInstance()
.
<code>DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE); </code>
You can use a DateFormat to parse also.
<code>myDate = df.parse(myString); </code>
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 completely numeric, such as12.13.52
or3:30pm
MEDIUM
is longer, such asJan 12, 1952
LONG
is longer, such asJanuary 12, 1952
or3:30:32pm
FULL
is pretty completely specified, such asTuesday, April 12, 1952 AD or 3:30:42pm PST
.
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 | |
---|---|
open |
Defines constants that are used as attribute keys in the |
Constants | |
---|---|
static Int |
Useful constant for AM_PM field alignment. |
static Int |
Useful constant for DATE field alignment. |
static Int |
Useful constant for DAY_OF_WEEK field alignment. |
static Int |
Useful constant for DAY_OF_WEEK_IN_MONTH field alignment. |
static Int |
Useful constant for DAY_OF_YEAR field alignment. |
static Int |
Constant for default style pattern. |
static Int |
Useful constant for ERA field alignment. |
static Int |
Constant for full style pattern. |
static Int |
Useful constant for zero-based HOUR field alignment. |
static Int |
Useful constant for one-based HOUR field alignment. |
static Int |
Useful constant for zero-based HOUR_OF_DAY field alignment. |
static Int |
Useful constant for one-based HOUR_OF_DAY field alignment. |
static Int |
Constant for long style pattern. |
static Int |
Constant for medium style pattern. |
static Int |
Useful constant for MILLISECOND field alignment. |
static Int |
Useful constant for MINUTE field alignment. |
static Int |
Useful constant for MONTH field alignment. |
static Int |
Useful constant for SECOND field alignment. |
static Int |
Constant for short style pattern. |
static Int |
Useful constant for TIMEZONE field alignment. |
static Int |
Useful constant for WEEK_OF_MONTH field alignment. |
static Int |
Useful constant for WEEK_OF_YEAR field alignment. |
static Int |
Useful constant for YEAR field alignment. |
Protected constructors | |
---|---|
Create a new date format. |
Public methods | |
---|---|
open Any |
clone() Overrides Cloneable |
open Boolean |
Overrides equals |
StringBuffer |
format(obj: Any, toAppendTo: StringBuffer, fieldPosition: FieldPosition) Formats the given |
String |
Formats a |
abstract StringBuffer |
format(date: Date, toAppendTo: StringBuffer, fieldPosition: FieldPosition) Formats a |
open static Array<Locale!> |
Returns an array of all locales for which the |
open Calendar |
Gets the calendar associated with this date/time formatter. |
static DateFormat |
Gets the date formatter with the default formatting style for the default |
static DateFormat |
getDateInstance(style: Int) Gets the date formatter with the given formatting style for the default |
static DateFormat |
getDateInstance(style: Int, aLocale: Locale) Gets the date formatter with the given formatting style for the given locale. |
static DateFormat |
Gets the date/time formatter with the default formatting style for the default |
static DateFormat |
getDateTimeInstance(dateStyle: Int, timeStyle: Int) Gets the date/time formatter with the given date and time formatting styles for the default |
static DateFormat |
getDateTimeInstance(dateStyle: Int, timeStyle: Int, aLocale: Locale) Gets the date/time formatter with the given formatting styles for the given locale. |
static DateFormat |
Get a default date/time formatter that uses the SHORT style for both the date and the time. |
open NumberFormat |
Gets the number formatter which this date/time formatter uses to format and parse a time. |
static DateFormat |
Gets the time formatter with the default formatting style for the default |
static DateFormat |
getTimeInstance(style: Int) Gets the time formatter with the given formatting style for the default |
static DateFormat |
getTimeInstance(style: Int, aLocale: Locale) Gets the time formatter with the given formatting style for the given locale. |
open TimeZone |
Gets the time zone. |
open Int |
hashCode() Overrides hashCode |
open Boolean |
Tell whether date/time parsing is to be lenient. |
open Date? |
Parses text from the beginning of the given string to produce a date. |
abstract Date? |
parse(source: String, pos: ParsePosition) Parse a date/time string according to the given parse position. |
open Any? |
parseObject(source: String, pos: ParsePosition) Parses text from a string to produce a |
open Unit |
setCalendar(newCalendar: Calendar) Set the calendar to be used by this date format. |
open Unit |
setLenient(lenient: Boolean) Specify whether or not date/time parsing is to be lenient. |
open Unit |
setNumberFormat(newNumberFormat: NumberFormat) Allows you to set the number formatter. |
open Unit |
setTimeZone(zone: TimeZone) Sets the time zone for the calendar of this |
Inherited functions | |
---|---|
Properties | |
---|---|
Calendar |
The |
NumberFormat |
The number formatter that |
Constants
AM_PM_FIELD
static val AM_PM_FIELD: Int
Useful constant for AM_PM field alignment. Used in FieldPosition of date/time formatting.
Value: 14
DATE_FIELD
static val DATE_FIELD: Int
Useful constant for DATE field alignment. Used in FieldPosition of date/time formatting.
Value: 3
DAY_OF_WEEK_FIELD
static val DAY_OF_WEEK_FIELD: Int
Useful constant for DAY_OF_WEEK field alignment. Used in FieldPosition of date/time formatting.
Value: 9
DAY_OF_WEEK_IN_MONTH_FIELD
static val DAY_OF_WEEK_IN_MONTH_FIELD: Int
Useful constant for DAY_OF_WEEK_IN_MONTH field alignment. Used in FieldPosition of date/time formatting.
Value: 11
DAY_OF_YEAR_FIELD
static val DAY_OF_YEAR_FIELD: Int
Useful constant for DAY_OF_YEAR field alignment. Used in FieldPosition of date/time formatting.
Value: 10
DEFAULT
static val DEFAULT: Int
Constant for default style pattern. Its value is MEDIUM.
Value: 2
ERA_FIELD
static val ERA_FIELD: Int
Useful constant for ERA field alignment. Used in FieldPosition of date/time formatting.
Value: 0
HOUR0_FIELD
static val HOUR0_FIELD: Int
Useful constant for zero-based HOUR field alignment. Used in FieldPosition of date/time formatting. HOUR0_FIELD is used for the zero-based 12-hour clock. For example, 11:30 PM + 1 hour results in 00:30 AM.
Value: 16
HOUR1_FIELD
static val HOUR1_FIELD: Int
Useful constant for one-based HOUR field alignment. Used in FieldPosition of date/time formatting. HOUR1_FIELD is used for the one-based 12-hour clock. For example, 11:30 PM + 1 hour results in 12:30 AM.
Value: 15
HOUR_OF_DAY0_FIELD
static val HOUR_OF_DAY0_FIELD: Int
Useful constant for zero-based HOUR_OF_DAY field alignment. Used in FieldPosition of date/time formatting. HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock. For example, 23:59 + 01:00 results in 00:59.
Value: 5
HOUR_OF_DAY1_FIELD
static val HOUR_OF_DAY1_FIELD: Int
Useful constant for one-based HOUR_OF_DAY field alignment. Used in FieldPosition of date/time formatting. HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock. For example, 23:59 + 01:00 results in 24:59.
Value: 4
MILLISECOND_FIELD
static val MILLISECOND_FIELD: Int
Useful constant for MILLISECOND field alignment. Used in FieldPosition of date/time formatting.
Value: 8
MINUTE_FIELD
static val MINUTE_FIELD: Int
Useful constant for MINUTE field alignment. Used in FieldPosition of date/time formatting.
Value: 6
MONTH_FIELD
static val MONTH_FIELD: Int
Useful constant for MONTH field alignment. Used in FieldPosition of date/time formatting.
Value: 2
SECOND_FIELD
static val SECOND_FIELD: Int
Useful constant for SECOND field alignment. Used in FieldPosition of date/time formatting.
Value: 7
TIMEZONE_FIELD
static val TIMEZONE_FIELD: Int
Useful constant for TIMEZONE field alignment. Used in FieldPosition of date/time formatting.
Value: 17
WEEK_OF_MONTH_FIELD
static val WEEK_OF_MONTH_FIELD: Int
Useful constant for WEEK_OF_MONTH field alignment. Used in FieldPosition of date/time formatting.
Value: 13
WEEK_OF_YEAR_FIELD
static val WEEK_OF_YEAR_FIELD: Int
Useful constant for WEEK_OF_YEAR field alignment. Used in FieldPosition of date/time formatting.
Value: 12
YEAR_FIELD
static val YEAR_FIELD: Int
Useful constant for YEAR field alignment. Used in FieldPosition of date/time formatting.
Value: 1
Protected constructors
Public methods
clone
open fun clone(): Any
Overrides Cloneable
Return | |
---|---|
Any |
a clone of this instance. |
Exceptions | |
---|---|
java.lang.CloneNotSupportedException |
if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned. |
equals
open fun equals(other: Any?): Boolean
Overrides equals
Parameters | |
---|---|
obj |
the reference object with which to compare. |
Return | |
---|---|
Boolean |
true if this object is the same as the obj argument; false otherwise. |
format
fun format(
obj: Any,
toAppendTo: StringBuffer,
fieldPosition: FieldPosition
): StringBuffer
Formats the given Object
into a date-time string. The formatted string is appended to the given StringBuffer
.
Parameters | |
---|---|
obj |
Any: Must be a Date or a Number representing a millisecond offset from the Epoch. |
toAppendTo |
StringBuffer: The string buffer for the returning date-time string. |
pos |
A FieldPosition identifying a field in the formatted text |
fieldPosition |
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' . |
Return | |
---|---|
StringBuffer |
the string buffer passed in as toAppendTo , with formatted text appended. |
Exceptions | |
---|---|
java.lang.NullPointerException |
if toAppendTo or pos is null |
java.lang.IllegalArgumentException |
if the Format cannot format the given obj . |
See Also
format
fun format(date: Date): String
Formats a Date
into a date-time string.
Parameters | |
---|---|
date |
Date: the time value to be formatted into a date-time string. |
Return | |
---|---|
String |
the formatted date-time string. |
format
abstract fun format(
date: Date,
toAppendTo: StringBuffer,
fieldPosition: FieldPosition
): StringBuffer
Formats a Date
into a date-time string. The formatted string is appended to the given StringBuffer
.
Parameters | |
---|---|
date |
Date: a Date to be formatted into a date-time string. |
toAppendTo |
StringBuffer: the string buffer for the returning date-time string. |
fieldPosition |
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' . |
Return | |
---|---|
StringBuffer |
the string buffer passed in as toAppendTo , with formatted text appended. |
getAvailableLocales
open static fun getAvailableLocales(): Array<Locale!>
Returns an array of all locales for which the get*Instance
methods of this class can return localized instances. It must contain at least a Locale
instance equal to Locale.US
.
Return | |
---|---|
Array<Locale!> |
An array of locales for which localized DateFormat instances are available. |
getCalendar
open fun getCalendar(): Calendar
Gets the calendar associated with this date/time formatter.
Return | |
---|---|
Calendar |
the calendar associated with this date/time formatter. |
getDateInstance
static fun getDateInstance(): DateFormat
Gets the date formatter with the default formatting style for the default FORMAT
locale.
This is equivalent to calling getDateInstance(DEFAULT,
.
Return | |
---|---|
DateFormat |
a date formatter. |
getDateInstance
static fun getDateInstance(style: Int): DateFormat
Gets the date formatter with the given formatting style for the default FORMAT
locale.
This is equivalent to calling getDateInstance(style,
.
Parameters | |
---|---|
style |
Int: the given formatting style. For example, SHORT for "M/d/yy" in the US locale. |
Return | |
---|---|
DateFormat |
a date formatter. |
getDateInstance
static fun getDateInstance(
style: Int,
aLocale: Locale
): DateFormat
Gets the date formatter with the given formatting style for the given locale.
Parameters | |
---|---|
style |
Int: the given formatting style. For example, SHORT for "M/d/yy" in the US locale. |
aLocale |
Locale: the given locale. |
Return | |
---|---|
DateFormat |
a date formatter. |
getDateTimeInstance
static fun getDateTimeInstance(): DateFormat
Gets the date/time formatter with the default formatting style for the default FORMAT
locale.
This is equivalent to calling getDateTimeInstance(DEFAULT,
.
Return | |
---|---|
DateFormat |
a date/time formatter. |
getDateTimeInstance
static fun getDateTimeInstance(
dateStyle: Int,
timeStyle: Int
): DateFormat
Gets the date/time formatter with the given date and time formatting styles for the default FORMAT
locale.
This is equivalent to calling getDateTimeInstance(dateStyle,
.
Parameters | |
---|---|
dateStyle |
Int: the given date formatting style. For example, SHORT for "M/d/yy" in the US locale. |
timeStyle |
Int: the given time formatting style. For example, SHORT for "h:mm a" in the US locale. |
Return | |
---|---|
DateFormat |
a date/time formatter. |
getDateTimeInstance
static fun getDateTimeInstance(
dateStyle: Int,
timeStyle: Int,
aLocale: Locale
): DateFormat
Gets the date/time formatter with the given formatting styles for the given locale.
Parameters | |
---|---|
dateStyle |
Int: the given date formatting style. |
timeStyle |
Int: the given time formatting style. |
aLocale |
Locale: the given locale. |
Return | |
---|---|
DateFormat |
a date/time formatter. |
getInstance
static fun getInstance(): DateFormat
Get a default date/time formatter that uses the SHORT style for both the date and the time.
Return | |
---|---|
DateFormat |
a date/time formatter |
getNumberFormat
open fun getNumberFormat(): NumberFormat
Gets the number formatter which this date/time formatter uses to format and parse a time.
Return | |
---|---|
NumberFormat |
the number formatter which this date/time formatter uses. |
getTimeInstance
static fun getTimeInstance(): DateFormat
Gets the time formatter with the default formatting style for the default FORMAT
locale.
This is equivalent to calling getTimeInstance(DEFAULT,
.
Return | |
---|---|
DateFormat |
a time formatter. |
getTimeInstance
static fun getTimeInstance(style: Int): DateFormat
Gets the time formatter with the given formatting style for the default FORMAT
locale.
This is equivalent to calling getTimeInstance(style,
.
Parameters | |
---|---|
style |
Int: the given formatting style. For example, SHORT for "h:mm a" in the US locale. |
Return | |
---|---|
DateFormat |
a time formatter. |
getTimeInstance
static fun getTimeInstance(
style: Int,
aLocale: Locale
): DateFormat
Gets the time formatter with the given formatting style for the given locale.
Parameters | |
---|---|
style |
Int: the given formatting style. For example, SHORT for "h:mm a" in the US locale. |
aLocale |
Locale: the given locale. |
Return | |
---|---|
DateFormat |
a time formatter. |
getTimeZone
open fun getTimeZone(): TimeZone
Gets the time zone. This method is equivalent to the following call.
<code>getCalendar().getTimeZone() </code>
Return | |
---|---|
TimeZone |
the time zone associated with the calendar of DateFormat. |
hashCode
open fun hashCode(): Int
Overrides hashCode
Return | |
---|---|
Int |
a hash code value for this object. |
isLenient
open fun isLenient(): Boolean
Tell whether date/time parsing is to be lenient. This method is equivalent to the following call.
<code>getCalendar().isLenient() </code>
Return | |
---|---|
Boolean |
true if the calendar is lenient; false otherwise. |
See Also
parse
open fun parse(source: String): Date?
Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.
See the parse(java.lang.String,java.text.ParsePosition)
method for more information on date parsing.
WARNING: Don't use this method to deserialize a date. The underlying localized date/time format and parsing behaviors can change across Android versions as common usage in the locale changes. Consider using long
type for storing a timestamp or java.time.format.DateTimeFormatter#ISO_INSTANT
for deserializing the ISO-8601 instant format.
Parameters | |
---|---|
source |
String: A String whose beginning should be parsed. |
Return | |
---|---|
Date? |
A Date parsed from the string. |
Exceptions | |
---|---|
java.text.ParseException |
if the beginning of the specified string cannot be parsed. |
parse
abstract fun parse(
source: String,
pos: ParsePosition
): Date?
Parse a date/time string according to the given parse position. For example, a time text "07/10/96 4:5 PM, PDT"
will be parsed into a Date
that is equivalent to Date(837039900000L)
.
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)
.
WARNING: Don't use this method to deserialize a date. The underlying localized date/time format and parsing behaviors can change across Android versions as common usage in the locale changes. Consider using long
type for storing a timestamp or java.time.format.DateTimeFormatter#ISO_INSTANT
for deserializing the ISO-8601 instant format.
This parsing operation uses the calendar
to produce a Date
. As a result, the calendar
's date-time fields and the TimeZone
value may have been overwritten, depending on subclass implementations. Any TimeZone
value that has previously been set by a call to setTimeZone
may need to be restored for further operations.
Parameters | |
---|---|
source |
String: The date/time string to be parsed |
pos |
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. |
Return | |
---|---|
Date? |
A Date , or null if the input could not be parsed |
parseObject
open fun parseObject(
source: String,
pos: ParsePosition
): Any?
Parses text from a string to produce a Date
.
The method attempts to parse text starting at the index given by pos
. If parsing succeeds, then the index of pos
is updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed date is returned. The updated pos
can be used to indicate the starting point for the next call to this method. If an error occurs, then the index of pos
is not changed, the error index of pos
is set to the index of the character where the error occurred, and null is returned.
See the parse(java.lang.String,java.text.ParsePosition)
method for more information on date parsing.
Parameters | |
---|---|
source |
String: A String , part of which should be parsed. |
pos |
ParsePosition: A ParsePosition object with index and error index information as described above. |
Return | |
---|---|
Any? |
A Date parsed from the string. In case of error, returns null. |
Exceptions | |
---|---|
java.lang.NullPointerException |
if source or pos is null. |
setCalendar
open fun setCalendar(newCalendar: Calendar): Unit
Set the calendar to be used by this date format. Initially, the default calendar for the specified or default locale is used.
Any TimeZone
and leniency values that have previously been set are overwritten by newCalendar
's values.
Parameters | |
---|---|
newCalendar |
Calendar: the new Calendar to be used by the date format |
setLenient
open fun setLenient(lenient: Boolean): Unit
Specify whether or not date/time parsing is to 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.
This method is equivalent to the following call.
<code>getCalendar().setLenient(lenient) </code>
This leniency value is overwritten by a call to setCalendar()
.
Parameters | |
---|---|
lenient |
Boolean: when true , parsing is lenient |
setNumberFormat
open fun setNumberFormat(newNumberFormat: NumberFormat): Unit
Allows you to set the number formatter.
Parameters | |
---|---|
newNumberFormat |
NumberFormat: the given new NumberFormat. |
setTimeZone
open fun setTimeZone(zone: TimeZone): Unit
Sets the time zone for the calendar of this DateFormat
object. This method is equivalent to the following call.
<code>getCalendar().setTimeZone(zone) </code>
The TimeZone
set by this method is overwritten by a setCalendar
call.
The TimeZone
set by this method may be overwritten as a result of a call to the parse method.
Parameters | |
---|---|
zone |
TimeZone: the given new time zone. |
Properties
calendar
protected var calendar: Calendar
The Calendar
instance used for calculating the date-time fields and the instant of time. This field is used for both formatting and parsing.
Subclasses should initialize this field to a Calendar
appropriate for the Locale
associated with this DateFormat
.
numberFormat
protected var numberFormat: NumberFormat
The number formatter that DateFormat
uses to format numbers in dates and times. Subclasses should initialize this to a number format appropriate for the locale associated with this DateFormat
.