68 * Clients are encouraged to create a date-time formatter using DateFormat::getInstance(), 69 * getDateInstance(), getDateInstance(), or getDateTimeInstance() rather than 70 * explicitly constructing an instance of SimpleDateFormat. This way, the client 71 * is guaranteed to get an appropriate formatting pattern for whatever locale the 72 * program is running in. However, if the client needs something more unusual than 73 * the default patterns in the locales, he can construct a SimpleDateFormat directly 74 * and give it an appropriate pattern (or use one of the factory methods on DateFormat 75 * and modify the pattern after the fact with toPattern() and applyPattern(). 76 * 77 *
Date and Time Patterns:
Date and time formats are specified by date and time pattern strings. 80 * Within date and time pattern strings, all unquoted ASCII letters [A-Za-z] are reserved 81 * as pattern letters representing calendar fields. SimpleDateFormat supports 82 * the date and time formatting algorithm and pattern letters defined by 83 * UTS#35 84 * Unicode Locale Data Markup Language (LDML) and further documented for ICU in the 85 * ICU 86 * User Guide. The following pattern letters are currently available (note that the actual 87 * values depend on CLDR and may change from the examples shown here):
SimpleDateFormat
630 * Any characters in the pattern that are not in the ranges of ['a'..'z'] and 631 * ['A'..'Z'] will be treated as quoted text. For instance, characters 632 * like ':', '.', ' ', '#' and '@' will appear in the resulting time text 633 * even they are not embraced within single quotes. 634 *
635 * A pattern containing any invalid pattern letter will result in a failing 636 * UErrorCode result during formatting or parsing. 637 *
638 * Examples using the US locale: 639 *
640 * \code 641 * Format Pattern Result 642 * -------------- ------- 643 * "yyyy.MM.dd G 'at' HH:mm:ss vvvv" ->> 1996.07.10 AD at 15:08:56 Pacific Time 644 * "EEE, MMM d, ''yy" ->> Wed, July 10, '96 645 * "h:mm a" ->> 12:08 PM 646 * "hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time 647 * "K:mm a, vvv" ->> 0:00 PM, PT 648 * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 1996.July.10 AD 12:08 PM 649 * \endcode 650 *
653 * \code 654 * UErrorCode success = U_ZERO_ERROR; 655 * SimpleTimeZone* pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST"); 656 * pdt->setStartRule( Calendar::APRIL, 1, Calendar::SUNDAY, 2*60*60*1000); 657 * pdt->setEndRule( Calendar::OCTOBER, -1, Calendar::SUNDAY, 2*60*60*1000); 658 * 659 * // Format the current time. 660 * SimpleDateFormat* formatter 661 * = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz", success ); 662 * GregorianCalendar cal(success); 663 * UDate currentTime_1 = cal.getTime(success); 664 * FieldPosition fp(FieldPosition::DONT_CARE); 665 * UnicodeString dateString; 666 * formatter->format( currentTime_1, dateString, fp ); 667 * cout << "result: " << dateString << endl; 668 * 669 * // Parse the previous string back into a Date. 670 * ParsePosition pp(0); 671 * UDate currentTime_2 = formatter->parse(dateString, pp ); 672 * \endcode 673 *
681 * When parsing a date string using the abbreviated year pattern ("y" or "yy"), 682 * SimpleDateFormat must interpret the abbreviated year 683 * relative to some century. It does this by adjusting dates to be 684 * within 80 years before and 20 years after the time the SimpleDateFormat 685 * instance is created. For example, using a pattern of "MM/dd/yy" and a 686 * SimpleDateFormat instance created on Jan 1, 1997, the string 687 * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" 688 * would be interpreted as May 4, 1964. 689 * During parsing, only strings consisting of exactly two digits, as defined by 690 * Unicode::isDigit(), will be parsed into the default century. 691 * Any other numeric string, such as a one digit string, a three or more digit 692 * string, or a two digit string that isn't all digits (for example, "-1"), is 693 * interpreted literally. So "01/02/3" or "01/02/003" are parsed (for the 694 * Gregorian calendar), using the same pattern, as Jan 2, 3 AD. Likewise (but 695 * only in lenient parse mode, the default) "01/02/-3" is parsed as Jan 2, 4 BC. 696 * 697 *
Unicode::isDigit()
698 * If the year pattern has more than two 'y' characters, the year is 699 * interpreted literally, regardless of the number of digits. So using the 700 * pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D. 701 * 702 *
703 * When numeric fields abut one another directly, with no intervening delimiter 704 * characters, they constitute a run of abutting numeric fields. Such runs are 705 * parsed specially. For example, the format "HHmmss" parses the input text 706 * "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and fails to 707 * parse "1234". In other words, the leftmost field of the run is flexible, 708 * while the others keep a fixed width. If the parse fails anywhere in the run, 709 * then the leftmost field is shortened by one character, and the entire run is 710 * parsed again. This is repeated until either the parse succeeds or the 711 * leftmost field is one character in length. If the parse still fails at that 712 * point, the parse of the run fails. 713 * 714 *
715 * For time zones that have no names, SimpleDateFormat uses strings GMT+hours:minutes or 716 * GMT-hours:minutes. 717 *
718 * The calendar defines what is the first day of the week, the first week of the 719 * year, whether hours are zero based or not (0 vs 12 or 24), and the timezone. 720 * There is one common number format to handle all the numbers; the digit count 721 * is handled programmatically according to the pattern. 722 * 723 *
User subclasses are not supported. While clients may write 724 * subclasses, such code will not necessarily work and will not be 725 * guaranteed to work stably from release to release. 726 */ 727 class U_I18N_API SimpleDateFormat: public DateFormat { 728 public: 729 /** 730 * Construct a SimpleDateFormat using the default pattern for the default 731 * locale. 732 *
733 * [Note:] Not all locales support SimpleDateFormat; for full generality, 734 * use the factory methods in the DateFormat class. 735 * @param status Output param set to success/failure code. 736 * @stable ICU 2.0 737 */ 738 SimpleDateFormat(UErrorCode& status); 739 740 /** 741 * Construct a SimpleDateFormat using the given pattern and the default locale. 742 * The locale is used to obtain the symbols used in formatting (e.g., the 743 * names of the months), but not to provide the pattern. 744 *
745 * [Note:] Not all locales support SimpleDateFormat; for full generality, 746 * use the factory methods in the DateFormat class. 747 * @param pattern the pattern for the format. 748 * @param status Output param set to success/failure code. 749 * @stable ICU 2.0 750 */ 751 SimpleDateFormat(const UnicodeString& pattern, 752 UErrorCode& status); 753 754 /** 755 * Construct a SimpleDateFormat using the given pattern, numbering system override, and the default locale. 756 * The locale is used to obtain the symbols used in formatting (e.g., the 757 * names of the months), but not to provide the pattern. 758 *
759 * A numbering system override is a string containing either the name of a known numbering system, 760 * or a set of field and numbering system pairs that specify which fields are to be formatted with 761 * the alternate numbering system. For example, to specify that all numeric fields in the specified 762 * date or time pattern are to be rendered using Thai digits, simply specify the numbering system override 763 * as "thai". To specify that just the year portion of the date be formatted using Hebrew numbering, 764 * use the override string "y=hebrew". Numbering system overrides can be combined using a semi-colon 765 * character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc. 766 * 767 *
768 * [Note:] Not all locales support SimpleDateFormat; for full generality, 769 * use the factory methods in the DateFormat class. 770 * @param pattern the pattern for the format. 771 * @param override the override string. 772 * @param status Output param set to success/failure code. 773 * @stable ICU 4.2 774 */ 775 SimpleDateFormat(const UnicodeString& pattern, 776 const UnicodeString& override, 777 UErrorCode& status); 778 779 /** 780 * Construct a SimpleDateFormat using the given pattern and locale. 781 * The locale is used to obtain the symbols used in formatting (e.g., the 782 * names of the months), but not to provide the pattern. 783 *
784 * [Note:] Not all locales support SimpleDateFormat; for full generality, 785 * use the factory methods in the DateFormat class. 786 * @param pattern the pattern for the format. 787 * @param locale the given locale. 788 * @param status Output param set to success/failure code. 789 * @stable ICU 2.0 790 */ 791 SimpleDateFormat(const UnicodeString& pattern, 792 const Locale& locale, 793 UErrorCode& status); 794 795 /** 796 * Construct a SimpleDateFormat using the given pattern, numbering system override, and locale. 797 * The locale is used to obtain the symbols used in formatting (e.g., the 798 * names of the months), but not to provide the pattern. 799 *
800 * A numbering system override is a string containing either the name of a known numbering system, 801 * or a set of field and numbering system pairs that specify which fields are to be formatted with 802 * the alternate numbering system. For example, to specify that all numeric fields in the specified 803 * date or time pattern are to be rendered using Thai digits, simply specify the numbering system override 804 * as "thai". To specify that just the year portion of the date be formatted using Hebrew numbering, 805 * use the override string "y=hebrew". Numbering system overrides can be combined using a semi-colon 806 * character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc. 807 *
808 * [Note:] Not all locales support SimpleDateFormat; for full generality, 809 * use the factory methods in the DateFormat class. 810 * @param pattern the pattern for the format. 811 * @param override the numbering system override. 812 * @param locale the given locale. 813 * @param status Output param set to success/failure code. 814 * @stable ICU 4.2 815 */ 816 SimpleDateFormat(const UnicodeString& pattern, 817 const UnicodeString& override, 818 const Locale& locale, 819 UErrorCode& status); 820 821 /** 822 * Construct a SimpleDateFormat using the given pattern and locale-specific 823 * symbol data. The formatter takes ownership of the DateFormatSymbols object; 824 * the caller is no longer responsible for deleting it. 825 * @param pattern the given pattern for the format. 826 * @param formatDataToAdopt the symbols to be adopted. 827 * @param status Output param set to success/faulure code. 828 * @stable ICU 2.0 829 */ 830 SimpleDateFormat(const UnicodeString& pattern, 831 DateFormatSymbols* formatDataToAdopt, 832 UErrorCode& status); 833 834 /** 835 * Construct a SimpleDateFormat using the given pattern and locale-specific 836 * symbol data. The DateFormatSymbols object is NOT adopted; the caller 837 * remains responsible for deleting it. 838 * @param pattern the given pattern for the format. 839 * @param formatData the formatting symbols to be use. 840 * @param status Output param set to success/faulure code. 841 * @stable ICU 2.0 842 */ 843 SimpleDateFormat(const UnicodeString& pattern, 844 const DateFormatSymbols& formatData, 845 UErrorCode& status); 846 847 /** 848 * Copy constructor. 849 * @stable ICU 2.0 850 */ 851 SimpleDateFormat(const SimpleDateFormat&); 852 853 /** 854 * Assignment operator. 855 * @stable ICU 2.0 856 */ 857 SimpleDateFormat& operator=(const SimpleDateFormat&); 858 859 /** 860 * Destructor. 861 * @stable ICU 2.0 862 */ 863 virtual ~SimpleDateFormat(); 864 865 /** 866 * Clone this Format object polymorphically. The caller owns the result and 867 * should delete it when done. 868 * @return A copy of the object. 869 * @stable ICU 2.0 870 */ 871 virtual SimpleDateFormat* clone() const override; 872 873 /** 874 * Return true if the given Format objects are semantically equal. Objects 875 * of different subclasses are considered unequal. 876 * @param other the object to be compared with. 877 * @return true if the given Format objects are semantically equal. 878 * @stable ICU 2.0 879 */ 880 virtual bool operator==(const Format& other) const override; 881 882 883 using DateFormat::format; 884 885 /** 886 * Format a date or time, which is the standard millis since 24:00 GMT, Jan 887 * 1, 1970. Overrides DateFormat pure virtual method. 888 *
889 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->> 890 * 1996.07.10 AD at 15:08:56 PDT 891 * 892 * @param cal Calendar set to the date and time to be formatted 893 * into a date/time string. 894 * @param appendTo Output parameter to receive result. 895 * Result is appended to existing contents. 896 * @param pos The formatting position. On input: an alignment field, 897 * if desired. On output: the offsets of the alignment field. 898 * @return Reference to 'appendTo' parameter. 899 * @stable ICU 2.1 900 */ 901 virtual UnicodeString& format( Calendar& cal, 902 UnicodeString& appendTo, 903 FieldPosition& pos) const override; 904 905 /** 906 * Format a date or time, which is the standard millis since 24:00 GMT, Jan 907 * 1, 1970. Overrides DateFormat pure virtual method. 908 *
909 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->> 910 * 1996.07.10 AD at 15:08:56 PDT 911 * 912 * @param cal Calendar set to the date and time to be formatted 913 * into a date/time string. 914 * @param appendTo Output parameter to receive result. 915 * Result is appended to existing contents. 916 * @param posIter On return, can be used to iterate over positions 917 * of fields generated by this format call. Field values 918 * are defined in UDateFormatField. 919 * @param status Input/output param set to success/failure code. 920 * @return Reference to 'appendTo' parameter. 921 * @stable ICU 4.4 922 */ 923 virtual UnicodeString& format( Calendar& cal, 924 UnicodeString& appendTo, 925 FieldPositionIterator* posIter, 926 UErrorCode& status) const override; 927 928 using DateFormat::parse; 929 930 /** 931 * Parse a date/time string beginning at the given parse position. For 932 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date 933 * that is equivalent to Date(837039928046). 934 *
935 * By default, parsing is lenient: If the input is not in the form used by 936 * this object's format method but can still be parsed as a date, then the 937 * parse succeeds. Clients may insist on strict adherence to the format by 938 * calling setLenient(false). 939 * @see DateFormat::setLenient(boolean) 940 * 941 * @param text The date/time string to be parsed 942 * @param cal A Calendar set on input to the date and time to be used for 943 * missing values in the date/time string being parsed, and set 944 * on output to the parsed date/time. When the calendar type is 945 * different from the internal calendar held by this SimpleDateFormat 946 * instance, the internal calendar will be cloned to a work 947 * calendar set to the same milliseconds and time zone as the 948 * cal parameter, field values will be parsed based on the work 949 * calendar, then the result (milliseconds and time zone) will 950 * be set in this calendar. 951 * @param pos On input, the position at which to start parsing; on 952 * output, the position at which parsing terminated, or the 953 * start position if the parse failed. 954 * @stable ICU 2.1 955 */ 956 virtual void parse( const UnicodeString& text, 957 Calendar& cal, 958 ParsePosition& pos) const override; 959 960 961 /** 962 * Set the start UDate used to interpret two-digit year strings. 963 * When dates are parsed having 2-digit year strings, they are placed within 964 * a assumed range of 100 years starting on the two digit start date. For 965 * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or 966 * some other year. SimpleDateFormat chooses a year so that the resultant 967 * date is on or after the two digit start date and within 100 years of the 968 * two digit start date. 969 *
970 * By default, the two digit start date is set to 80 years before the current 971 * time at which a SimpleDateFormat object is created. 972 * @param d start UDate used to interpret two-digit year strings. 973 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with 974 * an error value if there was a parse error. 975 * @stable ICU 2.0 976 */ 977 virtual void set2DigitYearStart(UDate d, UErrorCode& status); 978 979 /** 980 * Get the start UDate used to interpret two-digit year strings. 981 * When dates are parsed having 2-digit year strings, they are placed within 982 * a assumed range of 100 years starting on the two digit start date. For 983 * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or 984 * some other year. SimpleDateFormat chooses a year so that the resultant 985 * date is on or after the two digit start date and within 100 years of the 986 * two digit start date. 987 *
988 * By default, the two digit start date is set to 80 years before the current 989 * time at which a SimpleDateFormat object is created. 990 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with 991 * an error value if there was a parse error. 992 * @stable ICU 2.0 993 */ 994 UDate get2DigitYearStart(UErrorCode& status) const; 995 996 /** 997 * Return a pattern string describing this date format. 998 * @param result Output param to receive the pattern. 999 * @return A reference to 'result'. 1000 * @stable ICU 2.0 1001 */ 1002 virtual UnicodeString& toPattern(UnicodeString& result) const; 1003 1004 /** 1005 * Return a localized pattern string describing this date format. 1006 * In most cases, this will return the same thing as toPattern(), 1007 * but a locale can specify characters to use in pattern descriptions 1008 * in place of the ones described in this class's class documentation. 1009 * (Presumably, letters that would be more mnemonic in that locale's 1010 * language.) This function would produce a pattern using those 1011 * letters. 1012 *
1013 * Note: This implementation depends on DateFormatSymbols::getLocalPatternChars() 1014 * to get localized format pattern characters. ICU does not include 1015 * localized pattern character data, therefore, unless user sets localized 1016 * pattern characters manually, this method returns the same result as 1017 * toPattern(). 1018 * 1019 * @param result Receives the localized pattern. 1020 * @param status Output param set to success/failure code on 1021 * exit. If the pattern is invalid, this will be 1022 * set to a failure result. 1023 * @return A reference to 'result'. 1024 * @stable ICU 2.0 1025 */ 1026 virtual UnicodeString& toLocalizedPattern(UnicodeString& result, 1027 UErrorCode& status) const; 1028 1029 /** 1030 * Apply the given unlocalized pattern string to this date format. 1031 * (i.e., after this call, this formatter will format dates according to 1032 * the new pattern) 1033 * 1034 * @param pattern The pattern to be applied. 1035 * @stable ICU 2.0 1036 */ 1037 virtual void applyPattern(const UnicodeString& pattern); 1038 1039 /** 1040 * Apply the given localized pattern string to this date format. 1041 * (see toLocalizedPattern() for more information on localized patterns.) 1042 * 1043 * @param pattern The localized pattern to be applied. 1044 * @param status Output param set to success/failure code on 1045 * exit. If the pattern is invalid, this will be 1046 * set to a failure result. 1047 * @stable ICU 2.0 1048 */ 1049 virtual void applyLocalizedPattern(const UnicodeString& pattern, 1050 UErrorCode& status); 1051 1052 /** 1053 * Gets the date/time formatting symbols (this is an object carrying 1054 * the various strings and other symbols used in formatting: e.g., month 1055 * names and abbreviations, time zone names, AM/PM strings, etc.) 1056 * @return a copy of the date-time formatting data associated 1057 * with this date-time formatter. 1058 * @stable ICU 2.0 1059 */ 1060 virtual const DateFormatSymbols* getDateFormatSymbols(void) const; 1061 1062 /** 1063 * Set the date/time formatting symbols. The caller no longer owns the 1064 * DateFormatSymbols object and should not delete it after making this call. 1065 * @param newFormatSymbols the given date-time formatting symbols to copy. 1066 * @stable ICU 2.0 1067 */ 1068 virtual void adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols); 1069 1070 /** 1071 * Set the date/time formatting data. 1072 * @param newFormatSymbols the given date-time formatting symbols to copy. 1073 * @stable ICU 2.0 1074 */ 1075 virtual void setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols); 1076 1077 /** 1078 * Return the class ID for this class. This is useful only for comparing to 1079 * a return value from getDynamicClassID(). For example: 1080 *
1081 * . Base* polymorphic_pointer = createPolymorphicObject(); 1082 * . if (polymorphic_pointer->getDynamicClassID() == 1083 * . erived::getStaticClassID()) ... 1084 *
true
false