Logo Search packages:      
Sourcecode: icu version File versions

DateFormat Class Reference

#include <datefmt.h>

Inheritance diagram for DateFormat:

Format 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) {
          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().
       DateFormat* df =
           DateFormat::createDateInstance( DateFormat::SHORT, Locale::getFrance());
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

Definition at line 124 of file datefmt.h.

Public Types

enum  EField {
  kEraField = 0, kYearField, kMonthField, kDateField,
  kHourOfDay1Field, kHourOfDay0Field, kMinuteField, kSecondField,
  kMillisecondField, kDayOfWeekField, kDayOfYearField, kDayOfWeekInMonthField,
  kWeekOfYearField, kWeekOfMonthField, kAmPmField, kHour1Field,
  kHour0Field, kTimezoneField, kYearWOYField, kDOWLocalField,
  ERA_FIELD = kEraField, YEAR_FIELD = kYearField, MONTH_FIELD = kMonthField, DATE_FIELD = kDateField,
  HOUR_OF_DAY1_FIELD = kHourOfDay1Field, HOUR_OF_DAY0_FIELD = kHourOfDay0Field, MINUTE_FIELD = kMinuteField, SECOND_FIELD = kSecondField,
  MILLISECOND_FIELD = kMillisecondField, DAY_OF_WEEK_FIELD = kDayOfWeekField, DAY_OF_YEAR_FIELD = kDayOfYearField, DAY_OF_WEEK_IN_MONTH_FIELD = kDayOfWeekInMonthField,
  WEEK_OF_YEAR_FIELD = kWeekOfYearField, WEEK_OF_MONTH_FIELD = kWeekOfMonthField, AM_PM_FIELD = kAmPmField, HOUR1_FIELD = kHour1Field,
  HOUR0_FIELD = kHour0Field, TIMEZONE_FIELD = kTimezoneField
enum  EStyle {
  kNone = -1, kFull = 0, kLong = 1, kMedium = 2,
  kShort = 3, kDateOffset = kShort + 1, kDateTime = 8, 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 &result, UErrorCode &status) const
UnicodeStringformat (UDate date, UnicodeString &result) const
UnicodeStringformat (UDate date, UnicodeString &toAppendTo, FieldPosition &fieldPosition) const
virtual UnicodeStringformat (Calendar &cal, UnicodeString &toAppendTo, FieldPosition &fieldPosition) const =0
virtual UnicodeStringformat (const Formattable &obj, UnicodeString &toAppendTo, FieldPosition &pos, UErrorCode &status) const
virtual const CalendargetCalendar (void) const
virtual UClassID getDynamicClassID () const =0
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 DateFormatcreateDateInstance (EStyle style=kDefault, const Locale &aLocale=Locale::getDefault())
static DateFormatcreateDateTimeInstance (EStyle dateStyle=kDefault, EStyle timeStyle=kDefault, const Locale &aLocale=Locale::getDefault())
static DateFormatcreateInstance (void)
static DateFormatcreateTimeInstance (EStyle style=kDefault, const Locale &aLocale=Locale::getDefault())
static const Locale * getAvailableLocales (int32_t &count)

Protected Member Functions

 DateFormat (const DateFormat &)
 DateFormat ()
DateFormatoperator= (const DateFormat &)
void syntaxError (const UnicodeString &pattern, int32_t pos, UParseError &parseError)

Protected Attributes


Static Private Member Functions

static DateFormatcreate (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