DateFormat Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
<strong>[icu enhancement]</strong> ICU's replacement for java.text.DateFormat
.
[Android.Runtime.Register("android/icu/text/DateFormat", ApiSince=24, DoNotGenerateAcw=true)]
public abstract class DateFormat : Android.Icu.Text.UFormat
[<Android.Runtime.Register("android/icu/text/DateFormat", ApiSince=24, DoNotGenerateAcw=true)>]
type DateFormat = class
inherit UFormat
- Inheritance
- Derived
- Attributes
Remarks
<strong>[icu enhancement]</strong> ICU's replacement for java.text.DateFormat
. Methods, fields, and other functionality specific to ICU are labeled '<strong>[icu]</strong>'.
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". <ol> <li>The formatting styles include FULL, LONG, MEDIUM, and SHORT. More detail and examples of using these styles are provided in the method descriptions. <li>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". </ol>
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: <ul> <li>SHORT is completely numeric, such as 12.13.52 or 3:30pm <li>MEDIUM is longer, such as Jan 12, 1952 <li>LONG is longer, such as January 12, 1952 or 3:30:32pm <li>FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST. </ul>
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: <ol> <li>only keep the field pattern letter and ignore all other parts in a pattern, such as space, punctuation, and string literals. <li>are independent of the order of fields. <li>ignore certain differences in the field's pattern letter length: <ol> <li>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. <li>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. </ol> </ol>
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 <ul> <li>progressively parse through pieces of a string. <li>align any particular field, or find out where it is for selection on the screen. </ul>
<h3>Synchronization</h3>
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.
Java documentation for android.icu.text.DateFormat
.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Constructors
DateFormat() |
Creates a new date format. |
DateFormat(IntPtr, JniHandleOwnership) |
Fields
AbbrGenericTz |
<strong>[icu]</strong> Constant for generic non-location format, abbreviated if possible, such as PT; used in combinations date + time + zone, or time + zone. |
AbbrMonth |
<strong>[icu]</strong> Constant for date skeleton with abbreviated month. |
AbbrMonthDay |
<strong>[icu]</strong> Constant for date skeleton with abbreviated month and day. |
AbbrMonthWeekdayDay |
<strong>[icu]</strong> Constant for date skeleton with abbreviated month, weekday, and day. |
AbbrQuarter |
<strong>[icu]</strong> Constant for date skeleton with abbreviated quarter. |
AbbrSpecificTz |
<strong>[icu]</strong> Constant for specific non-location format, abbreviated if possible, such as PDT; used in combinations date + time + zone, or time + zone. |
AbbrUtcTz |
<strong>[icu]</strong> Constant for localized GMT/UTC format, such as GMT+8:00 or HPG-8:00; used in combinations date + time + zone, or time + zone. |
AbbrWeekday |
<strong>[icu]</strong> Constant for date skeleton with abbreviated weekday. |
AmPmField |
Obsolete.
FieldPosition selector for 'a' field alignment,
corresponding to the |
AmPmMidnightNoonField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'b' field alignment. |
DateField |
Obsolete.
FieldPosition selector for 'd' field alignment,
corresponding to the |
Day |
<strong>[icu]</strong> Constant for date skeleton with day. |
DayOfWeekField |
Obsolete.
FieldPosition selector for 'E' field alignment,
corresponding to the |
DayOfWeekInMonthField |
Obsolete.
FieldPosition selector for 'F' field alignment,
corresponding to the |
DayOfYearField |
Obsolete.
FieldPosition selector for 'D' field alignment,
corresponding to the |
Default |
Obsolete.
Constant for default style pattern. |
DowLocalField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'e' field alignment,
corresponding to the |
EraField |
Obsolete.
FieldPosition selector for 'G' field alignment,
corresponding to the |
ExtendedYearField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'u' field alignment,
corresponding to the |
FlexibleDayPeriodField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'B' field alignment. |
FractionalSecondField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'S' field alignment,
corresponding to the |
Full |
Obsolete.
Constant for full style pattern. |
GenericTz |
<strong>[icu]</strong> Constant for generic non-location format, such as Pacific Time; used in combinations date + time + zone, or time + zone. |
Hour |
<strong>[icu]</strong> Constant for date skeleton with hour, with the locale's preferred hour format (12 or 24). |
Hour0Field |
Obsolete.
FieldPosition selector for 'K' field alignment,
corresponding to the |
Hour1Field |
Obsolete.
FieldPosition selector for 'h' field alignment,
corresponding to the |
Hour24 |
<strong>[icu]</strong> Constant for date skeleton with hour in 24-hour presentation. |
Hour24Minute |
<strong>[icu]</strong> Constant for date skeleton with hour and minute in 24-hour presentation. |
Hour24MinuteSecond |
<strong>[icu]</strong> Constant for date skeleton with hour, minute, and second in 24-hour presentation. |
HourMinute |
<strong>[icu]</strong> Constant for date skeleton with hour and minute, with the locale's preferred hour format (12 or 24). |
HourMinuteSecond |
<strong>[icu]</strong> Constant for date skeleton with hour, minute, and second, with the locale's preferred hour format (12 or 24). |
HourOfDay0Field |
Obsolete.
FieldPosition selector for 'H' field alignment,
corresponding to the |
HourOfDay1Field |
Obsolete.
FieldPosition selector for 'k' field alignment,
corresponding to the |
JulianDayField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'g' field alignment,
corresponding to the |
LocationTz |
<strong>[icu]</strong> Constant for generic location format, such as Los Angeles Time; used in combinations date + time + zone, or time + zone. |
Long |
Obsolete.
Constant for long style pattern. |
Medium |
Obsolete.
Constant for medium style pattern. |
MillisecondField |
Obsolete.
Alias for FRACTIONAL_SECOND_FIELD. |
MillisecondsInDayField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'A' field alignment,
corresponding to the |
Minute |
<strong>[icu]</strong> Constant for date skeleton with minute. |
MinuteField |
Obsolete.
FieldPosition selector for 'm' field alignment,
corresponding to the |
MinuteSecond |
<strong>[icu]</strong> Constant for date skeleton with minute and second. |
Month |
<strong>[icu]</strong> Constant for date skeleton with month. |
MonthDay |
<strong>[icu]</strong> Constant for date skeleton with long month and day. |
MonthField |
Obsolete.
FieldPosition selector for 'M' field alignment,
corresponding to the |
MonthWeekdayDay |
<strong>[icu]</strong> Constant for date skeleton with month, weekday, and day. |
None |
Obsolete.
<strong>[icu]</strong> Constant for empty style pattern. |
NumMonth |
<strong>[icu]</strong> Constant for date skeleton with numeric month. |
NumMonthDay |
<strong>[icu]</strong> Constant for date skeleton with numeric month and day. |
NumMonthWeekdayDay |
<strong>[icu]</strong> Constant for date skeleton with numeric month, weekday, and day. |
Quarter |
<strong>[icu]</strong> Constant for date skeleton with quarter. |
QuarterField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'Q' field alignment,
corresponding to the |
Relative |
Obsolete.
<strong>[icu]</strong> Constant for relative style mask. |
RelativeDefault |
Obsolete.
<strong>[icu]</strong> Constant for relative default style pattern. |
RelativeFull |
Obsolete.
<strong>[icu]</strong> Constant for relative full style pattern. |
RelativeLong |
Obsolete.
<strong>[icu]</strong> Constant for relative style pattern. |
RelativeMedium |
Obsolete.
<strong>[icu]</strong> Constant for relative style pattern. |
RelativeShort |
Obsolete.
<strong>[icu]</strong> Constant for relative style pattern. |
Second |
<strong>[icu]</strong> Constant for date skeleton with second. |
SecondField |
Obsolete.
FieldPosition selector for 's' field alignment,
corresponding to the |
Short |
Obsolete.
Constant for short style pattern. |
SpecificTz |
<strong>[icu]</strong> Constant for specific non-location format, such as Pacific Daylight Time; used in combinations date + time + zone, or time + zone. |
StandaloneDayField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'c' field alignment,
corresponding to the |
StandaloneMonthField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'L' field alignment,
corresponding to the |
StandaloneQuarterField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'q' field alignment,
corresponding to the |
TimezoneField |
Obsolete.
FieldPosition selector for 'z' field alignment,
corresponding to the |
TimezoneGenericField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'v' field alignment,
corresponding to the |
TimezoneIsoField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'X' field alignment,
corresponding to the |
TimezoneIsoLocalField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'x' field alignment,
corresponding to the |
TimezoneLocalizedGmtOffsetField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'O' field alignment,
corresponding to the |
TimezoneRfcField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'Z' field alignment,
corresponding to the |
TimezoneSpecialField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'V' field alignment,
corresponding to the |
Weekday |
<strong>[icu]</strong> Constant for date skeleton with weekday. |
WeekOfMonthField |
Obsolete.
FieldPosition selector for 'W' field alignment,
corresponding to the |
WeekOfYearField |
Obsolete.
FieldPosition selector for 'w' field alignment,
corresponding to the |
Year |
<strong>[icu]</strong> Constant for date skeleton with year. |
YearAbbrMonth |
<strong>[icu]</strong> Constant for date skeleton with year and abbreviated month. |
YearAbbrMonthDay |
<strong>[icu]</strong> Constant for date skeleton with year, abbreviated month, and day. |
YearAbbrMonthWeekdayDay |
<strong>[icu]</strong> Constant for date skeleton with year, abbreviated month, weekday, and day. |
YearAbbrQuarter |
<strong>[icu]</strong> Constant for date skeleton with year and abbreviated quarter. |
YearField |
Obsolete.
FieldPosition selector for 'y' field alignment,
corresponding to the |
YearMonth |
<strong>[icu]</strong> Constant for date skeleton with year and month. |
YearMonthDay |
<strong>[icu]</strong> Constant for date skeleton with year, month, and day. |
YearMonthWeekdayDay |
<strong>[icu]</strong> Constant for date skeleton with year, month, weekday, and day. |
YearNameField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'U' field alignment,
corresponding to the |
YearNumMonth |
<strong>[icu]</strong> Constant for date skeleton with year and numeric month. |
YearNumMonthDay |
<strong>[icu]</strong> Constant for date skeleton with year, numeric month, and day. |
YearNumMonthWeekdayDay |
<strong>[icu]</strong> Constant for date skeleton with year, numeric month, weekday, and day. |
YearQuarter |
<strong>[icu]</strong> Constant for date skeleton with year and quarter. |
YearWoyField |
Obsolete.
<strong>[icu]</strong> FieldPosition selector for 'Y' field alignment,
corresponding to the |
Properties
Calendar |
Returns the calendar associated with this date/time formatter. -or- Sets the calendar to be used by this date format. |
CalendarLenient |
Returns whether date/time parsing in the encapsulated Calendar object is lenient. -or- Specifies whether date/time parsing in the encapsulated Calendar object should be lenient. |
Class |
Returns the runtime class of this |
DateInstance |
Returns the date formatter with the default formatting style
for the default |
DateTimeInstance |
Returns the date/time formatter with the default formatting style
for the default |
Handle |
The handle to the underlying Android instance. (Inherited from Object) |
Instance |
Returns a default date/time formatter that uses the SHORT style for both the date and the time. |
JniIdentityHashCode | (Inherited from Object) |
JniPeerMembers | |
Lenient |
Returns whether both date/time parsing in the encapsulated Calendar object and DateFormat whitespace & numeric processing is lenient. -or- Specifies whether date/time parsing is to be lenient. |
NumberFormat |
Returns the number formatter which this date/time formatter uses to format and parse a time. -or- Sets the number formatter. |
PeerReference | (Inherited from Object) |
ThresholdClass | |
ThresholdType | |
TimeInstance |
Gets the time formatter with the default formatting style
for the default |
TimeZone |
Returns the time zone. -or- Sets the time zone for the calendar of this DateFormat object. |
Methods
Clone() |
Creates and returns a copy of this object. (Inherited from _Format) |
Dispose() | (Inherited from Object) |
Dispose(Boolean) | (Inherited from Object) |
Equals(Object) |
Indicates whether some other object is "equal to" this one. (Inherited from Object) |
Format(Calendar, StringBuffer, FieldPosition) |
Formats a date into a date/time string. |
Format(Date, StringBuffer, FieldPosition) |
Formats a Date into a date/time string. |
Format(Date) |
Formats a Date into a date/time string. |
Format(Object, StringBuffer, FieldPosition) |
Formats a time object into a time string. |
Format(Object) |
Formats an object to produce a string. (Inherited from _Format) |
FormatToCharacterIterator(Object) |
Formats an Object producing an |
GetAvailableLocales() |
Returns the set of locales for which DateFormats are installed. |
GetBooleanAttribute(DateFormat+BooleanAttribute) | |
GetContext(DisplayContext+Type) | |
GetDateInstance(Calendar, DateFormatStyle, Locale) |
Creates a |
GetDateInstance(Calendar, DateFormatStyle, ULocale) |
Creates a |
GetDateInstance(Calendar, DateFormatStyle) |
Creates a |
GetDateInstance(DateFormatStyle, Locale) |
Returns the date formatter with the given formatting style for the given locale. |
GetDateInstance(DateFormatStyle, ULocale) |
Returns the date formatter with the given formatting style for the given locale. |
GetDateInstance(DateFormatStyle) |
Returns the date formatter with the given formatting style
for the default |
GetDateTimeInstance(Calendar, DateFormatStyle, Int32, Locale) |
Creates a |
GetDateTimeInstance(Calendar, DateFormatStyle, Int32, ULocale) |
Creates a |
GetDateTimeInstance(Calendar, DateFormatStyle, Int32) |
Creates a |
GetDateTimeInstance(DateFormatStyle, Int32, Locale) |
Returns the date/time formatter with the given formatting styles for the given locale. |
GetDateTimeInstance(DateFormatStyle, Int32, ULocale) |
Returns the date/time formatter with the given formatting styles for the given locale. |
GetDateTimeInstance(DateFormatStyle, Int32) |
Returns the date/time formatter with the given date and time
formatting styles for the default |
GetHashCode() |
Returns a hash code value for the object. (Inherited from Object) |
GetInstance(Calendar, Locale) |
Returns a date/time formatter that uses the SHORT style for both the date and the time. |
GetInstance(Calendar, ULocale) |
Returns a date/time formatter that uses the SHORT style for both the date and the time. |
GetInstance(Calendar) |
Returns a default date/time formatter that uses the SHORT style for both the date and the time. |
GetInstanceForSkeleton(Calendar, String, Locale) |
<strong>[icu]</strong> Creates a |
GetInstanceForSkeleton(Calendar, String, ULocale) |
<strong>[icu]</strong> Creates a |
GetInstanceForSkeleton(String, Locale) |
<strong>[icu]</strong> Returns a |
GetInstanceForSkeleton(String, ULocale) |
<strong>[icu]</strong> Returns a |
GetInstanceForSkeleton(String) |
<strong>[icu]</strong> Returns a |
GetPatternInstance(Calendar, String, Locale) |
<strong>[icu]</strong> Creates a |
GetPatternInstance(Calendar, String, ULocale) |
<strong>[icu]</strong> Creates a |
GetPatternInstance(String, Locale) |
<strong>[icu]</strong> Returns a |
GetPatternInstance(String, ULocale) |
<strong>[icu]</strong> Returns a |
GetPatternInstance(String) |
<strong>[icu]</strong> Returns a |
GetTimeInstance(Calendar, DateFormatStyle, Locale) |
Creates a |
GetTimeInstance(Calendar, DateFormatStyle, ULocale) |
Creates a |
GetTimeInstance(Calendar, DateFormatStyle) |
Creates a |
GetTimeInstance(DateFormatStyle, Locale) |
Returns the time formatter with the given formatting style for the given locale. |
GetTimeInstance(DateFormatStyle, ULocale) |
Returns the time formatter with the given formatting style for the given locale. |
GetTimeInstance(DateFormatStyle) |
Returns the time formatter with the given formatting style
for the default |
JavaFinalize() |
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. (Inherited from Object) |
Notify() |
Wakes up a single thread that is waiting on this object's monitor. (Inherited from Object) |
NotifyAll() |
Wakes up all threads that are waiting on this object's monitor. (Inherited from Object) |
Parse(String, Calendar, ParsePosition) |
Parses a date/time string according to the given parse position. |
Parse(String, ParsePosition) |
Parses a date/time string according to the given parse position. |
Parse(String) |
Parses a date/time string. |
ParseObject(String, ParsePosition) |
Parses a date/time string into an Object. |
ParseObject(String) |
Parses text from the beginning of the given string to produce an object. (Inherited from _Format) |
SetBooleanAttribute(DateFormat+BooleanAttribute, Boolean) | |
SetContext(DisplayContext) |
<strong>[icu]</strong> Set a particular DisplayContext value in the formatter, such as CAPITALIZATION_FOR_STANDALONE. |
SetHandle(IntPtr, JniHandleOwnership) |
Sets the Handle property. (Inherited from Object) |
ToArray<T>() | (Inherited from Object) |
ToString() |
Returns a string representation of the object. (Inherited from Object) |
UnregisterFromRuntime() | (Inherited from Object) |
Wait() |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>. (Inherited from Object) |
Wait(Int64, Int32) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Wait(Int64) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Explicit Interface Implementations
IJavaPeerable.Disposed() | (Inherited from Object) |
IJavaPeerable.DisposeUnlessReferenced() | (Inherited from Object) |
IJavaPeerable.Finalized() | (Inherited from Object) |
IJavaPeerable.JniManagedPeerState | (Inherited from Object) |
IJavaPeerable.SetJniIdentityHashCode(Int32) | (Inherited from Object) |
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) | (Inherited from Object) |
IJavaPeerable.SetPeerReference(JniObjectReference) | (Inherited from Object) |
Extension Methods
JavaCast<TResult>(IJavaObject) |
Performs an Android runtime-checked type conversion. |
JavaCast<TResult>(IJavaObject) | |
GetJniTypeName(IJavaPeerable) |
Gets the JNI name of the type of the instance |
JavaAs<TResult>(IJavaPeerable) |
Try to coerce |
TryJavaCast<TResult>(IJavaPeerable, TResult) |
Try to coerce |