2 *******************************************************************************
3 * Copyright (C) 2001-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Port From: ICU4C v1.8.1 : format : DateFormatMiscTests
10 * Source File: $ICU4CRoot/source/test/intltest/miscdtfm.cpp
13 package com.ibm.icu.dev.test.format;
15 import java.text.FieldPosition;
16 import java.text.ParseException;
17 import java.util.Date;
18 import java.util.Locale;
20 import com.ibm.icu.text.DateFormatSymbols;
21 import com.ibm.icu.text.SimpleDateFormat;
24 * Performs miscellaneous tests for DateFormat, SimpleDateFormat, DateFormatSymbols
26 public class DateFormatMiscTests extends com.ibm.icu.dev.test.TestFmwk {
28 public static void main(String[] args) throws Exception{
29 new DateFormatMiscTests().run(args);
35 public void Test4097450() {
37 // Date parse requiring 4 digit year.
40 "97", "1997", "97", "1997", "01", "2001", "01", "2001",
41 "1", "1", "11", "11", "111", "111"};
45 "yy", "yy", "yyyy", "yyyy", "yy", "yy", "yyyy", "yyyy",
46 "yy", "yyyy", "yy", "yyyy", "yy", "yyyy"};
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]);
56 StringBuffer str = new StringBuffer("");
57 FieldPosition pos = new FieldPosition(0);
58 logln(resultFormatter.format(formatter.parse(dstring[i]), str, pos).toString());
60 catch (ParseException exception) {
61 errln("exception --> " + exception);
68 * SimpleDateFormat constructor SimpleDateFormat(String, DateFormatSymbols)
69 * should clone the DateFormatSymbols parameter
71 public void Test4099975new() {
73 //test SimpleDateFormat Constructor
75 DateFormatSymbols symbols = new DateFormatSymbols(Locale.US);
76 SimpleDateFormat df = new SimpleDateFormat("E hh:mm", symbols);
77 SimpleDateFormat dfClone = (SimpleDateFormat) df.clone();
79 logln(df.toLocalizedPattern());
80 String s0 = df.format(d);
81 String s_dfClone = dfClone.format(d);
83 symbols.setLocalPatternChars("abcdefghijklmonpqr"); // change value of field
84 logln(df.toLocalizedPattern());
85 String s1 = df.format(d);
87 if (!s1.equals(s0) || !s1.equals(s_dfClone)) {
88 errln("Constructor: the formats are not equal");
90 if (!df.equals(dfClone)) {
91 errln("The Clone Object does not equal with the orignal source");
94 //test SimpleDateFormat.setDateFormatSymbols()
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();
101 logln(df.toLocalizedPattern());
102 String s0 = df.format(d);
103 String s_dfClone = dfClone.format(d);
105 symbols.setLocalPatternChars("abcdefghijklmonpqr"); // change value of field
106 logln(df.toLocalizedPattern());
107 String s1 = df.format(d);
109 if (!s1.equals(s0) || !s1.equals(s_dfClone)) {
110 errln("setDateFormatSymbols: the formats are not equal");
112 if (!df.equals(dfClone)) {
113 errln("The Clone Object does not equal with the orignal source");
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";
130 DateFormatSymbols symbols = new DateFormatSymbols(Locale.JAPAN);
131 final String[] eras = symbols.getEraNames();
133 assertEquals("BC =", bc, eras[0]);
134 assertEquals("AD =", ad, eras[1]);
136 // don't use hard-coded index!
137 final String zones[][] = symbols.getZoneStrings();
139 for (int i = 0; i < zones.length; ++i) {
140 if (tzID.equals(zones[i][0])) {
147 errln("could not find " + tzID);
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]);