Logo Search packages:      
Sourcecode: icu version File versions  Download package

DateFormat Class Reference

#include <datefmt.h>

Inheritance diagram for DateFormat:

Format UObject UMemory RelativeDateFormat SimpleDateFormat

List of all members.


Detailed Description

DateFormat is an abstract class for a family of classes that convert dates and times from their internal representations to textual form and back again in a language-independent manner. Converting from the internal representation (milliseconds since midnight, January 1, 1970) to text is known as "formatting," and converting from text to millis is known as "parsing." We currently define only one concrete subclass of DateFormat: SimpleDateFormat, which can handle pretty much all normal date formatting and parsing actions.

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:

 
      DateFormat* dfmt = DateFormat::createDateInstance();
      UDate myDate = Calendar::getNow();
      UnicodeString myString;
      myString = dfmt->format( myDate, myString );
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::createDateInstance();
      UnicodeString myString;
      UDate myDateArr[] = { 0.0, 100000000.0, 2000000000.0 }; // test values
      for (int32_t i = 0; i < 3; ++i) {
          myString.remove();
          cout << df->format( myDateArr[i], myString ) << endl;
      }
To get specific fields of a date, you can use UFieldPosition to get specific fields.
 
      DateFormat* dfmt = DateFormat::createDateInstance();
      FieldPosition pos(DateFormat::YEAR_FIELD);
      UnicodeString myString;
      myString = dfmt->format( myDate, myString );
      cout << myString << endl;
      cout << pos.getBeginIndex() << "," << pos. getEndIndex() << endl;
To format a date for a different Locale, specify it in the call to createDateInstance().
 
 
You can use a DateFormat to parse also.
 
       UErrorCode status = U_ZERO_ERROR;
       UDate myDate = df->parse(myString, status);
Use createDateInstance() to produce the normal date format for that country. There are other static factory methods available. Use createTimeInstance() to produce the normal time format for that country. Use createDateTimeInstance() to produce a DateFormat that formats both date and time. 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: 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 chck getDynamicClassID() before carrying out the cast.

You can also use forms of the parse and format methods with ParsePosition and FieldPosition to allow you to

User subclasses are not supported. While clients may write subclasses, such code will not necessarily work and will not be guaranteed to work stably from release to release.

Definition at line 138 of file datefmt.h.


Public Types

enum  EField {
  kEraField = UDAT_ERA_FIELD, kYearField = UDAT_YEAR_FIELD, kMonthField = UDAT_MONTH_FIELD, kDateField = UDAT_DATE_FIELD,
  kHourOfDay1Field = UDAT_HOUR_OF_DAY1_FIELD, kHourOfDay0Field = UDAT_HOUR_OF_DAY0_FIELD, kMinuteField = UDAT_MINUTE_FIELD, kSecondField = UDAT_SECOND_FIELD,
  kMillisecondField = UDAT_FRACTIONAL_SECOND_FIELD, kDayOfWeekField = UDAT_DAY_OF_WEEK_FIELD, kDayOfYearField = UDAT_DAY_OF_YEAR_FIELD, kDayOfWeekInMonthField = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
  kWeekOfYearField = UDAT_WEEK_OF_YEAR_FIELD, kWeekOfMonthField = UDAT_WEEK_OF_MONTH_FIELD, kAmPmField = UDAT_AM_PM_FIELD, kHour1Field = UDAT_HOUR1_FIELD,
  kHour0Field = UDAT_HOUR0_FIELD, kTimezoneField = UDAT_TIMEZONE_FIELD, kYearWOYField = UDAT_YEAR_WOY_FIELD, kDOWLocalField = UDAT_DOW_LOCAL_FIELD,
  kExtendedYearField = UDAT_EXTENDED_YEAR_FIELD, kJulianDayField = UDAT_JULIAN_DAY_FIELD, kMillisecondsInDayField = UDAT_MILLISECONDS_IN_DAY_FIELD, ERA_FIELD = UDAT_ERA_FIELD,
  YEAR_FIELD = UDAT_YEAR_FIELD, MONTH_FIELD = UDAT_MONTH_FIELD, DATE_FIELD = UDAT_DATE_FIELD, HOUR_OF_DAY1_FIELD = UDAT_HOUR_OF_DAY1_FIELD,
  HOUR_OF_DAY0_FIELD = UDAT_HOUR_OF_DAY0_FIELD, MINUTE_FIELD = UDAT_MINUTE_FIELD, SECOND_FIELD = UDAT_SECOND_FIELD, MILLISECOND_FIELD = UDAT_FRACTIONAL_SECOND_FIELD,
  DAY_OF_WEEK_FIELD = UDAT_DAY_OF_WEEK_FIELD, DAY_OF_YEAR_FIELD = UDAT_DAY_OF_YEAR_FIELD, DAY_OF_WEEK_IN_MONTH_FIELD = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD, WEEK_OF_YEAR_FIELD = UDAT_WEEK_OF_YEAR_FIELD,
  WEEK_OF_MONTH_FIELD = UDAT_WEEK_OF_MONTH_FIELD, AM_PM_FIELD = UDAT_AM_PM_FIELD, HOUR1_FIELD = UDAT_HOUR1_FIELD, HOUR0_FIELD = UDAT_HOUR0_FIELD,
  TIMEZONE_FIELD = UDAT_TIMEZONE_FIELD
}
enum  EStyle {
  kNone = -1, kFull = 0, kLong = 1, kMedium = 2,
  kShort = 3, kDateOffset = kShort + 1, kDateTime = 8, kDateTimeOffset = kDateTime + 1,
  kRelative = (1 << 7), kFullRelative = (kFull | kRelative), kLongRelative = kLong | kRelative, kMediumRelative = kMedium | kRelative,
  kShortRelative = kShort | kRelative, kDefault = kMedium, FULL = kFull, LONG = kLong,
  MEDIUM = kMedium, SHORT = kShort, DEFAULT = kDefault, DATE_OFFSET = kDateOffset,
  NONE = kNone, DATE_TIME = kDateTime
}

