001: /*
002: *******************************************************************************
003: * Copyright (C) 2001-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: /**
009: * Port From: ICU4C v1.8.1 : format : DateFormatMiscTests
010: * Source File: $ICU4CRoot/source/test/intltest/miscdtfm.cpp
011: **/package com.ibm.icu.dev.test.format;
012:
013: import com.ibm.icu.text.*;
014:
015: import java.text.FieldPosition;
016: import java.text.ParseException;
017: import java.util.Locale;
018: import java.util.Date;
019:
020: /**
021: * Performs miscellaneous tests for DateFormat, SimpleDateFormat, DateFormatSymbols
022: **/
023: public class DateFormatMiscTests extends com.ibm.icu.dev.test.TestFmwk {
024:
025: public static void main(String[] args) throws Exception {
026: new DateFormatMiscTests().run(args);
027: }
028:
029: /*
030: * @bug 4097450
031: */
032: public void Test4097450() {
033: //
034: // Date parse requiring 4 digit year.
035: //
036: String dstring[] = { "97", "1997", "97", "1997", "01", "2001",
037: "01", "2001", "1", "1", "11", "11", "111", "111" };
038:
039: String dformat[] = { "yy", "yy", "yyyy", "yyyy", "yy", "yy",
040: "yyyy", "yyyy", "yy", "yyyy", "yy", "yyyy", "yy",
041: "yyyy" };
042:
043: SimpleDateFormat formatter;
044: SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy");
045: logln("Format\tSource\tResult");
046: logln("-------\t-------\t-------");
047: for (int i = 0; i < dstring.length; i++) {
048: log(dformat[i] + "\t" + dstring[i] + "\t");
049: formatter = new SimpleDateFormat(dformat[i]);
050: try {
051: StringBuffer str = new StringBuffer("");
052: FieldPosition pos = new FieldPosition(0);
053: logln(resultFormatter.format(
054: formatter.parse(dstring[i]), str, pos)
055: .toString());
056: } catch (ParseException exception) {
057: errln("exception --> " + exception);
058: }
059: logln("");
060: }
061: }
062:
063: /* @Bug 4099975
064: * SimpleDateFormat constructor SimpleDateFormat(String, DateFormatSymbols)
065: * should clone the DateFormatSymbols parameter
066: */
067: public void Test4099975new() {
068: Date d = new Date();
069: //test SimpleDateFormat Constructor
070: {
071: DateFormatSymbols symbols = new DateFormatSymbols(Locale.US);
072: SimpleDateFormat df = new SimpleDateFormat("E hh:mm",
073: symbols);
074: SimpleDateFormat dfClone = (SimpleDateFormat) df.clone();
075:
076: logln(df.toLocalizedPattern());
077: String s0 = df.format(d);
078: String s_dfClone = dfClone.format(d);
079:
080: symbols.setLocalPatternChars("abcdefghijklmonpqr"); // change value of field
081: logln(df.toLocalizedPattern());
082: String s1 = df.format(d);
083:
084: if (!s1.equals(s0) || !s1.equals(s_dfClone)) {
085: errln("Constructor: the formats are not equal");
086: }
087: if (!df.equals(dfClone)) {
088: errln("The Clone Object does not equal with the orignal source");
089: }
090: }
091: //test SimpleDateFormat.setDateFormatSymbols()
092: {
093: DateFormatSymbols symbols = new DateFormatSymbols(Locale.US);
094: SimpleDateFormat df = new SimpleDateFormat("E hh:mm");
095: df.setDateFormatSymbols(symbols);
096: SimpleDateFormat dfClone = (SimpleDateFormat) df.clone();
097:
098: logln(df.toLocalizedPattern());
099: String s0 = df.format(d);
100: String s_dfClone = dfClone.format(d);
101:
102: symbols.setLocalPatternChars("abcdefghijklmonpqr"); // change value of field
103: logln(df.toLocalizedPattern());
104: String s1 = df.format(d);
105:
106: if (!s1.equals(s0) || !s1.equals(s_dfClone)) {
107: errln("setDateFormatSymbols: the formats are not equal");
108: }
109: if (!df.equals(dfClone)) {
110: errln("The Clone Object does not equal with the orignal source");
111: }
112: }
113: }
114:
115: /*
116: * @bug 4117335
117: */
118: public void Test4117335() {
119: final String bc = "\u7D00\u5143\u524D";
120: final String ad = "\u897f\u66a6";
121: final String jstLong = "\u65e5\u672c\u6a19\u6e96\u6642";
122: final String jdtLong = "\u65e5\u672c\u590f\u6642\u9593";
123: final String jstShort = "JST";
124: final String jdtShort = "JDT";
125: final String tzID = "Asia/Tokyo";
126:
127: DateFormatSymbols symbols = new DateFormatSymbols(Locale.JAPAN);
128: final String[] eras = symbols.getEras();
129:
130: assertEquals("BC =", bc, eras[0]);
131: assertEquals("AD =", ad, eras[1]);
132:
133: // don't use hard-coded index!
134: final String zones[][] = symbols.getZoneStrings();
135: int index = -1;
136: for (int i = 0; i < zones.length; ++i) {
137: if (tzID.equals(zones[i][0])) {
138: index = i;
139: break;
140: }
141: }
142:
143: if (index == -1) {
144: errln("could not find " + tzID);
145: } else {
146: assertEquals("Long zone name = ", jstLong, zones[index][1]);
147: assertEquals("Short zone name = ", jstShort,
148: zones[index][2]);
149: assertEquals("Long zone name (3) = ", jdtLong,
150: zones[index][3]);
151: assertEquals("Short zone name (4) = ", jdtShort,
152: zones[index][4]);
153: }
154: }
155: }
|