1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2008-2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 //! [getBestPatternExample1]
7 #include <iostream>
8 #include "unicode/smpdtfmt.h"
9 #include "unicode/dtptngen.h"
10 #include "unicode/ustdio.h"
11 //! [getBestPatternExample1]
12
13 using namespace std;
14
getBestPatternExample()15 static void getBestPatternExample() {
16
17 u_printf("========================================================================\n");
18 u_printf(" getBestPatternExample()\n");
19 u_printf("\n");
20 u_printf(" Use DateTimePatternGenerator to create customized date/time pattern:\n");
21 u_printf(" yQQQQ,yMMMM, MMMMd, hhmm, jjmm per locale\n");
22 u_printf("========================================================================\n");
23 //! [getBestPatternExample]
24 UnicodeString skeletons [] = {
25 UnicodeString("yQQQQ"), // year + full name of quarter, i.e., 4th quarter 1999
26 UnicodeString("yMMMM"), // year + full name of month, i.e., October 1999
27 UnicodeString("MMMMd"), // full name of month + day of the month, i.e., October 25
28 UnicodeString("hhmm"), // 12-hour-cycle format, i.e., 1:32 PM
29 UnicodeString("jjmm"), // preferred hour format for the given locale, i.e., 24-hour-cycle format for fr_FR
30 0,
31 };
32
33 Locale locales[] = {
34 Locale ("en_US"),
35 Locale ("fr_FR"),
36 Locale ("zh_CN"),
37 };
38
39 const char* filename = "sample.txt";
40 /* open a UTF-8 file for writing */
41 UFILE* f = u_fopen(filename, "w", NULL,"UTF-8");
42 UnicodeString dateReturned;
43 UErrorCode status =U_ZERO_ERROR;
44 Calendar *cal = Calendar::createInstance(status);
45 cal->set (1999,9,13,23,58,59);
46 UDate date = cal->getTime(status);
47 u_fprintf(f, "%-20S%-20S%-20S%-20S\n", UnicodeString("Skeleton").getTerminatedBuffer(),UnicodeString("en_US").getTerminatedBuffer(),UnicodeString("fr_FR").getTerminatedBuffer(),UnicodeString("zh_CN").getTerminatedBuffer());
48 for (int i=0;skeletons[i]!=NULL;i++) {
49 u_fprintf(f, "%-20S",skeletons[i].getTerminatedBuffer());
50 for (int j=0;j<sizeof(locales)/sizeof(locales[0]);j++) {
51 // create a DateTimePatternGenerator instance for given locale
52 DateTimePatternGenerator *dtfg= DateTimePatternGenerator::createInstance(locales[j],status);
53 // use getBestPattern method to get the best pattern for the given skeleton
54 UnicodeString pattern = dtfg->getBestPattern(skeletons[i],status);
55 // Constructs a SimpleDateFormat with the best pattern generated above and the given locale
56 SimpleDateFormat *sdf = new SimpleDateFormat(pattern,locales[j],status);
57 dateReturned.remove();
58 // Get the format of the given date
59 sdf->format(date,dateReturned,status);
60 /* write Unicode string to file */
61 u_fprintf(f, "%-20S", dateReturned.getTerminatedBuffer());
62 delete dtfg;
63 delete sdf;
64 }
65 u_fprintf(f,"\n");
66 }
67 /* close the file resource */
68 u_fclose(f);
69 delete cal;
70 //! [getBestPatternExample]
71 }
72
addPatternExample()73 static void addPatternExample() {
74
75 u_printf("========================================================================\n");
76 u_printf(" addPatternExample()\n");
77 u_printf("\n");
78 u_printf(" Use addPattern API to add new '. von' to existing pattern\n");
79 u_printf("========================================================================\n");
80 //! [addPatternExample]
81 UErrorCode status =U_ZERO_ERROR;
82 UnicodeString conflictingPattern,dateReturned, pattern;
83 Locale locale=Locale::getFrance();
84 Calendar *cal = Calendar::createInstance(status);
85 cal->set (1999,9,13,23,58,59);
86 UDate date = cal->getTime(status);
87 // Create an DateTimePatternGenerator instance for the given locale
88 DateTimePatternGenerator *dtfg= DateTimePatternGenerator::createInstance(locale,status);
89 SimpleDateFormat *sdf = new SimpleDateFormat(dtfg->getBestPattern("MMMMddHmm",status),locale,status);
90 // Add '. von' to the existing pattern
91 dtfg->addPattern("dd'. von' MMMM", true, conflictingPattern,status);
92 // Apply the new pattern
93 sdf->applyPattern(dtfg->getBestPattern("MMMMddHmm",status));
94 dateReturned = sdf->format(date, dateReturned, status);
95 pattern =sdf->toPattern(pattern);
96 u_printf("%s\n", "New Pattern for FRENCH: ");
97 u_printf("%S\n", pattern.getTerminatedBuffer());
98 u_printf("%s\n", "Date Time in new Pattern: ");
99 u_printf("%S\n", dateReturned.getTerminatedBuffer());
100 delete dtfg;
101 delete sdf;
102 delete cal;
103
104 //! [addPatternExample]
105 /* output of the sample code:
106 ************************************************************************************************
107 New Pattern for FRENCH: dd. 'von' MMMM HH:mm
108 Date Time in new Pattern: 13. von octobre 23:58
109
110 *************************************************************************************************/
111 }
112
replaceFieldTypesExample()113 static void replaceFieldTypesExample() {
114 // Use repalceFieldTypes API to replace zone 'zzzz' with 'vvvv'
115 u_printf("========================================================================\n");
116 u_printf(" replaceFieldTypeExample()\n");
117 u_printf("\n");
118 u_printf(" Use replaceFieldTypes API to replace zone 'zzzz' with 'vvvv'\n");
119 u_printf("========================================================================\n");
120 //! [replaceFieldTypesExample]
121 UFILE *out = u_finit(stdout, NULL, "UTF-8");
122 UErrorCode status =U_ZERO_ERROR;
123 UnicodeString pattern,dateReturned;
124 Locale locale =Locale::getFrance();
125 Calendar *cal = Calendar::createInstance(status);
126 cal->set (1999,9,13,23,58,59);
127 UDate date = cal->getTime(status);
128 TimeZone *zone = TimeZone::createTimeZone(UnicodeString("Europe/Paris"));
129 DateTimePatternGenerator *dtfg = DateTimePatternGenerator::createInstance(locale,status);
130 SimpleDateFormat *sdf = new SimpleDateFormat("EEEE d MMMM y HH:mm:ss zzzz",locale,status);
131 sdf->setTimeZone(*zone);
132 pattern = sdf->toPattern(pattern);
133 u_fprintf(out, "%S\n", UnicodeString("Pattern before replacement:").getTerminatedBuffer());
134 u_fprintf(out, "%S\n", pattern.getTerminatedBuffer());
135 dateReturned.remove();
136 dateReturned = sdf->format(date, dateReturned, status);
137 u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer());
138 u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer());
139 // Replace zone "zzzz" in the pattern with "vvvv"
140 UnicodeString newPattern = dtfg->replaceFieldTypes(pattern, "vvvv", status);
141 // Apply the new pattern
142 sdf->applyPattern(newPattern);
143 dateReturned.remove();
144 dateReturned = sdf->format(date, dateReturned, status);
145 u_fprintf(out, "%S\n", UnicodeString("Pattern after replacement:").getTerminatedBuffer());
146 u_fprintf(out, "%S\n", newPattern.getTerminatedBuffer());
147 u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer());
148 u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer());
149 delete sdf;
150 delete dtfg;
151 delete zone;
152 delete cal;
153 u_fclose(out);
154 //! [replaceFieldTypesExample]
155 }
156
main(int argc,char * argv[])157 int main (int argc, char* argv[])
158 {
159 getBestPatternExample();
160 addPatternExample();
161 replaceFieldTypesExample();
162 return 0;
163 }
164