Compartir a través de


DateFormat Class

Definition

DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner.

[Android.Runtime.Register("java/text/DateFormat", DoNotGenerateAcw=true)]
public abstract class DateFormat : Java.Text._Format
[<Android.Runtime.Register("java/text/DateFormat", DoNotGenerateAcw=true)>]
type DateFormat = class
    inherit _Format
Inheritance
DateFormat
Derived
Attributes

Remarks

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 &rarr; text), parsing (text &rarr; 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: <blockquote>

{@code
            myString = DateFormat.getDateInstance().format(myDate);
            }

</blockquote>

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. <blockquote>

{@code
            DateFormat df = DateFormat.getDateInstance();
            for (int i = 0; i < myDate.length; ++i) {
                output.println(df.format(myDate[i]) + "; ");
            }
            }

</blockquote>

To format a date for a different Locale, specify it in the call to #getDateInstance(int, Locale) getDateInstance(). <blockquote>

{@code
            DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
            }

</blockquote>

You can use a DateFormat to parse also. <blockquote>

{@code
            myDate = df.parse(myString);
            }

</blockquote>

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: <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>

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>

<h2>"synchronization">Synchronization</h2>

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.

Added in 1.1.

Java documentation for java.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()

Create a new date format.

DateFormat(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Fields

AmPmField

Useful constant for AM_PM field alignment.

DateField

Useful constant for DATE field alignment.

DayOfWeekField

Useful constant for DAY_OF_WEEK field alignment.

DayOfWeekInMonthField

Useful constant for DAY_OF_WEEK_IN_MONTH field alignment.

DayOfYearField

Useful constant for DAY_OF_YEAR field alignment.

Default

Constant for default style pattern.

EraField

Useful constant for ERA field alignment.

Full

Constant for full style pattern.

Hour0Field

Useful constant for zero-based HOUR field alignment.

Hour1Field

Useful constant for one-based HOUR field alignment.

HourOfDay0Field

Useful constant for zero-based HOUR_OF_DAY field alignment.

HourOfDay1Field

Useful constant for one-based HOUR_OF_DAY field alignment.

Long

Constant for long style pattern.

Medium

Constant for medium style pattern.

MillisecondField

Useful constant for MILLISECOND field alignment.

MinuteField

Useful constant for MINUTE field alignment.

MonthField

Useful constant for MONTH field alignment.

SecondField

Useful constant for SECOND field alignment.

Short

Constant for short style pattern.

TimezoneField

Useful constant for TIMEZONE field alignment.

WeekOfMonthField

Useful constant for WEEK_OF_MONTH field alignment.

WeekOfYearField

Useful constant for WEEK_OF_YEAR field alignment.

YearField

Useful constant for YEAR field alignment.

Properties

Calendar

Gets the calendar associated with this date/time formatter. -or- Set the calendar to be used by this date format.

Class

Returns the runtime class of this Object.

(Inherited from Object)
DateInstance

Gets the date formatter with the default formatting style for the default java.util.Locale.Category#FORMAT FORMAT locale.

DateTimeInstance

Gets the date/time formatter with the default formatting style for the default java.util.Locale.Category#FORMAT FORMAT locale.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
Instance

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

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Lenient

Tell whether date/time parsing is to be lenient. -or- Specify whether or not date/time parsing is to be lenient.

NumberFormat

Gets the number formatter which this date/time formatter uses to format and parse a time. -or- Allows you to set the number formatter.

PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

TimeInstance

Gets the time formatter with the default formatting style for the default java.util.Locale.Category#FORMAT FORMAT locale.

TimeZone

Gets 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(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 the given Object into a date-time string.

Format(Object)

Formats an object to produce a string.

(Inherited from _Format)
FormatToCharacterIterator(Object)

Formats an Object producing an AttributedCharacterIterator.

(Inherited from _Format)
GetAvailableLocales()

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

GetDateInstance(Int32, Locale)

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

GetDateInstance(Int32)

Gets the date formatter with the given formatting style for the default java.util.Locale.Category#FORMAT FORMAT locale.

GetDateTimeInstance(Int32, Int32, Locale)

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

GetDateTimeInstance(Int32, Int32)

Gets the date/time formatter with the given date and time formatting styles for the default java.util.Locale.Category#FORMAT FORMAT locale.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetTimeInstance(Int32, Locale)

Gets the time formatter with the given formatting style for the given locale.

GetTimeInstance(Int32)

Gets the time formatter with the given formatting style for the default java.util.Locale.Category#FORMAT FORMAT locale.

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, ParsePosition)

Parse a date/time string according to the given parse position.

Parse(String)

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

ParseObject(String, ParsePosition)

Parses text from a string to produce a Date.

ParseObject(String)

Parses text from the beginning of the given string to produce an object.

(Inherited from _Format)
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)

Applies to