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