2 *******************************************************************************
3 * Copyright (C) 2013, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.samples.text.datetimepatterngenerator;
8 // ---getBestPatternExample
11 import com.ibm.icu.text.DateFormat;
12 import com.ibm.icu.text.DateTimePatternGenerator;
13 import com.ibm.icu.text.SimpleDateFormat;
14 import com.ibm.icu.util.GregorianCalendar;
15 import com.ibm.icu.util.TimeZone;
16 import com.ibm.icu.util.ULocale;
17 // ---getBestPatternExample
19 * com.ibm.icu.text.DateTimePatternGenerator Sample Code
21 public class DateTimePatternGeneratorSample {
23 public static void main (String[] args) {
24 getBestPatternExample();
26 replaceFieldTypesExample();
29 public static void getBestPatternExample() {
30 System.out.println("========================================================================");
31 System.out.println(" getBestPatternExample()");
33 System.out.println(" Use DateTimePatternGenerator to create customized date/time pattern:");
34 System.out.println(" yQQQQ,yMMMM, MMMMd, hhmm, jjmm per locale");
35 System.out.println("========================================================================");
36 // ---getBestPatternExample
37 final String[] skeletons = {
38 "yQQQQ", // year + full name of quarter, i.e., 4th quarter 1999
39 "yMMMM", // year + full name of month, i.e., October 1999
40 "MMMMd", // full name of month + day of the month, i.e., October 25
41 "hhmm", // 12-hour-cycle format, i.e., 1:32 PM
42 "jjmm" // preferred hour format for the given locale, i.e., 24-hour-cycle format for fr_FR
44 final ULocale[] locales = {
45 new ULocale ("en_US"),
46 new ULocale ("fr_FR"),
47 new ULocale ("zh_CN"),
49 DateTimePatternGenerator dtfg = null;
50 Date date= new GregorianCalendar(1999,9,13,23,58,59).getTime();
51 System.out.printf("%-20s%-35s%-35s%-35s\n\n", "Skeleton", "en_US", "fr_FR","zh_CN");
52 for (String skeleton:skeletons) {
53 System.out.printf("%-20s", skeleton);
54 for (ULocale locale:locales) {
55 // create a DateTimePatternGenerator instance for given locale
56 dtfg = DateTimePatternGenerator.getInstance(locale);
57 // use getBestPattern method to get the best pattern for the given skeleton
58 String pattern = dtfg.getBestPattern(skeleton);
59 // Constructs a SimpleDateFormat with the best pattern generated above and the given locale
60 SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
61 // Get the format of the given date
62 System.out.printf("%-35s",sdf.format(date));
64 System.out.println("\n");
66 /** output of the sample code:
67 *************************************************************************************************************
68 Skeleton en_US fr_FR zh_CN
70 yQQQQ 4th quarter 1999 4e trimestre 1999 1999年第四季度
72 yMMMM October 1999 octobre 1999 1999年10月
74 MMMMd October 13 13 octobre 10月13日
76 hhmm 11:58 PM 11:58 PM 下午11:58
78 jjmm 11:58 PM 23:58 下午11:58
80 **************************************************************************************************************/
81 // Use DateTime.getPatternInstance to produce the same Date/Time format with predefined constant field value
82 final String[] dtfskeleton = {
83 DateFormat.YEAR_QUARTER, // year + full name of quarter, i.e., 4th quarter 1999
84 DateFormat.YEAR_MONTH, // year + full name of month, i.e., October 1999
85 DateFormat.MONTH_DAY // full name of month + day of the month, i.e., October 25
87 System.out.printf("%-20s%-35s%-35s%-35s\n\n", "Skeleton", "en_US", "fr_FR","zh_CN");
88 for (String skeleton:dtfskeleton) {
89 System.out.printf("%-20s", skeleton);
90 for (ULocale locale:locales) {
91 // Use DateFormat.getPatternInstance to get the date/time format for the locale,
92 // and apply the format to the given date
93 String df=DateFormat.getPatternInstance(skeleton,locale).format(date);
94 System.out.printf("%-35s",df);
96 System.out.println("\n");
99 /** output of the sample code:
100 ************************************************************************************************************
101 Skeleton en_US fr_FR zh_CN
103 yQQQQ 4th quarter 1999 4e trimestre 1999 1999年第四季度
105 yMMMM October 1999 octobre 1999 1999年10月
107 MMMMd October 13 13 octobre 10月13日
108 ************************************************************************************************************/
109 // ---getBestPatternExample
112 public static void addPatternExample() {
113 System.out.println("========================================================================");
114 System.out.println(" addPatternExample()");
115 System.out.println();
116 System.out.println(" Use addPattern API to add new '. von' to existing pattern");
117 System.out.println("========================================================================");
118 // ---addPatternExample
119 Date date= new GregorianCalendar(1999,9,13,23,58,59).getTime();
120 ULocale locale = ULocale.FRANCE;
121 // Create an DateTimePatternGenerator instance for the given locale
122 DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(locale);
123 SimpleDateFormat format = new SimpleDateFormat(gen.getBestPattern("MMMMddHmm"), locale);
124 DateTimePatternGenerator.PatternInfo returnInfo = new DateTimePatternGenerator.PatternInfo();
125 // Add '. von' to the existing pattern
126 gen.addPattern("dd'. von' MMMM", true, returnInfo);
127 // Apply the new pattern
128 format.applyPattern(gen.getBestPattern("MMMMddHmm"));
129 System.out.println("New Pattern for FRENCH: "+format.toPattern());
130 System.out.println("Date Time in new Pattern: "+format.format(date));
132 /** output of the sample code:
133 **************************************************************************************************
134 New Pattern for FRENCH: dd. 'von' MMMM HH:mm
135 Date Time in new Pattern: 13. von octobre 23:58
137 *************************************************************************************************/
138 // ---addPatternExample
141 public static void replaceFieldTypesExample() {
142 // Use repalceFieldTypes API to replace zone 'zzzz' with 'vvvv'
143 System.out.println("========================================================================");
144 System.out.println(" replaceFieldTypeExample()");
145 System.out.println();
146 System.out.println(" Use replaceFieldTypes API to replace zone 'zzzz' with 'vvvv");
147 System.out.println("========================================================================");
148 // ---replaceFieldTypesExample
149 Date date= new GregorianCalendar(1999,9,13,23,58,59).getTime();
150 TimeZone zone = TimeZone.getTimeZone("Europe/Paris");
151 ULocale locale = ULocale.FRANCE;
152 DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(locale);
153 SimpleDateFormat format = new SimpleDateFormat("EEEE d MMMM y HH:mm:ss zzzz",locale);
154 format.setTimeZone(zone);
155 String pattern = format.toPattern();
156 System.out.println("Pattern before replacement:");
157 System.out.println(pattern);
158 System.out.println("Date/Time format in fr_FR:");
159 System.out.println(format.format(date));
160 // Replace zone "zzzz" in the pattern with "vvvv"
161 String newPattern = gen.replaceFieldTypes(pattern, "vvvv");
162 // Apply the new pattern
163 format.applyPattern(newPattern);
164 System.out.println("Pattern after replacement:");
165 System.out.println(newPattern);
166 System.out.println("Date/Time format in fr_FR:");
167 System.out.println(format.format(date));
169 /** output of the sample code:
170 ***************************************************************************************************
171 Pattern before replacement:
172 EEEE d MMMM y HH:mm:ss zzzz
173 Date/Time format in fr_FR:
174 jeudi 14 octobre 1999 05:58:59 heure avancée d’Europe centrale
175 Pattern after replacement:
176 EEEE d MMMM y HH:mm:ss vvvv
177 Date/Time format in fr_FR:
178 jeudi 14 octobre 1999 05:58:59 heure de l’Europe centrale
180 **************************************************************************************************/
181 // ---replaceFieldTypesExample