Public Member Functions

virtual void adoptCalendar (Calendar *calendarToAdopt)
virtual void adoptNumberFormat (NumberFormat *formatToAdopt)
virtual void adoptTimeZone (TimeZone *zoneToAdopt)
virtual Formatclone () const =0
UnicodeStringformat (const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
UnicodeStringformat (UDate date, UnicodeString &appendTo) const
UnicodeStringformat (UDate date, UnicodeString &appendTo, FieldPosition &fieldPosition) const
virtual UnicodeStringformat (Calendar &cal, UnicodeString &appendTo, FieldPosition &fieldPosition) const =0
virtual UnicodeStringformat (const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
virtual const CalendargetCalendar (void) const
virtual UClassID getDynamicClassID () const =0
Locale getLocale (ULocDataLocaleType type, UErrorCode &status) const
const char * getLocaleID (ULocDataLocaleType type, UErrorCode &status) const
virtual const NumberFormatgetNumberFormat (void) const
virtual const TimeZonegetTimeZone (void) const
virtual UBool isLenient (void) const
UBool operator!= (const Format &other) const
virtual UBool operator== (const Format &) const
UDate parse (const UnicodeString &text, ParsePosition &pos) const
virtual void parse (const UnicodeString &text, Calendar &cal, ParsePosition &pos) const =0
virtual UDate parse (const UnicodeString &text, UErrorCode &status) const
void parseObject (const UnicodeString &source, Formattable &result, UErrorCode &status) const
virtual void parseObject (const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const
virtual void setCalendar (const Calendar &newCalendar)
virtual void setLenient (UBool lenient)
virtual void setNumberFormat (const NumberFormat &newNumberFormat)
virtual void setTimeZone (const TimeZone &zone)
virtual ~DateFormat ()

Static Public Member Functions

static DateFormat *U_EXPORT2 createDateInstance (EStyle style=kDefault, const Locale &aLocale=Locale::getDefault())
static DateFormat *U_EXPORT2 createDateTimeInstance (EStyle dateStyle=kDefault, EStyle timeStyle=kDefault, const Locale &aLocale=Locale::getDefault())
static DateFormat *U_EXPORT2 createInstance (void)
static DateFormat *U_EXPORT2 createTimeInstance (EStyle style=kDefault, const Locale &aLocale=Locale::getDefault())
static const Locale *U_EXPORT2 getAvailableLocales (int32_t &count)
static void U_EXPORT2 operator delete (void *, void *) U_NO_THROW
static void U_EXPORT2 operator delete (void *p) U_NO_THROW
static void U_EXPORT2 operator delete[] (void *p) U_NO_THROW
static void *U_EXPORT2 operator new (size_t, void *ptr) U_NO_THROW
static void *U_EXPORT2 operator new (size_t size) U_NO_THROW
static void *U_EXPORT2 operator new[] (size_t size) U_NO_THROW

Protected Member Functions

 DateFormat (const DateFormat &)
 DateFormat ()
DateFormatoperator= (const DateFormat &)
void setLocaleIDs (const char *valid, const char *actual)

Static Protected Member Functions

static void syntaxError (const UnicodeString &pattern, int32_t pos, UParseError &parseError)

Protected Attributes

CalendarfCalendar
NumberFormatfNumberFormat

Static Private Member Functions

static DateFormat *U_EXPORT2 create (EStyle timeStyle, EStyle dateStyle, const Locale &)

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index