
Format Date and Time Examples
Overview
The ICU DateFormat interface enables you to format a date in milliseconds into a string representation of the date. Also, the interface enables you to parse the string back to the internal date representation in milliseconds.
C++
DateFormat* df = DateFormat::createDateInstance(); UnicodeString myString; UDate myDateArr[] = { 0.0, 100000000.0, 2000000000.0 }; for (int32_t i = 0; i < 3; ++i) { myString.remove(); cout << df->format( myDateArr[i], myString ) << endl; } |
C
/* 1st example: format the dates in millis 100000000 and 2000000000 */ UErrorCode status=U_ZERO_ERROR; int32_t i, myStrlen=0; UChar* myString; UDate myDateArr[] = { 0.0, 100000000.0, 2000000000.0 }; // test values UDateFormat* df = udat_open(UCAL_DEFAULT, UCAL_DEFAULT, NULL, "GMT", &status); for (i = 0; i < 3; ++i) { myStrlen = udat_format(df, myDateArr[i], NULL, myStrlen, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; myString=(UChar*)malloc(sizeof(UChar) * (myStrlen+1) ); udat_format(df, myDateArr[i], myString, myStrlen+1, NULL, &status); printf("%s\n", austrdup(myString) ); /* austrdup( a function used to convert UChar* to char*) */ free(myString); } } |
To parse a date for a different locale, specify it in the locale call. This call creates a formatting object.
C++
DateFormat* df = DateFormat::createDateInstance ( DateFormat::SHORT, Locale::getFrance()); |
C
/* 2nd example: parse a date with short French date/time formatter */ UDateFormat* df = udat_open(UDAT_SHORT, UDAT_SHORT, "fr_FR", "GMT", &status); UErrorCode status = U_ZERO_ERROR; int32_t parsepos=0; UDate myDate = udat_parse(df, myString, u_strlen(myString), &parsepos, &status); |
To get specific fields of a date, you can use the FieldPosition function for C++ or UFieldPosition function for C.
C++
UErrorCode status = U_ZERO_ERROR; FieldPosition pos(DateFormat::YEAR_FIELD) UDate myDate = Calendar::getNow(); UnicodeString str; DateFormat* df = DateFormat::createDateInstance ( DateFormat::LONG, Locale::getFrance()); df->format(myDate, str, pos, status); cout << pos.getBeginIndex() << "," << pos. getEndIndex() << endl; |
C
UErrorCode status = U_ZERO_ERROR; UFieldPosition pos; UChar *myString; int32_t myStrlen = 0; char buffer[1024]; pos.field = 1; /* Same as the DateFormat::EField enum */ UDateFormat* dfmt = udat_open(UCAL_DEFAULT, UCAL_DEFAULT, NULL, "PST", &status); myStrlen = udat_format(dfmt, myDate, NULL, myStrlen, &pos, &status); if (status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; myString=(UChar*)malloc(sizeof(UChar) * (myStrlen+1) ); udat_format(dfmt, myDate, myString, myStrlen+1, &pos, &status); } printf("date format: %s\n", u_austrcpy(buffer, myString)); buffer[pos.endIndex] = 0; // NULL terminate the string. printf("UFieldPosition position equals %s\n", &buffer[pos.beginIndex]); |
DateTimePatternGenerator
This class lets you get a different variety of patterns, such as month+day. The following illustrates this in Java.
// set up the generator DateTimePatternGenerator generator = DateTimePatternGenerator.getInstance(locale); // get a pattern for an abbreviated month and day final String pattern = generator.getBestPattern("MMMd"); SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale); // use it to format (or parse) String formatted = formatter.format(new Date()); // for French, the result is "13 sept." |
It also contains some helper functions for parsing patterns. Here's an example of replacing the kind of timezone used in a pattern.
/** * Replace the zone string with a different type, eg v's for z's, etc. * <p>Called with a pattern, such as one gotten from * <pre> * String pattern = ((SimpleDateFormat) * DateFormat.getTimeInstance(style, locale)).toPattern(); * </pre> * @param pattern original pattern to change, such as "HH:mm zzzz" * @param newZone Must be: z, zzzz, Z, ZZZZ, v, vvvv, V, or VVVV * @return */ public String replaceZoneString(String pattern, String newZone) { final List itemList = formatParser.set(pattern).getItems(); boolean found = false; for (int i = 0; i < itemList.size(); ++i) { Object item = itemList.get(i); if (item instanceof VariableField) { // the first character of the variable field determines the type, // according to CLDR. String variableField = item.toString(); switch (variableField.charAt(0)) { case 'z': case 'Z': case 'v': case 'V': if (!variableField.equals(newZone)) { found = true; itemList.set(i, new VariableField(newZone)); } break; } } } return found ? formatParser.toString() : pattern; } |
Copyright (c) 2000 - 2007 IBM and Others - PDF Version - Feedback: http://icu-project.org/contacts.html
User Guide for ICU v3.8 Generated 2007-09-14.