0001: /*
0002: *******************************************************************************
0003: * Copyright (C) 2001-2005, International Business Machines Corporation and *
0004: * others. All Rights Reserved. *
0005: *******************************************************************************
0006: */
0007:
0008: /**
0009: * Port From: ICU4C v1.8.1 : format : DateFormatRegressionTest
0010: * Source File: $ICU4CRoot/source/test/intltest/dtfmrgts.cpp
0011: **/package com.ibm.icu.dev.test.format;
0012:
0013: import com.ibm.icu.text.*;
0014: import com.ibm.icu.util.*;
0015:
0016: import java.io.*;
0017: import java.text.FieldPosition;
0018: import java.text.Format;
0019: import java.text.ParseException;
0020: import java.text.ParsePosition;
0021: import java.util.Date;
0022: import java.util.Locale;
0023:
0024: /**
0025: * Performs regression test for DateFormat
0026: **/
0027: public class DateFormatRegressionTest extends
0028: com.ibm.icu.dev.test.TestFmwk {
0029:
0030: public static void main(String[] args) throws Exception {
0031: new DateFormatRegressionTest().run(args);
0032: }
0033:
0034: /**
0035: * @bug 4029195
0036: */
0037: public void Test4029195() {
0038: Calendar cal = Calendar.getInstance();
0039: Date today = cal.getTime();
0040: logln("today: " + today);
0041: SimpleDateFormat sdf = (SimpleDateFormat) DateFormat
0042: .getDateInstance();
0043: String pat = sdf.toPattern();
0044: logln("pattern: " + pat);
0045: StringBuffer fmtd = new StringBuffer("");
0046: FieldPosition pos = new FieldPosition(0);
0047: fmtd = sdf.format(today, fmtd, pos);
0048: logln("today: " + fmtd);
0049:
0050: sdf.applyPattern("G yyyy DDD");
0051: StringBuffer todayS = new StringBuffer("");
0052: todayS = sdf.format(today, todayS, pos);
0053: logln("today: " + todayS);
0054: try {
0055: today = sdf.parse(todayS.toString());
0056: logln("today date: " + today);
0057: } catch (Exception e) {
0058: errln("Error reparsing date: " + e.getMessage());
0059: }
0060:
0061: try {
0062: StringBuffer rt = new StringBuffer("");
0063: rt = sdf.format(sdf.parse(todayS.toString()), rt, pos);
0064: logln("round trip: " + rt);
0065: if (!rt.toString().equals(todayS.toString()))
0066: errln("Fail: Want " + todayS + " Got " + rt);
0067: } catch (ParseException e) {
0068: errln("Fail: " + e);
0069: e.printStackTrace();
0070: }
0071: }
0072:
0073: /**
0074: * @bug 4052408
0075: */
0076: public void Test4052408() {
0077:
0078: DateFormat fmt = DateFormat.getDateTimeInstance(
0079: DateFormat.SHORT, DateFormat.SHORT, Locale.US);
0080: Calendar cal = Calendar.getInstance();
0081: cal.clear();
0082: cal.set(97 + 1900, Calendar.MAY, 3, 8, 55);
0083: Date dt = cal.getTime();
0084: String str = fmt.format(dt);
0085: logln(str);
0086:
0087: if (!str.equals("5/3/97 8:55 AM"))
0088: errln("Fail: Test broken; Want 5/3/97 8:55 AM Got " + str);
0089:
0090: String expected[] = { "", //"ERA_FIELD",
0091: "97", //"YEAR_FIELD",
0092: "5", //"MONTH_FIELD",
0093: "3", //"DATE_FIELD",
0094: "", //"HOUR_OF_DAY1_FIELD",
0095: "", //"HOUR_OF_DAY0_FIELD",
0096: "55", //"MINUTE_FIELD",
0097: "", //"SECOND_FIELD",
0098: "", //"MILLISECOND_FIELD",
0099: "", //"DAY_OF_WEEK_FIELD",
0100: "", //"DAY_OF_YEAR_FIELD",
0101: "", //"DAY_OF_WEEK_IN_MONTH_FIELD",
0102: "", //"WEEK_OF_YEAR_FIELD",
0103: "", //"WEEK_OF_MONTH_FIELD",
0104: "AM", //"AM_PM_FIELD",
0105: "8", //"HOUR1_FIELD",
0106: "", //"HOUR0_FIELD",
0107: "" //"TIMEZONE_FIELD"
0108: };
0109: String fieldNames[] = { "ERA_FIELD", "YEAR_FIELD",
0110: "MONTH_FIELD", "DATE_FIELD", "HOUR_OF_DAY1_FIELD",
0111: "HOUR_OF_DAY0_FIELD", "MINUTE_FIELD", "SECOND_FIELD",
0112: "MILLISECOND_FIELD", "DAY_OF_WEEK_FIELD",
0113: "DAY_OF_YEAR_FIELD", "DAY_OF_WEEK_IN_MONTH_FIELD",
0114: "WEEK_OF_YEAR_FIELD", "WEEK_OF_MONTH_FIELD",
0115: "AM_PM_FIELD", "HOUR1_FIELD", "HOUR0_FIELD",
0116: "TIMEZONE_FIELD" };
0117:
0118: boolean pass = true;
0119: for (int i = 0; i <= 17; ++i) {
0120: FieldPosition pos = new FieldPosition(i);
0121: StringBuffer buf = new StringBuffer("");
0122: fmt.format(dt, buf, pos);
0123: //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
0124: String dst = buf.substring(pos.getBeginIndex(), pos
0125: .getEndIndex());
0126: str = dst;
0127: log(i + ": " + fieldNames[i] + ", \"" + str + "\", "
0128: + pos.getBeginIndex() + ", " + pos.getEndIndex());
0129: String exp = expected[i];
0130: if ((exp.length() == 0 && str.length() == 0)
0131: || str.equals(exp))
0132: logln(" ok");
0133: else {
0134: logln(" expected " + exp);
0135: pass = false;
0136: }
0137: }
0138: if (!pass)
0139: errln("Fail: FieldPosition not set right by DateFormat");
0140: }
0141:
0142: /**
0143: * @bug 4056591
0144: * Verify the function of the [s|g]et2DigitYearStart() API.
0145: */
0146: public void Test4056591() {
0147:
0148: try {
0149: SimpleDateFormat fmt = new SimpleDateFormat("yyMMdd",
0150: Locale.US);
0151: Calendar cal = Calendar.getInstance();
0152: cal.clear();
0153: cal.set(1809, Calendar.DECEMBER, 25);
0154: Date start = cal.getTime();
0155: fmt.set2DigitYearStart(start);
0156: if ((fmt.get2DigitYearStart() != start))
0157: errln("get2DigitYearStart broken");
0158: cal.clear();
0159: cal.set(1809, Calendar.DECEMBER, 25);
0160: Date d1 = cal.getTime();
0161: cal.clear();
0162: cal.set(1909, Calendar.DECEMBER, 24);
0163: Date d2 = cal.getTime();
0164: cal.clear();
0165: cal.set(1809, Calendar.DECEMBER, 26);
0166: Date d3 = cal.getTime();
0167: cal.clear();
0168: cal.set(1861, Calendar.DECEMBER, 25);
0169: Date d4 = cal.getTime();
0170:
0171: Date dates[] = { d1, d2, d3, d4 };
0172:
0173: String strings[] = { "091225", "091224", "091226", "611225" };
0174:
0175: for (int i = 0; i < 4; i++) {
0176: String s = strings[i];
0177: Date exp = dates[i];
0178: Date got = fmt.parse(s);
0179: logln(s + " . " + got + "; exp " + exp);
0180: if (got.getTime() != exp.getTime())
0181: errln("set2DigitYearStart broken");
0182: }
0183: } catch (ParseException e) {
0184: errln("Fail: " + e);
0185: e.printStackTrace();
0186: }
0187: }
0188:
0189: /**
0190: * @bug 4059917
0191: */
0192: public void Test4059917() {
0193: SimpleDateFormat fmt;
0194: String myDate;
0195: fmt = new SimpleDateFormat("yyyy/MM/dd");
0196: myDate = "1997/01/01";
0197: aux917(fmt, myDate);
0198: fmt = new SimpleDateFormat("yyyyMMdd");
0199: myDate = "19970101";
0200: aux917(fmt, myDate);
0201: }
0202:
0203: public void aux917(SimpleDateFormat fmt, String str) {
0204:
0205: String pat = fmt.toPattern();
0206: logln("==================");
0207: logln("testIt: pattern=" + pat + " string=" + str);
0208: ParsePosition pos = new ParsePosition(0);
0209: Object o = fmt.parseObject(str, pos);
0210: //logln( UnicodeString("Parsed object: ") + o );
0211:
0212: StringBuffer formatted = new StringBuffer("");
0213: FieldPosition poss = new FieldPosition(0);
0214: formatted = fmt.format(o, formatted, poss);
0215:
0216: logln("Formatted string: " + formatted);
0217: if (!formatted.toString().equals(str))
0218: errln("Fail: Want " + str + " Got " + formatted);
0219: }
0220:
0221: /**
0222: * @bug 4060212
0223: */
0224: public void Test4060212() {
0225: String dateString = "1995-040.05:01:29";
0226: logln("dateString= " + dateString);
0227: logln("Using yyyy-DDD.hh:mm:ss");
0228: SimpleDateFormat formatter = new SimpleDateFormat(
0229: "yyyy-DDD.hh:mm:ss");
0230: ParsePosition pos = new ParsePosition(0);
0231: Date myDate = formatter.parse(dateString, pos);
0232: DateFormat fmt = DateFormat.getDateTimeInstance(
0233: DateFormat.FULL, DateFormat.LONG);
0234: String myString = fmt.format(myDate);
0235: logln(myString);
0236: Calendar cal = new GregorianCalendar();
0237: cal.setTime(myDate);
0238: if ((cal.get(Calendar.DAY_OF_YEAR) != 40))
0239: errln("Fail: Got " + cal.get(Calendar.DAY_OF_YEAR)
0240: + " Want 40");
0241:
0242: logln("Using yyyy-ddd.hh:mm:ss");
0243: formatter = new SimpleDateFormat("yyyy-ddd.hh:mm:ss");
0244: pos.setIndex(0);
0245: myDate = formatter.parse(dateString, pos);
0246: myString = fmt.format(myDate);
0247: logln(myString);
0248: cal.setTime(myDate);
0249: if ((cal.get(Calendar.DAY_OF_YEAR) != 40))
0250: errln("Fail: Got " + cal.get(Calendar.DAY_OF_YEAR)
0251: + " Want 40");
0252: }
0253:
0254: /**
0255: * @bug 4061287
0256: */
0257: public void Test4061287() {
0258:
0259: SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
0260: try {
0261: logln(df.parse("35/01/1971").toString());
0262: } catch (ParseException e) {
0263: errln("Fail: " + e);
0264: e.printStackTrace();
0265: }
0266: df.setLenient(false);
0267: boolean ok = false;
0268: try {
0269: logln(df.parse("35/01/1971").toString());
0270: } catch (ParseException e) {
0271: ok = true;
0272: }
0273: if (!ok)
0274: errln("Fail: Lenient not working");
0275: }
0276:
0277: /**
0278: * @bug 4065240
0279: */
0280: public void Test4065240() {
0281: Date curDate;
0282: DateFormat shortdate, fulldate;
0283: String strShortDate, strFullDate;
0284: Locale saveLocale = Locale.getDefault();
0285: TimeZone saveZone = TimeZone.getDefault();
0286:
0287: try {
0288: Locale curLocale = new Locale("de", "DE");
0289: Locale.setDefault(curLocale);
0290: // {sfb} adoptDefault instead of setDefault
0291: //TimeZone.setDefault(TimeZone.createTimeZone("EST"));
0292: TimeZone.setDefault(TimeZone.getTimeZone("EST"));
0293: Calendar cal = Calendar.getInstance();
0294: cal.clear();
0295: cal.set(98 + 1900, 0, 1);
0296: curDate = cal.getTime();
0297: shortdate = DateFormat.getDateInstance(DateFormat.SHORT);
0298: fulldate = DateFormat.getDateTimeInstance(DateFormat.LONG,
0299: DateFormat.LONG);
0300: strShortDate = "The current date (short form) is ";
0301: String temp;
0302: temp = shortdate.format(curDate);
0303: strShortDate += temp;
0304: strFullDate = "The current date (long form) is ";
0305: String temp2 = fulldate.format(curDate);
0306: strFullDate += temp2;
0307:
0308: logln(strShortDate);
0309: logln(strFullDate);
0310:
0311: // {sfb} What to do with resource bundle stuff?????
0312:
0313: // Check to see if the resource is present; if not, we can't test
0314: //ResourceBundle bundle = //The variable is never used
0315: // ICULocaleData.getBundle("DateFormatZoneData", curLocale);
0316:
0317: // {sfb} API change to ResourceBundle -- add getLocale()
0318: /*if (bundle.getLocale().getLanguage().equals("de")) {
0319: // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
0320: if (!strFullDate.endsWith("GMT-05:00"))
0321: errln("Fail: Want GMT-05:00");
0322: } else {
0323: logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
0324: logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
0325: }*/
0326: } catch (Exception e) {
0327: logln(e.getMessage());
0328: } finally {
0329: Locale.setDefault(saveLocale);
0330: TimeZone.setDefault(saveZone);
0331: }
0332:
0333: }
0334:
0335: /*
0336: DateFormat.equals is too narrowly defined. As a result, MessageFormat
0337: does not work correctly. DateFormat.equals needs to be written so
0338: that the Calendar sub-object is not compared using Calendar.equals,
0339: but rather compared for equivalency. This may necessitate adding a
0340: (package private) method to Calendar to test for equivalency.
0341:
0342: Currently this bug breaks MessageFormat.toPattern
0343: */
0344: /**
0345: * @bug 4071441
0346: */
0347: public void Test4071441() {
0348: DateFormat fmtA = DateFormat.getInstance();
0349: DateFormat fmtB = DateFormat.getInstance();
0350:
0351: // {sfb} Is it OK to cast away const here?
0352: Calendar calA = fmtA.getCalendar();
0353: Calendar calB = fmtB.getCalendar();
0354: calA.clear();
0355: calA.set(1900, 0, 0);
0356: calB.clear();
0357: calB.set(1900, 0, 0);
0358: if (!calA.equals(calB))
0359: errln("Fail: Can't complete test; Calendar instances unequal");
0360: if (!fmtA.equals(fmtB))
0361: errln("Fail: DateFormat unequal when Calendars equal");
0362: calB.clear();
0363: calB.set(1961, Calendar.DECEMBER, 25);
0364: if (calA.equals(calB))
0365: errln("Fail: Can't complete test; Calendar instances equal");
0366: if (!fmtA.equals(fmtB))
0367: errln("Fail: DateFormat unequal when Calendars equivalent");
0368: logln("DateFormat.equals ok");
0369: }
0370:
0371: /* The java.text.DateFormat.parse(String) method expects for the
0372: US locale a string formatted according to mm/dd/yy and parses it
0373: correctly.
0374:
0375: When given a string mm/dd/yyyy it only parses up to the first
0376: two y's, typically resulting in a date in the year 1919.
0377:
0378: Please extend the parsing method(s) to handle strings with
0379: four-digit year values (probably also applicable to various
0380: other locales. */
0381: /**
0382: * @bug 4073003
0383: */
0384: public void Test4073003() {
0385: try {
0386: DateFormat fmt = DateFormat.getDateInstance(
0387: DateFormat.SHORT, Locale.US);
0388: String tests[] = { "12/25/61", "12/25/1961", "4/3/2010",
0389: "4/3/10" };
0390: for (int i = 0; i < 4; i += 2) {
0391: Date d = fmt.parse(tests[i]);
0392: Date dd = fmt.parse(tests[i + 1]);
0393: String s;
0394: s = fmt.format(d);
0395: String ss;
0396: ss = fmt.format(dd);
0397: if (d.getTime() != dd.getTime())
0398: errln("Fail: " + d + " != " + dd);
0399: if (!s.equals(ss))
0400: errln("Fail: " + s + " != " + ss);
0401: logln("Ok: " + s + " " + d);
0402: }
0403: } catch (ParseException e) {
0404: errln("Fail: " + e);
0405: e.printStackTrace();
0406: }
0407: }
0408:
0409: /**
0410: * @bug 4089106
0411: */
0412: public void Test4089106() {
0413: TimeZone def = TimeZone.getDefault();
0414: try {
0415: TimeZone z = new SimpleTimeZone((int) (1.25 * 3600000),
0416: "FAKEZONE");
0417: TimeZone.setDefault(z);
0418: SimpleDateFormat f = new SimpleDateFormat();
0419: if (!f.getTimeZone().equals(z))
0420: errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
0421: } finally {
0422: TimeZone.setDefault(def);
0423: }
0424: }
0425:
0426: /**
0427: * @bug 4100302
0428: */
0429: public void Test4100302() {
0430:
0431: Locale locales[] = { Locale.CANADA, Locale.CANADA_FRENCH,
0432: Locale.CHINA, Locale.CHINESE, Locale.ENGLISH,
0433: Locale.FRANCE, Locale.FRENCH, Locale.GERMAN,
0434: Locale.GERMANY, Locale.ITALIAN, Locale.ITALY,
0435: Locale.JAPAN, Locale.JAPANESE, Locale.KOREA,
0436: Locale.KOREAN, Locale.PRC, Locale.SIMPLIFIED_CHINESE,
0437: Locale.TAIWAN, Locale.TRADITIONAL_CHINESE, Locale.UK,
0438: Locale.US };
0439: try {
0440: boolean pass = true;
0441: for (int i = 0; i < 21; i++) {
0442: Format format = DateFormat.getDateTimeInstance(
0443: DateFormat.FULL, DateFormat.FULL, locales[i]);
0444: byte[] bytes;
0445: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0446: ObjectOutputStream oos = new ObjectOutputStream(baos);
0447: oos.writeObject(format);
0448: oos.flush();
0449: baos.close();
0450: bytes = baos.toByteArray();
0451: ObjectInputStream ois = new ObjectInputStream(
0452: new ByteArrayInputStream(bytes));
0453: Object o = ois.readObject();
0454: if (!format.equals(o)) {
0455: pass = false;
0456: logln("DateFormat instance for locale "
0457: + locales[i]
0458: + " is incorrectly serialized/deserialized.");
0459: } else {
0460: logln("DateFormat instance for locale "
0461: + locales[i] + " is OKAY.");
0462: }
0463: }
0464: if (!pass)
0465: errln("Fail: DateFormat serialization/equality bug");
0466: } catch (OptionalDataException e) {
0467: errln("Fail: " + e);
0468: } catch (IOException e) {
0469: errln("Fail: " + e);
0470: } catch (ClassNotFoundException e) {
0471: errln("Fail: " + e);
0472: }
0473:
0474: }
0475:
0476: /**
0477: * @bug 4101483
0478: */
0479: public void Test4101483() {
0480: SimpleDateFormat sdf = new SimpleDateFormat("z", Locale.US);
0481: FieldPosition fp = new FieldPosition(DateFormat.TIMEZONE_FIELD);
0482: Date d = new Date(9234567890L);
0483: StringBuffer buf = new StringBuffer("");
0484: sdf.format(d, buf, fp);
0485: logln(sdf.format(d, buf, fp).toString());
0486: logln("beginIndex = " + fp.getBeginIndex());
0487: logln("endIndex = " + fp.getEndIndex());
0488: if (fp.getBeginIndex() == fp.getEndIndex())
0489: errln("Fail: Empty field");
0490: }
0491:
0492: /**
0493: * @bug 4103340
0494: * @bug 4138203
0495: * This bug really only works in Locale.US, since that's what the locale
0496: * used for Date.toString() is. Bug 4138203 reports that it fails on Korean
0497: * NT; it would actually have failed on any non-US locale. Now it should
0498: * work on all locales.
0499: */
0500: public void Test4103340() {
0501:
0502: // choose a date that is the FIRST of some month
0503: // and some arbitrary time
0504: Calendar cal = Calendar.getInstance();
0505: cal.clear();
0506: cal.set(1997, 3, 1, 1, 1, 1);
0507: Date d = cal.getTime();
0508: SimpleDateFormat df = new SimpleDateFormat("MMMM", Locale.US);
0509: String s = d.toString();
0510: StringBuffer s2 = new StringBuffer("");
0511: FieldPosition pos = new FieldPosition(0);
0512: s2 = df.format(d, s2, pos);
0513: logln("Date=" + s);
0514: logln("DF=" + s2);
0515: String substr = s2.substring(0, 2);
0516: if (s.indexOf(substr) == -1)
0517: errln("Months should match");
0518: }
0519:
0520: /**
0521: * @bug 4103341
0522: */
0523: public void Test4103341() {
0524: TimeZone saveZone = TimeZone.getDefault();
0525: try {
0526: // {sfb} changed from adoptDefault to setDefault
0527: TimeZone.setDefault(TimeZone.getTimeZone("CST"));
0528: SimpleDateFormat simple = new SimpleDateFormat(
0529: "MM/dd/yyyy HH:mm");
0530: TimeZone temp = TimeZone.getDefault();
0531: if (!simple.getTimeZone().equals(temp))
0532: errln("Fail: SimpleDateFormat not using default zone");
0533: } finally {
0534: TimeZone.setDefault(saveZone);
0535: }
0536: }
0537:
0538: /**
0539: * @bug 4104136
0540: */
0541: public void Test4104136() {
0542: SimpleDateFormat sdf = new SimpleDateFormat();
0543: String pattern = "'time' hh:mm";
0544: sdf.applyPattern(pattern);
0545: logln("pattern: \"" + pattern + "\"");
0546: String strings[] = { "time 10:30", "time 10:x", "time 10x" };
0547: ParsePosition ppos[] = { new ParsePosition(10),
0548: new ParsePosition(0), new ParsePosition(0) };
0549: Calendar cal = Calendar.getInstance();
0550: cal.clear();
0551: cal.set(1970, Calendar.JANUARY, 1, 10, 30);
0552: Date dates[] = { cal.getTime(), new Date(-1), new Date(-1) };
0553: for (int i = 0; i < 3; i++) {
0554: String text = strings[i];
0555: ParsePosition finish = ppos[i];
0556: Date exp = dates[i];
0557: ParsePosition pos = new ParsePosition(0);
0558: Date d = sdf.parse(text, pos);
0559: logln(" text: \"" + text + "\"");
0560: logln(" index: %d" + pos.getIndex());
0561: logln(" result: " + d);
0562: if (pos.getIndex() != finish.getIndex())
0563: errln("Fail: Expected pos " + finish.getIndex());
0564: if (!((d == null && exp.equals(new Date(-1))) || (d
0565: .equals(exp))))
0566: errln("Fail: Expected result " + exp);
0567: }
0568: }
0569:
0570: /**
0571: * @bug 4104522
0572: * CANNOT REPRODUCE
0573: * According to the bug report, this test should throw a
0574: * StringIndexOutOfBoundsException during the second parse. However,
0575: * this is not seen.
0576: */
0577: public void Test4104522() {
0578: SimpleDateFormat sdf = new SimpleDateFormat();
0579: String pattern = "'time' hh:mm";
0580: sdf.applyPattern(pattern);
0581: logln("pattern: \"" + pattern + "\"");
0582: // works correctly
0583: ParsePosition pp = new ParsePosition(0);
0584: String text = "time ";
0585: Date dt = sdf.parse(text, pp);
0586: logln(" text: \"" + text + "\"" + " date: " + dt);
0587: // works wrong
0588: pp.setIndex(0);
0589: text = "time";
0590: dt = sdf.parse(text, pp);
0591: logln(" text: \"" + text + "\"" + " date: " + dt);
0592: }
0593:
0594: /**
0595: * @bug 4106807
0596: */
0597: public void Test4106807() {
0598: Date dt;
0599: DateFormat df = DateFormat.getDateTimeInstance();
0600:
0601: SimpleDateFormat sdfs[] = {
0602: new SimpleDateFormat("yyyyMMddHHmmss"),
0603: new SimpleDateFormat("yyyyMMddHHmmss'Z'"),
0604: new SimpleDateFormat("yyyyMMddHHmmss''"),
0605: new SimpleDateFormat("yyyyMMddHHmmss'a''a'"),
0606: new SimpleDateFormat("yyyyMMddHHmmss %") };
0607: String strings[] = { "19980211140000", "19980211140000",
0608: "19980211140000", "19980211140000a", "19980211140000 " };
0609: GregorianCalendar gc = new GregorianCalendar();
0610: TimeZone timeZone = TimeZone.getDefault();
0611: TimeZone gmt = (TimeZone) timeZone.clone();
0612: gmt.setRawOffset(0);
0613: for (int i = 0; i < 5; i++) {
0614: SimpleDateFormat format = sdfs[i];
0615: String dateString = strings[i];
0616: try {
0617: format.setTimeZone(gmt);
0618: dt = format.parse(dateString);
0619: // {sfb} some of these parses will fail purposely
0620:
0621: StringBuffer fmtd = new StringBuffer("");
0622: FieldPosition pos = new FieldPosition(0);
0623: fmtd = df.format(dt, fmtd, pos);
0624: logln(fmtd.toString());
0625: //logln(df.format(dt));
0626: gc.setTime(dt);
0627: logln("" + gc.get(Calendar.ZONE_OFFSET));
0628: StringBuffer s = new StringBuffer("");
0629: s = format.format(dt, s, pos);
0630: logln(s.toString());
0631: } catch (ParseException e) {
0632: logln("No way Jose");
0633: }
0634: }
0635: }
0636:
0637: /*
0638: Synopsis: Chinese time zone CTT is not recogonized correctly.
0639: Description: Platform Chinese Windows 95 - ** Time zone set to CST **
0640: */
0641: /**
0642: * @bug 4108407
0643: */
0644:
0645: // {sfb} what to do with this one ??
0646: public void Test4108407() {
0647: /*
0648: // TODO user.timezone is a protected system property, catch securityexception and warn
0649: // if this is reenabled
0650: long l = System.currentTimeMillis();
0651: logln("user.timezone = " + System.getProperty("user.timezone", "?"));
0652: logln("Time Zone :" +
0653: DateFormat.getDateInstance().getTimeZone().getID());
0654: logln("Default format :" +
0655: DateFormat.getDateInstance().format(new Date(l)));
0656: logln("Full format :" +
0657: DateFormat.getDateInstance(DateFormat.FULL).format(new
0658: Date(l)));
0659: logln("*** Set host TZ to CST ***");
0660: logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");
0661: */
0662: }
0663:
0664: /**
0665: * @bug 4134203
0666: * SimpleDateFormat won't parse "GMT"
0667: */
0668: public void Test4134203() {
0669: String dateFormat = "MM/dd/yy HH:mm:ss zzz";
0670: SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
0671:
0672: ParsePosition p0 = new ParsePosition(0);
0673: Date d = fmt.parse("01/22/92 04:52:00 GMT", p0);
0674: logln(d.toString());
0675: if (p0.equals(new ParsePosition(0)))
0676: errln("Fail: failed to parse 'GMT'");
0677: // In the failure case an exception is thrown by parse();
0678: // if no exception is thrown, the test passes.
0679: }
0680:
0681: /**
0682: * @bug 4151631
0683: * SimpleDateFormat incorrect handling of 2 single quotes in format()
0684: */
0685: public void Test4151631() {
0686: String pattern = "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
0687: logln("pattern=" + pattern);
0688: SimpleDateFormat format = new SimpleDateFormat(pattern,
0689: Locale.US);
0690: StringBuffer result = new StringBuffer("");
0691: FieldPosition pos = new FieldPosition(0);
0692: Calendar cal = Calendar.getInstance();
0693: cal.clear();
0694: cal.set(1998, Calendar.JUNE, 30, 13, 30, 0);
0695: Date d = cal.getTime();
0696: result = format.format(d, result, pos);
0697: if (!result
0698: .toString()
0699: .equals(
0700: "TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')")) {
0701: errln("Fail: result=" + result);
0702: } else {
0703: logln("Pass: result=" + result);
0704: }
0705: }
0706:
0707: /**
0708: * @bug 4151706
0709: * 'z' at end of date format throws index exception in SimpleDateFormat
0710: * CANNOT REPRODUCE THIS BUG ON 1.2FCS
0711: */
0712: public void Test4151706() {
0713: String dateString = "Thursday, 31-Dec-98 23:00:00 GMT";
0714: SimpleDateFormat fmt = new SimpleDateFormat(
0715: "EEEE, dd-MMM-yy HH:mm:ss z", Locale.US);
0716: Calendar cal = Calendar.getInstance(
0717: TimeZone.getTimeZone("UTC"), Locale.US);
0718: cal.clear();
0719: cal.set(1998, Calendar.DECEMBER, 31, 23, 0, 0);
0720: Date d = new Date();
0721: try {
0722: d = fmt.parse(dateString);
0723: // {sfb} what about next two lines?
0724: if (d.getTime() != cal.getTime().getTime())
0725: errln("Incorrect value: " + d);
0726: } catch (Exception e) {
0727: errln("Fail: " + e);
0728: }
0729: StringBuffer temp = new StringBuffer("");
0730: FieldPosition pos = new FieldPosition(0);
0731: logln(dateString + " . " + fmt.format(d, temp, pos));
0732: }
0733:
0734: /**
0735: * @bug 4162071
0736: * Cannot reproduce this bug under 1.2 FCS -- it may be a convoluted duplicate
0737: * of some other bug that has been fixed.
0738: */
0739: public void Test4162071() {
0740: String dateString = "Thu, 30-Jul-1999 11:51:14 GMT";
0741: String format = "EEE', 'dd-MMM-yyyy HH:mm:ss z"; // RFC 822/1123
0742: SimpleDateFormat df = new SimpleDateFormat(format, Locale.US);
0743: try {
0744: Date x = df.parse(dateString);
0745: StringBuffer temp = new StringBuffer("");
0746: FieldPosition pos = new FieldPosition(0);
0747: logln(dateString + " -> " + df.format(x, temp, pos));
0748: } catch (Exception e) {
0749: errln("Parse format \"" + format + "\" failed.");
0750: }
0751: }
0752:
0753: /**
0754: * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" . 1999).
0755: */
0756: public void Test4182066() {
0757: SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy",
0758: Locale.US);
0759: SimpleDateFormat dispFmt = new SimpleDateFormat(
0760: "MMM dd yyyy HH:mm:ss GG", Locale.US);
0761: /* We expect 2-digit year formats to put 2-digit years in the right
0762: * window. Out of range years, that is, anything less than "00" or
0763: * greater than "99", are treated as literal years. So "1/2/3456"
0764: * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC.
0765: */
0766: final String STRINGS[] = { "02/29/00", "01/23/01", "04/05/-1",
0767: "01/23/-9", "11/12/1314", "10/31/1", "09/12/+1",
0768: "09/12/001", };
0769: int STRINGS_COUNT = STRINGS.length;
0770:
0771: Calendar cal = Calendar.getInstance();
0772: Date FAIL_DATE = cal.getTime();
0773: cal.clear();
0774: cal.set(2000, Calendar.FEBRUARY, 29);
0775: Date d0 = cal.getTime();
0776: cal.clear();
0777: cal.set(2001, Calendar.JANUARY, 23);
0778: Date d1 = cal.getTime();
0779: cal.clear();
0780: cal.set(-1, Calendar.APRIL, 5);
0781: Date d2 = cal.getTime();
0782: cal.clear();
0783: cal.set(-9, Calendar.JANUARY, 23);
0784: Date d3 = cal.getTime();
0785: cal.clear();
0786: cal.set(1314, Calendar.NOVEMBER, 12);
0787: Date d4 = cal.getTime();
0788: cal.clear();
0789: cal.set(1, Calendar.OCTOBER, 31);
0790: Date d5 = cal.getTime();
0791: cal.clear();
0792: cal.set(1, Calendar.SEPTEMBER, 12);
0793: Date d7 = cal.getTime();
0794: Date DATES[] = { d0, d1, d2, d3, d4, d5, FAIL_DATE, d7 };
0795:
0796: String out = "";
0797: boolean pass = true;
0798: for (int i = 0; i < STRINGS_COUNT; ++i) {
0799: String str = STRINGS[i];
0800: Date expected = DATES[i];
0801: Date actual = null;
0802: try {
0803: actual = fmt.parse(str);
0804: } catch (ParseException e) {
0805: actual = FAIL_DATE;
0806: }
0807: String actStr = "";
0808: if ((actual.getTime()) == FAIL_DATE.getTime()) {
0809: actStr += "null";
0810: } else {
0811: // Yuck: See j25
0812: actStr = ((DateFormat) dispFmt).format(actual);
0813: }
0814:
0815: if (expected.getTime() == (actual.getTime())) {
0816: out += str + " => " + actStr + "\n";
0817: } else {
0818: String expStr = "";
0819: if (expected.getTime() == FAIL_DATE.getTime()) {
0820: expStr += "null";
0821: } else {
0822: // Yuck: See j25
0823: expStr = ((DateFormat) dispFmt).format(expected);
0824: }
0825: out += "FAIL: " + str + " => " + actStr + ", expected "
0826: + expStr + "\n";
0827: pass = false;
0828: }
0829: }
0830: if (pass) {
0831: log(out);
0832: } else {
0833: err(out);
0834: }
0835: }
0836:
0837: /**
0838: * j32 {JDK Bug 4210209 4209272}
0839: * DateFormat cannot parse Feb 29 2000 when setLenient(false)
0840: */
0841: public void Test4210209() {
0842:
0843: String pattern = "MMM d, yyyy";
0844: DateFormat fmt = new SimpleDateFormat(pattern, Locale.US);
0845: DateFormat disp = new SimpleDateFormat("MMM dd yyyy GG",
0846: Locale.US);
0847:
0848: Calendar calx = fmt.getCalendar();
0849: calx.setLenient(false);
0850: Calendar calendar = Calendar.getInstance();
0851: calendar.clear();
0852: calendar.set(2000, Calendar.FEBRUARY, 29);
0853: Date d = calendar.getTime();
0854: String s = fmt.format(d);
0855: logln(disp.format(d) + " f> " + pattern + " => \"" + s + "\"");
0856: ParsePosition pos = new ParsePosition(0);
0857: d = fmt.parse(s, pos);
0858: logln("\"" + s + "\" p> " + pattern + " => "
0859: + (d != null ? disp.format(d) : "null"));
0860: logln("Parse pos = " + pos.getIndex() + ", error pos = "
0861: + pos.getErrorIndex());
0862: if (pos.getErrorIndex() != -1) {
0863: errln("FAIL: Error index should be -1");
0864: }
0865:
0866: // The underlying bug is in GregorianCalendar. If the following lines
0867: // succeed, the bug is fixed. If the bug isn't fixed, they will throw
0868: // an exception.
0869: GregorianCalendar cal = new GregorianCalendar();
0870: cal.clear();
0871: cal.setLenient(false);
0872: cal.set(2000, Calendar.FEBRUARY, 29); // This should work!
0873: d = cal.getTime();
0874: logln("Attempt to set Calendar to Feb 29 2000: "
0875: + disp.format(d));
0876: }
0877:
0878: public void Test714() {
0879: //TimeZone Offset
0880: TimeZone defaultTZ = TimeZone.getDefault();
0881: TimeZone PST = TimeZone.getTimeZone("PST");
0882: int defaultOffset = defaultTZ.getRawOffset();
0883: int PSTOffset = PST.getRawOffset();
0884: Date d = new Date(978103543000l - (defaultOffset - PSTOffset));
0885: d = new Date(d.getTime()
0886: - (defaultTZ.inDaylightTime(d) ? 3600000 : 0));
0887: DateFormat fmt = DateFormat.getDateTimeInstance(-1,
0888: DateFormat.MEDIUM, Locale.US);
0889: String tests = "7:25:43 AM";
0890: String s = fmt.format(d);
0891: if (!s.equals(tests)) {
0892: errln("Fail: " + s + " != " + tests);
0893: } else {
0894: logln("OK: " + s + " == " + tests);
0895: }
0896: }
0897:
0898: static final char kArabicZero = 0x0660;
0899: static final char kHindiZero = 0x0966;
0900: static final char kLatinZero = 0x0030;
0901:
0902: public void TestHindiArabicDigits() {
0903: String s;
0904: char first;
0905: String what;
0906:
0907: {
0908: DateFormat df = DateFormat.getInstance(
0909: new GregorianCalendar(), new Locale("hi", "IN"));
0910: what = "Gregorian Calendar, hindi";
0911: s = df.format(new Date(0)); /* 31/12/1969 */
0912: logln(what + "=" + s);
0913: first = s.charAt(0);
0914: if (first < kHindiZero || first > (kHindiZero + 9)) {
0915: errln(what + "- wrong digit, got " + s
0916: + " (integer digit value "
0917: + new Integer((int) first).toString());
0918: }
0919: }
0920:
0921: {
0922: DateFormat df = DateFormat.getInstance(
0923: new IslamicCalendar(), new Locale("ar", "IQ"));
0924: s = df.format(new Date(0)); /* 21/10/1989 */
0925: what = "Islamic Calendar, Arabic";
0926: logln(what + ": " + s);
0927: first = s.charAt(0);
0928: if (first < kArabicZero || first > (kArabicZero + 9)) {
0929: errln(what + " wrong digit, got " + s
0930: + " (integer digit value "
0931: + new Integer((int) first).toString());
0932: }
0933: }
0934:
0935: {
0936: DateFormat df = DateFormat.getInstance(
0937: new GregorianCalendar(), new Locale("ar", "IQ"));
0938: s = df.format(new Date(0)); /* 31/12/1969 */
0939: what = "Gregorian, ar_IQ, df.getInstance";
0940: logln(what + ": " + s);
0941: first = s.charAt(0);
0942: if (first < kArabicZero || first > (kArabicZero + 9)) {
0943: errln(what + " wrong digit but got " + s
0944: + " (integer digit value "
0945: + new Integer((int) first).toString());
0946: }
0947: }
0948: {
0949: DateFormat df = DateFormat.getInstance(
0950: new GregorianCalendar(), new Locale("mt", "MT"));
0951: s = df.format(new Date(0)); /* 31/12/1969 */
0952: what = "Gregorian, mt_MT, df.getInstance";
0953: logln(what + ": " + s);
0954: first = s.charAt(0);
0955: if (first < kLatinZero || first > (kLatinZero + 9)) {
0956: errln(what + " wrong digit but got " + s
0957: + " (integer digit value "
0958: + new Integer((int) first).toString());
0959: }
0960: }
0961:
0962: {
0963: DateFormat df = DateFormat.getInstance(
0964: new IslamicCalendar(), new Locale("ar", "IQ"));
0965: s = df.format(new Date(0)); /* 31/12/1969 */
0966: what = "Islamic calendar, ar_IQ, df.getInstance";
0967: logln(what + ": " + s);
0968: first = s.charAt(0);
0969: if (first < kArabicZero || first > (kArabicZero + 9)) {
0970: errln(what + " wrong digit but got " + s
0971: + " (integer digit value "
0972: + new Integer((int) first).toString());
0973: }
0974: }
0975:
0976: {
0977: DateFormat df = DateFormat.getDateTimeInstance(
0978: DateFormat.SHORT, DateFormat.SHORT, new Locale(
0979: "ar", "IQ"));
0980: s = df.format(new Date(0)); /* 31/12/1969 */
0981: what = "ar_IQ, getDateTimeInstance";
0982: logln(what + ": " + s);
0983: first = s.charAt(0);
0984: if (first < kArabicZero || first > (kArabicZero + 9)) {
0985: errln(what + " wrong digit but got " + s
0986: + " (integer digit value "
0987: + new Integer((int) first).toString());
0988: }
0989: }
0990:
0991: {
0992: DateFormat df = DateFormat.getInstance(
0993: new JapaneseCalendar(), new Locale("ar", "IQ"));
0994: s = df.format(new Date(0)); /* 31/12/1969 */
0995: what = "ar_IQ, Japanese Calendar, getInstance";
0996: logln(what + ": " + s);
0997: first = s.charAt(0);
0998: if (first < kArabicZero || first > (kArabicZero + 9)) {
0999: errln(what + " wrong digit but got " + s
1000: + " (integer digit value "
1001: + new Integer((int) first).toString());
1002: }
1003: }
1004: }
1005: }
|