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