1 /* 2 ******************************************************************************* 3 * Copyright (C) 2001-2011, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7 8 /** 9 * Port From: ICU4C v1.8.1 : format : DateFormatMiscTests 10 * Source File: $ICU4CRoot/source/test/intltest/miscdtfm.cpp 11 **/ 12 13 package com.ibm.icu.dev.test.format; 14 15 import java.text.FieldPosition; 16 import java.text.ParseException; 17 import java.util.Date; 18 import java.util.Locale; 19 20 import com.ibm.icu.text.DateFormatSymbols; 21 import com.ibm.icu.text.SimpleDateFormat; 22 23 /** 24 * Performs miscellaneous tests for DateFormat, SimpleDateFormat, DateFormatSymbols 25 **/ 26 public class DateFormatMiscTests extends com.ibm.icu.dev.test.TestFmwk { 27 28 public static void main(String[] args) throws Exception{ 29 new DateFormatMiscTests().run(args); 30 } 31 32 /* 33 * @bug 4097450 34 */ 35 public void Test4097450() { 36 // 37 // Date parse requiring 4 digit year. 38 // 39 String dstring[] = { 40 "97", "1997", "97", "1997", "01", "2001", "01", "2001", 41 "1", "1", "11", "11", "111", "111"}; 42 43 String dformat[] = 44 { 45 "yy", "yy", "yyyy", "yyyy", "yy", "yy", "yyyy", "yyyy", 46 "yy", "yyyy", "yy", "yyyy", "yy", "yyyy"}; 47 48 SimpleDateFormat formatter; 49 SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy"); 50 logln("Format\tSource\tResult"); 51 logln("-------\t-------\t-------"); 52 for (int i = 0; i < dstring.length ; i++) { 53 log(dformat[i] + "\t" + dstring[i] + "\t"); 54 formatter = new SimpleDateFormat(dformat[i]); 55 try { 56 StringBuffer str = new StringBuffer(""); 57 FieldPosition pos = new FieldPosition(0); 58 logln(resultFormatter.format(formatter.parse(dstring[i]), str, pos).toString()); 59 } 60 catch (ParseException exception) { 61 errln("exception --> " + exception); 62 } 63 logln(""); 64 } 65 } 66 67 /* @Bug 4099975 68 * SimpleDateFormat constructor SimpleDateFormat(String, DateFormatSymbols) 69 * should clone the DateFormatSymbols parameter 70 */ 71 public void Test4099975new() { 72 Date d = new Date(); 73 //test SimpleDateFormat Constructor 74 { 75 DateFormatSymbols symbols = new DateFormatSymbols(Locale.US); 76 SimpleDateFormat df = new SimpleDateFormat("E hh:mm", symbols); 77 SimpleDateFormat dfClone = (SimpleDateFormat) df.clone(); 78 79 logln(df.toLocalizedPattern()); 80 String s0 = df.format(d); 81 String s_dfClone = dfClone.format(d); 82 83 symbols.setLocalPatternChars("abcdefghijklmonpqr"); // change value of field 84 logln(df.toLocalizedPattern()); 85 String s1 = df.format(d); 86 87 if (!s1.equals(s0) || !s1.equals(s_dfClone)) { 88 errln("Constructor: the formats are not equal"); 89 } 90 if (!df.equals(dfClone)) { 91 errln("The Clone Object does not equal with the orignal source"); 92 } 93 } 94 //test SimpleDateFormat.setDateFormatSymbols() 95 { 96 DateFormatSymbols symbols = new DateFormatSymbols(Locale.US); 97 SimpleDateFormat df = new SimpleDateFormat("E hh:mm"); 98 df.setDateFormatSymbols(symbols); 99 SimpleDateFormat dfClone = (SimpleDateFormat) df.clone(); 100 101 logln(df.toLocalizedPattern()); 102 String s0 = df.format(d); 103 String s_dfClone = dfClone.format(d); 104 105 symbols.setLocalPatternChars("abcdefghijklmonpqr"); // change value of field 106 logln(df.toLocalizedPattern()); 107 String s1 = df.format(d); 108 109 if (!s1.equals(s0) || !s1.equals(s_dfClone)) { 110 errln("setDateFormatSymbols: the formats are not equal"); 111 } 112 if (!df.equals(dfClone)) { 113 errln("The Clone Object does not equal with the orignal source"); 114 } 115 } 116 } 117 118 /* 119 * @bug 4117335 120 */ 121 public void Test4117335() { 122 final String bc = "\u7D00\u5143\u524D"; 123 final String ad = "\u897f\u66a6"; 124 final String jstLong = "\u65e5\u672c\u6a19\u6e96\u6642"; 125 final String jdtLong = "\u65e5\u672c\u590f\u6642\u9593"; 126 final String jstShort = "JST"; 127 final String jdtShort = "JDT"; 128 final String tzID = "Asia/Tokyo"; 129 130 DateFormatSymbols symbols = new DateFormatSymbols(Locale.JAPAN); 131 final String[] eras = symbols.getEraNames(); 132 133 assertEquals("BC =", bc, eras[0]); 134 assertEquals("AD =", ad, eras[1]); 135 136 // don't use hard-coded index! 137 final String zones[][] = symbols.getZoneStrings(); 138 int index = -1; 139 for (int i = 0; i < zones.length; ++i) { 140 if (tzID.equals(zones[i][0])) { 141 index = i; 142 break; 143 } 144 } 145 146 if (index == -1) { 147 errln("could not find " + tzID); 148 } else { 149 assertEquals("Long zone name = ", jstLong, zones[index][1]); 150 assertEquals("Short zone name = ", jstShort, zones[index][2]); 151 assertEquals("Long zone name (3) = ", jdtLong, zones[index][3]); 152 assertEquals("Short zone name (4) = ", jdtShort, zones[index][4]); 153 } 154 } 155 } 156