001: /*
002: *******************************************************************************
003: * Copyright (C) 2001-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: /*
009: * New added, 2001-10-17 [Jing/GCL]
010: */
011:
012: package com.ibm.icu.dev.test.format;
013:
014: import com.ibm.icu.text.*;
015: import com.ibm.icu.util.*;
016: import java.util.Date;
017: import java.text.ParseException;
018: import java.text.ParsePosition;
019: import java.util.Locale;
020:
021: public class DateFormatRegressionTestJ extends
022: com.ibm.icu.dev.test.TestFmwk {
023:
024: private static final String TIME_STRING = "2000/11/17 08:01:00";
025: private static final long UTC_LONG = 974476860000L;
026: private static SimpleDateFormat sdf_;
027:
028: protected void init() throws Exception {
029: sdf_ = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
030:
031: }
032:
033: public static void main(String[] args) throws Exception {
034: new DateFormatRegressionTestJ().run(args);
035: }
036:
037: //Return value of getAmPmStrings
038: public void Test4103926() {
039: String act_Ampms[];
040: String exp_Ampms[] = { "AM", "PM" };
041: Locale.setDefault(Locale.US);
042:
043: DateFormatSymbols dfs = new DateFormatSymbols();
044: act_Ampms = dfs.getAmPmStrings();
045: if (act_Ampms.length != exp_Ampms.length) {
046: errln("The result is not expected!");
047: } else {
048: for (int i = 0; i < act_Ampms.length; i++) {
049: if (!act_Ampms[i].equals(exp_Ampms[i]))
050: errln("The result is not expected!");
051: }
052: }
053: }
054:
055: //Missing digit in millisecone format in SimpleDateFormat
056: public void Test4148168() {
057: Date d = new Date(1002705212906l);
058: String[] ISOPattern = { "''yyyy-MM-dd-hh.mm.ss.S''",
059: "''yyyy-MM-dd-hh.mm.ss.SS''",
060: "''yyyy-MM-dd-hh.mm.ss.SSS''",
061: "''yyyy-MM-dd-hh.mm.ss.SSSS''",
062: "''yyyy-MM-dd-hh.mm.ss.SSSSS''",
063: "''yyyy-MM-dd-hh.mm.ss.SSSSSS''",
064: "''yyyy-MM-dd-hh.mm.ss.SSSSSSS''",
065: "''yyyy-MM-dd-hh.mm.ss.SSS000''" };
066: SimpleDateFormat aSimpleDF = (SimpleDateFormat) DateFormat
067: .getDateTimeInstance();
068:
069: for (int i = 0; i < ISOPattern.length; i++) {
070: aSimpleDF.applyPattern(ISOPattern[i]);
071: logln("Pattern = " + aSimpleDF.toPattern());
072: logln("Format = " + aSimpleDF.format(d));
073: }
074: }
075:
076: //DateFormat getDateTimeInstance(int, int), invalid styles no exception
077: public void Test4213086() {
078: Date someDate = new Date();
079: String d = null;
080: try {
081: DateFormat df2 = DateFormat.getDateTimeInstance(2, -2);
082: d = df2.format(someDate);
083: errln("we should catch an exception here");
084: } catch (Exception e) {
085: logln("dateStyle = 2" + "\t timeStyle = -2");
086: logln("Exception caught!");
087: }
088:
089: try {
090: DateFormat df3 = DateFormat.getDateTimeInstance(4, 2);
091: d = df3.format(someDate);
092: errln("we should catch an exception here");
093: } catch (Exception e) {
094: logln("dateStyle = 4" + "\t timeStyle = 2");
095: logln("Exception caught!");
096: logln("********************************************");
097: }
098:
099: try {
100: DateFormat df4 = DateFormat.getDateTimeInstance(-12, -12);
101: d = df4.format(someDate);
102: errln("we should catch an exception here");
103: } catch (Exception e) {
104: logln("dateStyle = -12" + "\t timeStyle = -12");
105: logln("Exception caught!");
106: logln("********************************************");
107: }
108:
109: try {
110: DateFormat df5 = DateFormat.getDateTimeInstance(2, 123);
111: d = df5.format(someDate);
112: errln("we should catch an exception here");
113: } catch (Exception e) {
114: logln("dateStyle = 2" + "\t timeStyle = 123");
115: logln("Exception caught!");
116: logln("********************************************");
117: }
118: //read the value in d to get rid of the warning
119: if (d != null) {
120: logln("The value of d: " + d);
121: }
122: }
123:
124: //DateFormat.format works wrongly?
125: public void Test4250359() {
126: Locale.setDefault(Locale.US);
127: Calendar cal = Calendar.getInstance();
128: cal.clear();
129: cal.set(101 + 1900, 9, 9, 17, 53);
130: Date d = cal.getTime();
131: DateFormat tf = DateFormat.getTimeInstance(DateFormat.SHORT);
132: String act_result = tf.format(d);
133: String exp_result = "5:53 PM";
134:
135: if (!act_result.equals(exp_result)) {
136: errln("The result is not expected");
137: }
138: }
139:
140: //pattern "s.S, parse '1ms'"
141: public void Test4253490() {
142: Date d = new Date(1002705212231l);
143:
144: String[] ISOPattern = { "''yyyy-MM-dd-hh.mm.ss.S''",
145: "''yyyy-MM-dd-hh.mm.ss.SS''",
146: "''yyyy-MM-dd-hh.mm.ss.SSS''",
147: "''yyyy-MM-dd-hh.mm.ss.SSSS''",
148: "''yyyy-MM-dd-hh.mm.ss.SSSSS''",
149: "''yyyy-MM-dd-hh.mm.ss.SSSSSS''",
150: "''yyyy-MM-dd-hh.mm.ss.SSSSSSS''" };
151:
152: SimpleDateFormat aSimpleDF = (SimpleDateFormat) DateFormat
153: .getDateTimeInstance();
154: for (int i = 0; i < ISOPattern.length; i++) {
155: aSimpleDF.applyPattern(ISOPattern[i]);
156: logln("Pattern = " + aSimpleDF.toPattern());
157: logln("Format = " + aSimpleDF.format(d));
158: }
159: }
160:
161: //about regression test
162: public void Test4266432() {
163: Locale.setDefault(Locale.JAPAN);
164: Locale loc = Locale.getDefault();
165: String dateFormat = "MM/dd/yy HH:mm:ss zzz";
166: SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
167:
168: ParsePosition p0 = new ParsePosition(0);
169: logln("Under " + loc + " locale");
170: Date d = fmt.parse("01/22/92 04:52:00 GMT", p0);
171: logln(d.toString());
172: }
173:
174: //SimpleDateFormat inconsistent for number of digits for years
175: public void Test4358730() {
176: SimpleDateFormat sdf = new SimpleDateFormat();
177: Calendar cal = Calendar.getInstance();
178: cal.clear();
179: cal.set(2001, 11, 10);
180: Date today = cal.getTime();
181:
182: sdf.applyPattern("MM d y");
183: logln(sdf.format(today));
184: sdf.applyPattern("MM d yy");
185: logln(sdf.format(today));
186:
187: sdf.applyPattern("MM d yyy");
188: logln(sdf.format(today));
189:
190: sdf.applyPattern("MM d yyyy");
191: logln(sdf.format(today));
192:
193: sdf.applyPattern("MM d yyyyy");
194: logln(sdf.format(today));
195: }
196:
197: //Parse invalid string
198: public void Test4375399() {
199: final String pattern = new String(
200: "yyyy.MM.dd G 'at' hh:mm:ss z");
201: SimpleDateFormat sdf = new SimpleDateFormat(pattern,
202: Locale.JAPAN);
203: try {
204: Date currentTime = sdf
205: .parse(
206: "vggf 20 01.0 9.29 ap. J.-C. at 05:26:33 GMT+08:00",
207: new ParsePosition(0));
208: if (currentTime == null)
209: logln("parse right");
210: } catch (Exception e) {
211: errln("Error");
212: }
213: }
214:
215: /*
216: public void Test4407042() {
217: DateParseThread d1 = new DateParseThread();
218: DateFormatThread d2 = new DateFormatThread();
219: d1.start();
220: d2.start();
221: try {
222: logln("test");
223: Thread.sleep(1000);
224: } catch (Exception e) {}
225: }*/
226:
227: public void Test4468663() {
228: Date d = new Date(-93716671115767l);
229: String origin_d = d.toString();
230: String str;
231: final String pattern = new String("EEEE, MMMM d, yyyy");
232: SimpleDateFormat sdf = new SimpleDateFormat(pattern);
233:
234: if (sdf.getTimeZone().useDaylightTime()) {
235: logln("original date: " + origin_d.toString());
236: str = sdf.format(d);
237: logln(" after format----->" + str);
238:
239: d = sdf.parse(str, new ParsePosition(0));
240: logln(" after parse----->" + d.toString());
241:
242: str = sdf.format(d);
243: logln(" after format----->" + str);
244:
245: d = sdf.parse(str, new ParsePosition(0));
246: logln(" after parse----->" + d.toString());
247:
248: str = sdf.format(d);
249: logln(" after format----->" + str);
250: }
251: }
252:
253: //Class used by Test4407042
254: class DateParseThread extends Thread {
255: public void run() {
256: SimpleDateFormat sdf = (SimpleDateFormat) sdf_.clone();
257: TimeZone defaultTZ = TimeZone.getDefault();
258: TimeZone PST = TimeZone.getTimeZone("PST");
259: int defaultOffset = defaultTZ.getRawOffset();
260: int PSTOffset = PST.getRawOffset();
261: int offset = defaultOffset - PSTOffset;
262: long ms = UTC_LONG - offset;
263: try {
264: int i = 0;
265: while (i < 10000) {
266: Date date = sdf.parse(TIME_STRING);
267: long t = date.getTime();
268: i++;
269: if (t != ms) {
270: throw new ParseException("Parse Error: " + i
271: + " (" + sdf.format(date) + ") " + t
272: + " != " + ms, 0);
273: }
274: }
275: } catch (Exception e) {
276: errln("parse error: " + e.getMessage());
277: }
278: }
279: }
280:
281: //Class used by Test4407042
282: class DateFormatThread extends Thread {
283: public void run() {
284: SimpleDateFormat sdf = (SimpleDateFormat) sdf_.clone();
285: TimeZone tz = TimeZone.getTimeZone("PST");
286: sdf.setTimeZone(tz);
287: int i = 0;
288: while (i < 10000) {
289: i++;
290: String s = sdf.format(new Date(UTC_LONG));
291: if (!s.equals(TIME_STRING)) {
292: errln("Format Error: " + i + " " + s + " != "
293: + TIME_STRING);
294: }
295: }
296: }
297: }
298:
299: }
|