0001: /**
0002: *******************************************************************************
0003: * Copyright (C) 2000-2006, International Business Machines Corporation and *
0004: * others. All Rights Reserved. *
0005: *******************************************************************************
0006: */package com.ibm.icu.dev.test.calendar;
0007:
0008: import com.ibm.icu.util.*;
0009:
0010: import java.util.Date;
0011: import java.util.Locale;
0012: import java.util.MissingResourceException;
0013:
0014: import com.ibm.icu.text.*;
0015:
0016: /**
0017: * @test 1.32 99/11/14
0018: * @bug 4031502 4035301 4040996 4051765 4059654 4061476 4070502 4071197 4071385
0019: * 4073929 4083167 4086724 4092362 4095407 4096231 4096539 4100311 4103271
0020: * 4106136 4108764 4114578 4118384 4125881 4125892 4136399 4141665 4142933
0021: * 4145158 4145983 4147269 4149677 4162587 4165343 4166109 4167060 4173516
0022: * 4174361 4177484 4197699 4209071 4288792
0023: */
0024: public class CalendarRegression extends com.ibm.icu.dev.test.TestFmwk {
0025:
0026: public static void main(String[] args) throws Exception {
0027: new CalendarRegression().run(args);
0028: }
0029:
0030: static final String[] FIELD_NAME = { "ERA", "YEAR", "MONTH",
0031: "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH",
0032: "DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH",
0033: "AM_PM", "HOUR", "HOUR_OF_DAY", "MINUTE", "SECOND",
0034: "MILLISECOND", "ZONE_OFFSET", "DST_OFFSET", "YEAR_WOY",
0035: "DOW_LOCAL", "EXTENDED_YEAR", "JULIAN_DAY",
0036: "MILLISECONDS_IN_DAY" };
0037:
0038: /*
0039: Synopsis: java.sql.Timestamp constructor works wrong on Windows 95
0040:
0041: ==== Here is the test ====
0042: public static void main (String args[]) {
0043: java.sql.Timestamp t= new java.sql.Timestamp(0,15,5,5,8,13,123456700);
0044: logln("expected=1901-04-05 05:08:13.1234567");
0045: logln(" result="+t);
0046: }
0047:
0048: ==== Here is the output of the test on Solaris or NT ====
0049: expected=1901-04-05 05:08:13.1234567
0050: result=1901-04-05 05:08:13.1234567
0051:
0052: ==== Here is the output of the test on Windows95 ====
0053: expected=1901-04-05 05:08:13.1234567
0054: result=1901-04-05 06:08:13.1234567
0055: */
0056:
0057: public void Test4031502() {
0058: try {
0059: // This bug actually occurs on Windows NT as well, and doesn't
0060: // require the host zone to be set; it can be set in Java.
0061: String[] ids = TimeZone.getAvailableIDs();
0062: boolean bad = false;
0063: for (int i = 0; i < ids.length; ++i) {
0064: TimeZone zone = TimeZone.getTimeZone(ids[i]);
0065: GregorianCalendar cal = new GregorianCalendar(zone);
0066: cal.clear();
0067: cal.set(1900, 15, 5, 5, 8, 13);
0068: if (cal.get(Calendar.HOUR) != 5) {
0069: logln("Fail: " + zone.getID() + " "
0070: + zone.useDaylightTime()
0071: + "; DST_OFFSET = "
0072: + cal.get(Calendar.DST_OFFSET)
0073: / (60 * 60 * 1000.0) + "; ZONE_OFFSET = "
0074: + cal.get(Calendar.ZONE_OFFSET)
0075: / (60 * 60 * 1000.0)
0076: + "; getRawOffset() = "
0077: + zone.getRawOffset() / (60 * 60 * 1000.0)
0078: + "; HOUR = " + cal.get(Calendar.HOUR));
0079: cal.clear();
0080: cal.set(1900, 15, 5, 5, 8, 13);
0081: if (cal.get(Calendar.HOUR) != 5) {
0082: logln("Fail: " + zone.getID() + " "
0083: + zone.useDaylightTime()
0084: + "; DST_OFFSET = "
0085: + cal.get(Calendar.DST_OFFSET)
0086: / (60 * 60 * 1000.0)
0087: + "; ZONE_OFFSET = "
0088: + cal.get(Calendar.ZONE_OFFSET)
0089: / (60 * 60 * 1000.0)
0090: + "; getRawOffset() = "
0091: + zone.getRawOffset()
0092: / (60 * 60 * 1000.0) + "; HOUR = "
0093: + cal.get(Calendar.HOUR));
0094: cal.clear();
0095: cal.set(1900, 15, 5, 5, 8, 13);
0096: logln("ms = " + cal.getTime() + " ("
0097: + cal.getTime().getTime() + ")");
0098: cal.get(Calendar.HOUR);
0099: java.util.GregorianCalendar cal2 = new java.util.GregorianCalendar(
0100: java.util.TimeZone.getTimeZone(ids[i]));
0101: cal2.clear();
0102: cal2.set(1900, 15, 5, 5, 8, 13);
0103: cal2.get(Calendar.HOUR);
0104: logln("java.util.GC: " + zone.getID() + " "
0105: + zone.useDaylightTime()
0106: + "; DST_OFFSET = "
0107: + cal2.get(Calendar.DST_OFFSET)
0108: / (60 * 60 * 1000.0)
0109: + "; ZONE_OFFSET = "
0110: + cal2.get(Calendar.ZONE_OFFSET)
0111: / (60 * 60 * 1000.0)
0112: + "; getRawOffset() = "
0113: + zone.getRawOffset()
0114: / (60 * 60 * 1000.0) + "; HOUR = "
0115: + cal.get(Calendar.HOUR));
0116: logln("ms = " + cal2.getTime() + " ("
0117: + cal2.getTime().getTime() + ")");
0118: bad = true;
0119: } else if (false) { // Change to true to debug
0120: logln("OK: " + zone.getID() + " "
0121: + zone.useDaylightTime() + " "
0122: + cal.get(Calendar.DST_OFFSET)
0123: / (60 * 60 * 1000) + " "
0124: + zone.getRawOffset()
0125: / (60 * 60 * 1000) + ": HOUR = "
0126: + cal.get(Calendar.HOUR));
0127: }
0128: }
0129: if (bad)
0130: errln("TimeZone problems with GC");
0131: }
0132: } catch (MissingResourceException e) {
0133: warnln("Could not load data. " + e.getMessage());
0134: }
0135: }
0136:
0137: public void Test4035301() {
0138:
0139: try {
0140: GregorianCalendar c = new GregorianCalendar(98, 8, 7);
0141: GregorianCalendar d = new GregorianCalendar(98, 8, 7);
0142: if (c.after(d) || c.after(c) || c.before(d) || c.before(c)
0143: || !c.equals(c) || !c.equals(d))
0144: errln("Fail");
0145: } catch (Exception e) {
0146: // TODO Auto-generated catch block
0147: warnln("Could not load data. " + e.getMessage());
0148: }
0149: }
0150:
0151: public void Test4040996() {
0152: try {
0153: String[] ids = TimeZone
0154: .getAvailableIDs(-8 * 60 * 60 * 1000);
0155: SimpleTimeZone pdt = new SimpleTimeZone(
0156: -8 * 60 * 60 * 1000, ids[0]);
0157: pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY,
0158: 2 * 60 * 60 * 1000);
0159: pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
0160: 2 * 60 * 60 * 1000);
0161: Calendar calendar = new GregorianCalendar(pdt);
0162:
0163: calendar.set(Calendar.MONTH, 3);
0164: calendar.set(Calendar.DAY_OF_MONTH, 18);
0165: calendar.set(Calendar.SECOND, 30);
0166:
0167: logln("MONTH: " + calendar.get(Calendar.MONTH));
0168: logln("DAY_OF_MONTH: "
0169: + calendar.get(Calendar.DAY_OF_MONTH));
0170: logln("MINUTE: " + calendar.get(Calendar.MINUTE));
0171: logln("SECOND: " + calendar.get(Calendar.SECOND));
0172:
0173: calendar.add(Calendar.SECOND, 6);
0174: //This will print out todays date for MONTH and DAY_OF_MONTH
0175: //instead of the date it was set to.
0176: //This happens when adding MILLISECOND or MINUTE also
0177: logln("MONTH: " + calendar.get(Calendar.MONTH));
0178: logln("DAY_OF_MONTH: "
0179: + calendar.get(Calendar.DAY_OF_MONTH));
0180: logln("MINUTE: " + calendar.get(Calendar.MINUTE));
0181: logln("SECOND: " + calendar.get(Calendar.SECOND));
0182: if (calendar.get(Calendar.MONTH) != 3
0183: || calendar.get(Calendar.DAY_OF_MONTH) != 18
0184: || calendar.get(Calendar.SECOND) != 36)
0185: errln("Fail: Calendar.add misbehaves");
0186: } catch (Exception e) {
0187: warnln("Could not load data. " + e.getMessage());
0188: }
0189: }
0190:
0191: public void Test4051765() {
0192: try {
0193: Calendar cal = Calendar.getInstance();
0194: cal.setLenient(false);
0195: cal.set(Calendar.DAY_OF_WEEK, 0);
0196: try {
0197: cal.getTime();
0198: errln("Fail: DAY_OF_WEEK 0 should be disallowed");
0199: } catch (IllegalArgumentException e) {
0200: return;
0201: }
0202: } catch (Exception e) {
0203: // TODO Auto-generated catch block
0204: warnln("Could not load data. " + e.getMessage());
0205: }
0206: }
0207:
0208: /*
0209: * User error - no bug here public void Test4059524() { // Create calendar
0210: * for April 10, 1997 GregorianCalendar calendar = new GregorianCalendar(); //
0211: * print out a bunch of interesting things logln("ERA: " +
0212: * calendar.get(calendar.ERA)); logln("YEAR: " +
0213: * calendar.get(calendar.YEAR)); logln("MONTH: " +
0214: * calendar.get(calendar.MONTH)); logln("WEEK_OF_YEAR: " +
0215: * calendar.get(calendar.WEEK_OF_YEAR)); logln("WEEK_OF_MONTH: " +
0216: * calendar.get(calendar.WEEK_OF_MONTH)); logln("DATE: " +
0217: * calendar.get(calendar.DATE)); logln("DAY_OF_MONTH: " +
0218: * calendar.get(calendar.DAY_OF_MONTH)); logln("DAY_OF_YEAR: " +
0219: * calendar.get(calendar.DAY_OF_YEAR)); logln("DAY_OF_WEEK: " +
0220: * calendar.get(calendar.DAY_OF_WEEK)); logln("DAY_OF_WEEK_IN_MONTH: " +
0221: * calendar.get(calendar.DAY_OF_WEEK_IN_MONTH)); logln("AM_PM: " +
0222: * calendar.get(calendar.AM_PM)); logln("HOUR: " +
0223: * calendar.get(calendar.HOUR)); logln("HOUR_OF_DAY: " +
0224: * calendar.get(calendar.HOUR_OF_DAY)); logln("MINUTE: " +
0225: * calendar.get(calendar.MINUTE)); logln("SECOND: " +
0226: * calendar.get(calendar.SECOND)); logln("MILLISECOND: " +
0227: * calendar.get(calendar.MILLISECOND)); logln("ZONE_OFFSET: " +
0228: * (calendar.get(calendar.ZONE_OFFSET)/(60*60*1000))); logln("DST_OFFSET: " +
0229: * (calendar.get(calendar.DST_OFFSET)/(60*60*1000))); calendar = new
0230: * GregorianCalendar(1997,3,10); calendar.getTime(); logln("April 10,
0231: * 1997"); logln("ERA: " + calendar.get(calendar.ERA)); logln("YEAR: " +
0232: * calendar.get(calendar.YEAR)); logln("MONTH: " +
0233: * calendar.get(calendar.MONTH)); logln("WEEK_OF_YEAR: " +
0234: * calendar.get(calendar.WEEK_OF_YEAR)); logln("WEEK_OF_MONTH: " +
0235: * calendar.get(calendar.WEEK_OF_MONTH)); logln("DATE: " +
0236: * calendar.get(calendar.DATE)); logln("DAY_OF_MONTH: " +
0237: * calendar.get(calendar.DAY_OF_MONTH)); logln("DAY_OF_YEAR: " +
0238: * calendar.get(calendar.DAY_OF_YEAR)); logln("DAY_OF_WEEK: " +
0239: * calendar.get(calendar.DAY_OF_WEEK)); logln("DAY_OF_WEEK_IN_MONTH: " +
0240: * calendar.get(calendar.DAY_OF_WEEK_IN_MONTH)); logln("AM_PM: " +
0241: * calendar.get(calendar.AM_PM)); logln("HOUR: " +
0242: * calendar.get(calendar.HOUR)); logln("HOUR_OF_DAY: " +
0243: * calendar.get(calendar.HOUR_OF_DAY)); logln("MINUTE: " +
0244: * calendar.get(calendar.MINUTE)); logln("SECOND: " +
0245: * calendar.get(calendar.SECOND)); logln("MILLISECOND: " +
0246: * calendar.get(calendar.MILLISECOND)); logln("ZONE_OFFSET: " +
0247: * (calendar.get(calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
0248: * logln("DST_OFFSET: " + (calendar.get(calendar.DST_OFFSET)/(60*60*1000))); //
0249: * in hours }
0250: */
0251:
0252: public void Test4059654() {
0253: // try {
0254: // work around bug for jdk1.4 on solaris 2.6, which uses funky
0255: // timezone names
0256: // jdk1.4.1 will drop support for 2.6 so we should be ok when it
0257: // comes out
0258: java.util.TimeZone javazone = java.util.TimeZone
0259: .getTimeZone("GMT");
0260: TimeZone icuzone = TimeZone.getTimeZone("GMT");
0261:
0262: GregorianCalendar gc = new GregorianCalendar(icuzone);
0263:
0264: gc.set(1997, 3, 1, 15, 16, 17); // April 1, 1997
0265:
0266: gc.set(Calendar.HOUR, 0);
0267: gc.set(Calendar.AM_PM, Calendar.AM);
0268: gc.set(Calendar.MINUTE, 0);
0269: gc.set(Calendar.SECOND, 0);
0270: gc.set(Calendar.MILLISECOND, 0);
0271:
0272: Date cd = gc.getTime();
0273: java.util.Calendar cal = java.util.Calendar
0274: .getInstance(javazone);
0275: cal.clear();
0276: cal.set(1997, 3, 1, 0, 0, 0);
0277: Date exp = cal.getTime();
0278: if (!cd.equals(exp))
0279: errln("Fail: Calendar.set broken. Got " + cd + " Want "
0280: + exp);
0281: // } catch (RuntimeException e) {
0282: // TODO Auto-generated catch block
0283: // e.printStackTrace();
0284: // }
0285: }
0286:
0287: public void Test4061476() {
0288: SimpleDateFormat fmt = new SimpleDateFormat("ddMMMyy",
0289: Locale.UK);
0290: Calendar cal = GregorianCalendar.getInstance(TimeZone
0291: .getTimeZone("GMT"), Locale.UK);
0292: fmt.setCalendar(cal);
0293: try {
0294: Date date = fmt.parse("29MAY97");
0295: cal.setTime(date);
0296: } catch (Exception e) {
0297: System.out.print("");
0298: }
0299: cal.set(Calendar.HOUR_OF_DAY, 13);
0300: logln("Hour: " + cal.get(Calendar.HOUR_OF_DAY));
0301: cal.add(Calendar.HOUR_OF_DAY, 6);
0302: logln("Hour: " + cal.get(Calendar.HOUR_OF_DAY));
0303: if (cal.get(Calendar.HOUR_OF_DAY) != 19)
0304: errln("Fail: Want 19 Got " + cal.get(Calendar.HOUR_OF_DAY));
0305: }
0306:
0307: public void Test4070502() {
0308: java.util.Calendar tempcal = java.util.Calendar.getInstance();
0309: tempcal.clear();
0310: tempcal.set(1998, 0, 30);
0311: Date d = getAssociatedDate(tempcal.getTime());
0312: Calendar cal = new GregorianCalendar();
0313: cal.setTime(d);
0314: if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
0315: || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
0316: errln("Fail: Want weekday Got " + d);
0317: }
0318:
0319: /**
0320: * Get the associated date starting from a specified date NOTE: the
0321: * unnecessary "getTime()'s" below are a work-around for a bug in jdk 1.1.3
0322: * (and probably earlier versions also)
0323: * <p>
0324: *
0325: * @param d
0326: * The date to start from
0327: */
0328: public static Date getAssociatedDate(Date d) {
0329: GregorianCalendar cal = new GregorianCalendar();
0330: cal.setTime(d);
0331: //cal.add(field, amount); //<-- PROBLEM SEEN WITH field = DATE,MONTH
0332: // cal.getTime(); // <--- REMOVE THIS TO SEE BUG
0333: while (true) {
0334: int wd = cal.get(Calendar.DAY_OF_WEEK);
0335: if (wd == Calendar.SATURDAY || wd == Calendar.SUNDAY) {
0336: cal.add(Calendar.DATE, 1);
0337: // cal.getTime();
0338: } else
0339: break;
0340: }
0341: return cal.getTime();
0342: }
0343:
0344: public void Test4071197() {
0345: dowTest(false);
0346: dowTest(true);
0347: }
0348:
0349: void dowTest(boolean lenient) {
0350: GregorianCalendar cal = new GregorianCalendar();
0351: cal.set(1997, Calendar.AUGUST, 12); // Wednesday
0352: // cal.getTime(); // Force update
0353: cal.setLenient(lenient);
0354: cal.set(1996, Calendar.DECEMBER, 1); // Set the date to be December 1,
0355: // 1996
0356: int dow = cal.get(Calendar.DAY_OF_WEEK);
0357: int min = cal.getMinimum(Calendar.DAY_OF_WEEK);
0358: int max = cal.getMaximum(Calendar.DAY_OF_WEEK);
0359: logln(cal.getTime().toString());
0360: if (min != Calendar.SUNDAY || max != Calendar.SATURDAY)
0361: errln("FAIL: Min/max bad");
0362: if (dow < min || dow > max)
0363: errln("FAIL: Day of week " + dow + " out of range");
0364: if (dow != Calendar.SUNDAY)
0365: errln("FAIL: Day of week should be SUNDAY Got " + dow);
0366: }
0367:
0368: public void Test4071385() {
0369: // work around bug for jdk1.4 on solaris 2.6, which uses funky timezone
0370: // names
0371: // jdk1.4.1 will drop support for 2.6 so we should be ok when it comes out
0372: java.util.TimeZone javazone = java.util.TimeZone
0373: .getTimeZone("GMT");
0374: TimeZone icuzone = TimeZone.getTimeZone("GMT");
0375:
0376: Calendar cal = Calendar.getInstance(icuzone);
0377: java.util.Calendar tempcal = java.util.Calendar
0378: .getInstance(javazone);
0379: tempcal.clear();
0380: tempcal.set(1998, Calendar.JUNE, 24);
0381: cal.setTime(tempcal.getTime());
0382: cal.set(Calendar.MONTH, Calendar.NOVEMBER); // change a field
0383: logln(cal.getTime().toString());
0384: tempcal.set(1998, Calendar.NOVEMBER, 24);
0385: if (!cal.getTime().equals(tempcal.getTime()))
0386: errln("Fail");
0387: }
0388:
0389: public void Test4073929() {
0390: GregorianCalendar foo1 = new GregorianCalendar(1997, 8, 27);
0391: foo1.add(Calendar.DAY_OF_MONTH, +1);
0392: int testyear = foo1.get(Calendar.YEAR);
0393: int testmonth = foo1.get(Calendar.MONTH);
0394: int testday = foo1.get(Calendar.DAY_OF_MONTH);
0395: if (testyear != 1997 || testmonth != 8 || testday != 28)
0396: errln("Fail: Calendar not initialized");
0397: }
0398:
0399: public void Test4083167() {
0400: TimeZone saveZone = TimeZone.getDefault();
0401: try {
0402: TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
0403: Date firstDate = new Date();
0404: Calendar cal = new GregorianCalendar();
0405: cal.setTime(firstDate);
0406: long firstMillisInDay = cal.get(Calendar.HOUR_OF_DAY)
0407: * 3600000L + cal.get(Calendar.MINUTE) * 60000L
0408: + cal.get(Calendar.SECOND) * 1000L
0409: + cal.get(Calendar.MILLISECOND);
0410:
0411: logln("Current time: " + firstDate.toString());
0412:
0413: for (int validity = 0; validity < 30; validity++) {
0414: Date lastDate = new Date(firstDate.getTime()
0415: + (long) validity * 1000 * 24 * 60 * 60);
0416: cal.setTime(lastDate);
0417: long millisInDay = cal.get(Calendar.HOUR_OF_DAY)
0418: * 3600000L + cal.get(Calendar.MINUTE) * 60000L
0419: + cal.get(Calendar.SECOND) * 1000L
0420: + cal.get(Calendar.MILLISECOND);
0421: if (firstMillisInDay != millisInDay)
0422: errln("Day has shifted " + lastDate);
0423: }
0424: } finally {
0425: TimeZone.setDefault(saveZone);
0426: }
0427: }
0428:
0429: public void Test4086724() {
0430: SimpleDateFormat date;
0431: TimeZone saveZone = TimeZone.getDefault();
0432: Locale saveLocale = Locale.getDefault();
0433: try {
0434: Locale.setDefault(Locale.UK);
0435: TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
0436: date = new SimpleDateFormat(
0437: "dd MMM yyy (zzzz) 'is in week' ww");
0438: Calendar cal = Calendar.getInstance();
0439: cal.set(1997, Calendar.SEPTEMBER, 30);
0440: Date now = cal.getTime();
0441: logln(date.format(now));
0442: cal.set(1997, Calendar.JANUARY, 1);
0443: now = cal.getTime();
0444: logln(date.format(now));
0445: cal.set(1997, Calendar.JANUARY, 8);
0446: now = cal.getTime();
0447: logln(date.format(now));
0448: cal.set(1996, Calendar.DECEMBER, 31);
0449: now = cal.getTime();
0450: logln(date.format(now));
0451: } finally {
0452: Locale.setDefault(saveLocale);
0453: TimeZone.setDefault(saveZone);
0454: }
0455: logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");
0456: }
0457:
0458: public void Test4092362() {
0459: GregorianCalendar cal1 = new GregorianCalendar(1997, 10, 11,
0460: 10, 20, 40);
0461: /*
0462: * cal1.set( Calendar.YEAR, 1997 ); cal1.set( Calendar.MONTH, 10 );
0463: * cal1.set( Calendar.DATE, 11 ); cal1.set( Calendar.HOUR, 10 );
0464: * cal1.set( Calendar.MINUTE, 20 ); cal1.set( Calendar.SECOND, 40 );
0465: */
0466:
0467: logln(" Cal1 = " + cal1.getTime().getTime());
0468: logln(" Cal1 time in ms = " + cal1.get(Calendar.MILLISECOND));
0469: for (int k = 0; k < 100; k++) {
0470: System.out.print("");
0471: }
0472:
0473: GregorianCalendar cal2 = new GregorianCalendar(1997, 10, 11,
0474: 10, 20, 40);
0475: /*
0476: * cal2.set( Calendar.YEAR, 1997 ); cal2.set( Calendar.MONTH, 10 );
0477: * cal2.set( Calendar.DATE, 11 ); cal2.set( Calendar.HOUR, 10 );
0478: * cal2.set( Calendar.MINUTE, 20 ); cal2.set( Calendar.SECOND, 40 );
0479: */
0480:
0481: logln(" Cal2 = " + cal2.getTime().getTime());
0482: logln(" Cal2 time in ms = " + cal2.get(Calendar.MILLISECOND));
0483: if (!cal1.equals(cal2))
0484: errln("Fail: Milliseconds randomized");
0485: }
0486:
0487: public void Test4095407() {
0488: GregorianCalendar a = new GregorianCalendar(1997,
0489: Calendar.NOVEMBER, 13);
0490: int dow = a.get(Calendar.DAY_OF_WEEK);
0491: if (dow != Calendar.THURSDAY)
0492: errln("Fail: Want THURSDAY Got " + dow);
0493: }
0494:
0495: public void Test4096231() {
0496: TimeZone GMT = TimeZone.getTimeZone("GMT");
0497: TimeZone PST = TimeZone.getTimeZone("PST");
0498: int sec = 0, min = 0, hr = 0, day = 1, month = 10, year = 1997;
0499:
0500: Calendar cal1 = new GregorianCalendar(PST);
0501: cal1.setTime(new Date(880698639000L));
0502: int p;
0503: logln("PST 1 is: " + (p = cal1.get(Calendar.HOUR_OF_DAY)));
0504: cal1.setTimeZone(GMT);
0505: // Issue 1: Changing the timezone doesn't change the
0506: // represented time.
0507: int h1, h2;
0508: logln("GMT 1 is: " + (h1 = cal1.get(Calendar.HOUR_OF_DAY)));
0509: cal1.setTime(new Date(880698639000L));
0510: logln("GMT 2 is: " + (h2 = cal1.get(Calendar.HOUR_OF_DAY)));
0511: // Note: This test had a bug in it. It wanted h1!=h2, when
0512: // what was meant was h1!=p. Fixed this concurrent with fix
0513: // to 4177484.
0514: if (p == h1 || h1 != h2)
0515: errln("Fail: Hour same in different zones");
0516:
0517: Calendar cal2 = new GregorianCalendar(GMT);
0518: Calendar cal3 = new GregorianCalendar(PST);
0519: cal2.set(Calendar.MILLISECOND, 0);
0520: cal3.set(Calendar.MILLISECOND, 0);
0521:
0522: cal2.set(cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH),
0523: cal1.get(Calendar.DAY_OF_MONTH), cal1
0524: .get(Calendar.HOUR_OF_DAY), cal1
0525: .get(Calendar.MINUTE), cal1
0526: .get(Calendar.SECOND));
0527:
0528: long t1, t2, t3, t4;
0529: logln("RGMT 1 is: " + (t1 = cal2.getTime().getTime()));
0530: cal3.set(year, month, day, hr, min, sec);
0531: logln("RPST 1 is: " + (t2 = cal3.getTime().getTime()));
0532: cal3.setTimeZone(GMT);
0533: logln("RGMT 2 is: " + (t3 = cal3.getTime().getTime()));
0534: cal3.set(cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH),
0535: cal1.get(Calendar.DAY_OF_MONTH), cal1
0536: .get(Calendar.HOUR_OF_DAY), cal1
0537: .get(Calendar.MINUTE), cal1
0538: .get(Calendar.SECOND));
0539: // Issue 2: Calendar continues to use the timezone in its
0540: // constructor for set() conversions, regardless
0541: // of calls to setTimeZone()
0542: logln("RGMT 3 is: " + (t4 = cal3.getTime().getTime()));
0543: if (t1 == t2 || t1 != t4 || t2 != t3)
0544: errln("Fail: Calendar zone behavior faulty");
0545: }
0546:
0547: public void Test4096539() {
0548: int[] y = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
0549:
0550: for (int x = 0; x < 12; x++) {
0551: GregorianCalendar gc = new GregorianCalendar(1997, x, y[x]);
0552: int m1, m2;
0553: log((m1 = gc.get(Calendar.MONTH) + 1) + "/"
0554: + gc.get(Calendar.DATE) + "/"
0555: + gc.get(Calendar.YEAR) + " + 1mo = ");
0556:
0557: gc.add(Calendar.MONTH, 1);
0558: logln((m2 = gc.get(Calendar.MONTH) + 1) + "/"
0559: + gc.get(Calendar.DATE) + "/"
0560: + gc.get(Calendar.YEAR));
0561: int m = (m1 % 12) + 1;
0562: if (m2 != m)
0563: errln("Fail: Want " + m + " Got " + m2);
0564: }
0565:
0566: }
0567:
0568: public void Test4100311() {
0569: GregorianCalendar cal = (GregorianCalendar) Calendar
0570: .getInstance();
0571: cal.set(Calendar.YEAR, 1997);
0572: cal.set(Calendar.DAY_OF_YEAR, 1);
0573: Date d = cal.getTime(); // Should be Jan 1
0574: logln(d.toString());
0575: if (cal.get(Calendar.DAY_OF_YEAR) != 1)
0576: errln("Fail: DAY_OF_YEAR not set");
0577: }
0578:
0579: public void Test4103271() {
0580: SimpleDateFormat sdf = new SimpleDateFormat();
0581: int numYears = 40, startYear = 1997, numDays = 15;
0582: String output, testDesc;
0583: GregorianCalendar testCal = (GregorianCalendar) Calendar
0584: .getInstance();
0585: testCal.clear();
0586: sdf.setCalendar(testCal);
0587: sdf.applyPattern("d MMM yyyy");
0588: boolean fail = false;
0589: for (int firstDay = 1; firstDay <= 2; firstDay++) {
0590: for (int minDays = 1; minDays <= 7; minDays++) {
0591: testCal.setMinimalDaysInFirstWeek(minDays);
0592: testCal.setFirstDayOfWeek(firstDay);
0593: testDesc = ("Test" + String.valueOf(firstDay) + String
0594: .valueOf(minDays));
0595: logln(testDesc + " => 1st day of week="
0596: + String.valueOf(firstDay)
0597: + ", minimum days in first week="
0598: + String.valueOf(minDays));
0599: for (int j = startYear; j <= startYear + numYears; j++) {
0600: testCal.set(j, 11, 25);
0601: for (int i = 0; i < numDays; i++) {
0602: testCal.add(Calendar.DATE, 1);
0603: String calWOY;
0604: int actWOY = testCal.get(Calendar.WEEK_OF_YEAR);
0605: if (actWOY < 1 || actWOY > 53) {
0606: Date d = testCal.getTime();
0607: calWOY = String.valueOf(actWOY);
0608: output = testDesc + " - " + sdf.format(d)
0609: + "\t";
0610: output = output + "\t" + calWOY;
0611: logln(output);
0612: fail = true;
0613: }
0614: }
0615: }
0616: }
0617: }
0618:
0619: int[] DATA = { 3, 52, 52, 52, 52, 52, 52, 52, 1, 1, 1, 1, 1, 1,
0620: 1, 2, 2, 2, 2, 2, 2, 2, 4, 52, 52, 52, 52, 52, 52, 52,
0621: 53, 53, 53, 53, 53, 53, 53, 1, 1, 1, 1, 1, 1, 1, };
0622: testCal.setFirstDayOfWeek(Calendar.SUNDAY);
0623: for (int j = 0; j < DATA.length; j += 22) {
0624: logln("Minimal days in first week = " + DATA[j]
0625: + " Week starts on Sunday");
0626: testCal.setMinimalDaysInFirstWeek(DATA[j]);
0627: testCal.set(1997, Calendar.DECEMBER, 21);
0628: for (int i = 0; i < 21; ++i) {
0629: int woy = testCal.get(Calendar.WEEK_OF_YEAR);
0630: log(testCal.getTime() + " " + woy);
0631: if (woy != DATA[j + 1 + i]) {
0632: log(" ERROR");
0633: fail = true;
0634: }
0635: //logln();
0636:
0637: // Now compute the time from the fields, and make sure we
0638: // get the same answer back. This is a round-trip test.
0639: Date save = testCal.getTime();
0640: testCal.clear();
0641: testCal.set(Calendar.YEAR, DATA[j + 1 + i] < 25 ? 1998
0642: : 1997);
0643: testCal.set(Calendar.WEEK_OF_YEAR, DATA[j + 1 + i]);
0644: testCal.set(Calendar.DAY_OF_WEEK, (i % 7)
0645: + Calendar.SUNDAY);
0646: if (!testCal.getTime().equals(save)) {
0647: logln(" Parse failed: " + testCal.getTime());
0648: fail = true;
0649: }
0650:
0651: testCal.setTime(save);
0652: testCal.add(Calendar.DAY_OF_MONTH, 1);
0653: }
0654: }
0655:
0656: Date d[] = new Date[8];
0657: java.util.Calendar tempcal = java.util.Calendar.getInstance();
0658: tempcal.clear();
0659: tempcal.set(1997, Calendar.DECEMBER, 28);
0660: d[0] = tempcal.getTime();
0661: tempcal.set(1998, Calendar.JANUARY, 10);
0662: d[1] = tempcal.getTime();
0663: tempcal.set(1998, Calendar.DECEMBER, 31);
0664: d[2] = tempcal.getTime();
0665: tempcal.set(1999, Calendar.JANUARY, 1);
0666: d[3] = tempcal.getTime();
0667: // Test field disambiguation with a few special hard-coded cases.
0668: // This shouldn't fail if the above cases aren't failing.
0669: Object[] DISAM = { new Integer(1998), new Integer(1),
0670: new Integer(Calendar.SUNDAY), d[0], new Integer(1998),
0671: new Integer(2), new Integer(Calendar.SATURDAY), d[1],
0672: new Integer(1998), new Integer(53),
0673: new Integer(Calendar.THURSDAY), d[2],
0674: new Integer(1998), new Integer(53),
0675: new Integer(Calendar.FRIDAY), d[3], };
0676: testCal.setMinimalDaysInFirstWeek(3);
0677: testCal.setFirstDayOfWeek(Calendar.SUNDAY);
0678: for (int i = 0; i < DISAM.length; i += 4) {
0679: int y = ((Integer) DISAM[i]).intValue();
0680: int woy = ((Integer) DISAM[i + 1]).intValue();
0681: int dow = ((Integer) DISAM[i + 2]).intValue();
0682: Date exp = (Date) DISAM[i + 3];
0683: testCal.clear();
0684: testCal.set(Calendar.YEAR, y);
0685: testCal.set(Calendar.WEEK_OF_YEAR, woy);
0686: testCal.set(Calendar.DAY_OF_WEEK, dow);
0687: log(y + "-W" + woy + "-DOW" + dow + " expect:" + exp
0688: + " got:" + testCal.getTime());
0689: if (!testCal.getTime().equals(exp)) {
0690: log(" FAIL");
0691: fail = true;
0692: }
0693: //logln();
0694: }
0695:
0696: // Now try adding and rolling
0697: Object ADD = new Object();
0698: Object ROLL = new Object();
0699: tempcal.set(1998, Calendar.DECEMBER, 25);
0700: d[0] = tempcal.getTime();
0701: tempcal.set(1999, Calendar.JANUARY, 1);
0702: d[1] = tempcal.getTime();
0703: tempcal.set(1997, Calendar.DECEMBER, 28);
0704: d[2] = tempcal.getTime();
0705: tempcal.set(1998, Calendar.JANUARY, 4);
0706: d[3] = tempcal.getTime();
0707: tempcal.set(1998, Calendar.DECEMBER, 27);
0708: d[4] = tempcal.getTime();
0709: tempcal.set(1997, Calendar.DECEMBER, 28);
0710: d[5] = tempcal.getTime();
0711: tempcal.set(1999, Calendar.JANUARY, 2);
0712: d[6] = tempcal.getTime();
0713: tempcal.set(1998, Calendar.JANUARY, 3);
0714: d[7] = tempcal.getTime();
0715:
0716: Object[] ADDROLL = { ADD, new Integer(1), d[0], d[1], ADD,
0717: new Integer(1), d[2], d[3], ROLL, new Integer(1), d[4],
0718: d[5], ROLL, new Integer(1), d[6], d[7], };
0719: testCal.setMinimalDaysInFirstWeek(3);
0720: testCal.setFirstDayOfWeek(Calendar.SUNDAY);
0721: for (int i = 0; i < ADDROLL.length; i += 4) {
0722: int amount = ((Integer) ADDROLL[i + 1]).intValue();
0723: Date before = (Date) ADDROLL[i + 2];
0724: Date after = (Date) ADDROLL[i + 3];
0725:
0726: testCal.setTime(before);
0727: if (ADDROLL[i] == ADD)
0728: testCal.add(Calendar.WEEK_OF_YEAR, amount);
0729: else
0730: testCal.roll(Calendar.WEEK_OF_YEAR, amount);
0731: log((ADDROLL[i] == ADD ? "add(WOY," : "roll(WOY,") + amount
0732: + ") " + before + " => " + testCal.getTime());
0733: if (!after.equals(testCal.getTime())) {
0734: logln(" exp:" + after + " FAIL");
0735: fail = true;
0736: } else
0737: logln(" ok");
0738:
0739: testCal.setTime(after);
0740: if (ADDROLL[i] == ADD)
0741: testCal.add(Calendar.WEEK_OF_YEAR, -amount);
0742: else
0743: testCal.roll(Calendar.WEEK_OF_YEAR, -amount);
0744: log((ADDROLL[i] == ADD ? "add(WOY," : "roll(WOY,")
0745: + (-amount) + ") " + after + " => "
0746: + testCal.getTime());
0747: if (!before.equals(testCal.getTime())) {
0748: logln(" exp:" + before + " FAIL");
0749: fail = true;
0750: } else
0751: logln(" ok");
0752: }
0753:
0754: if (fail)
0755: errln("Fail: Week of year misbehaving");
0756: }
0757:
0758: public void Test4106136() {
0759: Locale saveLocale = Locale.getDefault();
0760: String[] names = { "Calendar", "DateFormat", "NumberFormat" };
0761: try {
0762: Locale[] locales = { Locale.CHINESE, Locale.CHINA };
0763: for (int i = 0; i < locales.length; ++i) {
0764: Locale.setDefault(locales[i]);
0765: int[] n = { Calendar.getAvailableLocales().length,
0766: DateFormat.getAvailableLocales().length,
0767: NumberFormat.getAvailableLocales().length };
0768: for (int j = 0; j < n.length; ++j) {
0769: if (n[j] == 0)
0770: errln("Fail: " + names[j]
0771: + " has no locales for " + locales[i]);
0772: }
0773: }
0774: } finally {
0775: Locale.setDefault(saveLocale);
0776: }
0777: }
0778:
0779: public void Test4108764() {
0780: java.util.Calendar tempcal = java.util.Calendar.getInstance();
0781: tempcal.clear();
0782: tempcal.set(1997, Calendar.MARCH, 15, 12, 00, 00);
0783: Date d00 = tempcal.getTime();
0784: tempcal.set(1997, Calendar.MARCH, 15, 12, 00, 56);
0785: Date d01 = tempcal.getTime();
0786: tempcal.set(1997, Calendar.MARCH, 15, 12, 34, 00);
0787: Date d10 = tempcal.getTime();
0788: tempcal.set(1997, Calendar.MARCH, 15, 12, 34, 56);
0789: Date d11 = tempcal.getTime();
0790: tempcal.set(1997, Calendar.JANUARY, 15, 12, 34, 56);
0791: Date dM = tempcal.getTime();
0792: tempcal.clear();
0793: tempcal.set(1970, Calendar.JANUARY, 1);
0794: Date epoch = tempcal.getTime();
0795:
0796: Calendar cal = Calendar.getInstance();
0797: cal.setTime(d11);
0798:
0799: cal.clear(Calendar.MINUTE);
0800: logln(cal.getTime().toString());
0801: if (!cal.getTime().equals(d01)) {
0802: errln("Fail: " + d11 + " clear(MINUTE) => expect " + d01
0803: + ", got " + cal.getTime());
0804: }
0805:
0806: cal.set(Calendar.SECOND, 0);
0807: logln(cal.getTime().toString());
0808: if (!cal.getTime().equals(d00))
0809: errln("Fail: set(SECOND, 0) broken");
0810:
0811: cal.setTime(d11);
0812: cal.set(Calendar.SECOND, 0);
0813: logln(cal.getTime().toString());
0814: if (!cal.getTime().equals(d10))
0815: errln("Fail: set(SECOND, 0) broken #2");
0816:
0817: cal.clear(Calendar.MINUTE);
0818: logln(cal.getTime().toString());
0819: if (!cal.getTime().equals(d00))
0820: errln("Fail: clear(MINUTE) broken #2");
0821:
0822: cal.clear();
0823: logln(cal.getTime().toString());
0824: if (!cal.getTime().equals(epoch))
0825: errln("Fail: after clear() expect " + epoch + ", got "
0826: + cal.getTime());
0827:
0828: cal.setTime(d11);
0829: cal.clear(Calendar.MONTH);
0830: logln(cal.getTime().toString());
0831: if (!cal.getTime().equals(dM)) {
0832: errln("Fail: " + d11 + " clear(MONTH) => expect " + dM
0833: + ", got " + cal.getTime());
0834: }
0835: }
0836:
0837: public void Test4114578() {
0838: int ONE_HOUR = 60 * 60 * 1000;
0839: Calendar cal = Calendar.getInstance();
0840: cal.setTimeZone(TimeZone.getTimeZone("PST"));
0841:
0842: java.util.Calendar tempcal = java.util.Calendar.getInstance();
0843: tempcal.clear();
0844: tempcal.set(1998, Calendar.APRIL, 5, 1, 0);
0845: long onset = tempcal.getTime().getTime() + ONE_HOUR;
0846: tempcal.set(1998, Calendar.OCTOBER, 25, 0, 0);
0847: long cease = tempcal.getTime().getTime() + 2 * ONE_HOUR;
0848:
0849: boolean fail = false;
0850:
0851: final int ADD = 1;
0852: final int ROLL = 2;
0853:
0854: long[] DATA = {
0855: // Start Action Amt Expected_change
0856: onset - ONE_HOUR, ADD, 1, ONE_HOUR, onset, ADD, -1,
0857: -ONE_HOUR, onset - ONE_HOUR, ROLL, 1, ONE_HOUR, onset,
0858: ROLL, -1, -ONE_HOUR, cease - ONE_HOUR, ADD, 1,
0859: ONE_HOUR, cease, ADD, -1, -ONE_HOUR, cease - ONE_HOUR,
0860: ROLL, 1, ONE_HOUR, cease, ROLL, -1, -ONE_HOUR, };
0861:
0862: for (int i = 0; i < DATA.length; i += 4) {
0863: Date date = new Date(DATA[i]);
0864: int amt = (int) DATA[i + 2];
0865: long expectedChange = DATA[i + 3];
0866:
0867: log(date.toString());
0868: cal.setTime(date);
0869:
0870: switch ((int) DATA[i + 1]) {
0871: case ADD:
0872: log(" add (HOUR," + (amt < 0 ? "" : "+") + amt + ")= ");
0873: cal.add(Calendar.HOUR, amt);
0874: break;
0875: case ROLL:
0876: log(" roll(HOUR," + (amt < 0 ? "" : "+") + amt + ")= ");
0877: cal.roll(Calendar.HOUR, amt);
0878: break;
0879: }
0880:
0881: log(cal.getTime().toString());
0882:
0883: long change = cal.getTime().getTime() - date.getTime();
0884: if (change != expectedChange) {
0885: fail = true;
0886: logln(" FAIL");
0887: } else
0888: logln(" OK");
0889: }
0890:
0891: if (fail)
0892: errln("Fail: roll/add misbehaves around DST onset/cease");
0893: }
0894:
0895: /**
0896: * Make sure maximum for HOUR field is 11, not 12.
0897: */
0898: public void Test4118384() {
0899: Calendar cal = Calendar.getInstance();
0900: if (cal.getMaximum(Calendar.HOUR) != 11
0901: || cal.getLeastMaximum(Calendar.HOUR) != 11
0902: || cal.getActualMaximum(Calendar.HOUR) != 11)
0903: errln("Fail: maximum of HOUR field should be 11");
0904: }
0905:
0906: /**
0907: * Check isLeapYear for BC years.
0908: */
0909: public void Test4125881() {
0910: GregorianCalendar cal = (GregorianCalendar) Calendar
0911: .getInstance();
0912: DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
0913: cal.clear();
0914: for (int y = -20; y <= 10; ++y) {
0915: cal.set(Calendar.ERA, y < 1 ? GregorianCalendar.BC
0916: : GregorianCalendar.AD);
0917: cal.set(Calendar.YEAR, y < 1 ? 1 - y : y);
0918: logln(y + " = " + fmt.format(cal.getTime()) + " "
0919: + cal.isLeapYear(y));
0920: if (cal.isLeapYear(y) != ((y + 40) % 4 == 0))
0921: errln("Leap years broken");
0922: }
0923: }
0924:
0925: // I am disabling this test -- it is currently failing because of a bug
0926: // in Sun's latest change to STZ.getOffset(). I have filed a Sun bug
0927: // against this problem.
0928:
0929: // Re-enabled after 'porting' TZ and STZ from java.util to com.ibm.icu.util.
0930: /**
0931: * Prove that GregorianCalendar is proleptic (it used to cut off at 45 BC,
0932: * and not have leap years before then).
0933: */
0934: public void Test4125892() {
0935: GregorianCalendar cal = (GregorianCalendar) Calendar
0936: .getInstance();
0937: //DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
0938: //fmt = null;
0939: cal.clear();
0940: cal.set(Calendar.ERA, GregorianCalendar.BC);
0941: cal.set(Calendar.YEAR, 81); // 81 BC is a leap year (proleptically)
0942: cal.set(Calendar.MONTH, Calendar.FEBRUARY);
0943: cal.set(Calendar.DATE, 28);
0944: cal.add(Calendar.DATE, 1);
0945: if (cal.get(Calendar.DATE) != 29 || !cal.isLeapYear(-80)) // -80 == 81 BC
0946: errln("Calendar not proleptic");
0947: }
0948:
0949: /**
0950: * Calendar and GregorianCalendar hashCode() methods need improvement.
0951: * Calendar needs a good implementation that subclasses can override, and
0952: * GregorianCalendar should use that implementation.
0953: */
0954: public void Test4136399() {
0955: /*
0956: * Note: This test is actually more strict than it has to be.
0957: * Technically, there is no requirement that unequal objects have
0958: * unequal hashes. We only require equal objects to have equal hashes.
0959: * It is desirable for unequal objects to have distributed hashes, but
0960: * there is no hard requirement here.
0961: *
0962: * In this test we make assumptions about certain attributes of calendar
0963: * objects getting represented in the hash, which need not always be the
0964: * case (although it does work currently with the given test).
0965: */
0966: Calendar a = Calendar.getInstance();
0967: Calendar b = (Calendar) a.clone();
0968: if (a.hashCode() != b.hashCode()) {
0969: errln("Calendar hash code unequal for cloned objects");
0970: }
0971: TimeZone atz1 = a.getTimeZone();
0972: TimeZone atz2 = (TimeZone) atz1.clone();
0973: if (!atz1.equals(atz2)) {
0974: errln("The clone timezones are not equal");
0975: }
0976: if (atz1.hashCode() != atz2.hashCode()) {
0977: errln("TimeZone hash code unequal for cloned objects");
0978: }
0979: b.setMinimalDaysInFirstWeek(7 - a.getMinimalDaysInFirstWeek());
0980: if (a.hashCode() == b.hashCode()) {
0981: errln("Calendar hash code ignores minimal days in first week");
0982: }
0983: b.setMinimalDaysInFirstWeek(a.getMinimalDaysInFirstWeek());
0984:
0985: b.setFirstDayOfWeek((a.getFirstDayOfWeek() % 7) + 1); // Next day
0986: if (a.hashCode() == b.hashCode()) {
0987: errln("Calendar hash code ignores first day of week");
0988: }
0989: b.setFirstDayOfWeek(a.getFirstDayOfWeek());
0990:
0991: b.setLenient(!a.isLenient());
0992: if (a.hashCode() == b.hashCode()) {
0993: errln("Calendar hash code ignores lenient setting");
0994: }
0995: b.setLenient(a.isLenient());
0996:
0997: // Assume getTimeZone() returns a reference, not a clone
0998: // of a reference -- this is true as of this writing
0999: TimeZone atz = a.getTimeZone();
1000: TimeZone btz = b.getTimeZone();
1001:
1002: btz.setRawOffset(atz.getRawOffset() + 60 * 60 * 1000);
1003: if (atz.hashCode() == btz.hashCode()) {
1004: errln(atz.hashCode() + "==" + btz.hashCode());
1005: }
1006: if (a.getTimeZone() != b.getTimeZone()
1007: && a.hashCode() == b.hashCode()) {
1008: errln("Calendar hash code ignores zone");
1009: }
1010: b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset());
1011:
1012: GregorianCalendar c = new GregorianCalendar();
1013: GregorianCalendar d = (GregorianCalendar) c.clone();
1014: if (c.hashCode() != d.hashCode()) {
1015: errln("GregorianCalendar hash code unequal for clones objects");
1016: }
1017: Date cutover = c.getGregorianChange();
1018: d.setGregorianChange(new Date(cutover.getTime() + 24 * 60 * 60
1019: * 1000));
1020: if (c.hashCode() == d.hashCode()) {
1021: errln("GregorianCalendar hash code ignores cutover");
1022: }
1023: }
1024:
1025: /**
1026: * GregorianCalendar.equals() ignores cutover date
1027: */
1028: public void Test4141665() {
1029: GregorianCalendar cal = new GregorianCalendar();
1030: GregorianCalendar cal2 = (GregorianCalendar) cal.clone();
1031: Date cut = cal.getGregorianChange();
1032: Date cut2 = new Date(cut.getTime() + 100 * 24 * 60 * 60 * 1000L); // 100 days
1033: // later
1034: if (!cal.equals(cal2)) {
1035: errln("Cloned GregorianCalendars not equal");
1036: }
1037: cal2.setGregorianChange(cut2);
1038: if (cal.equals(cal2)) {
1039: errln("GregorianCalendar.equals() ignores cutover");
1040: }
1041: }
1042:
1043: /**
1044: * Bug states that ArrayIndexOutOfBoundsException is thrown by
1045: * GregorianCalendar.roll() when IllegalArgumentException should be.
1046: */
1047: public void Test4142933() {
1048: GregorianCalendar calendar = new GregorianCalendar();
1049: try {
1050: calendar.roll(-1, true);
1051: errln("Test failed, no exception trown");
1052: } catch (IllegalArgumentException e) {
1053: // OK: Do nothing
1054: // logln("Test passed");
1055: System.out.print("");
1056: } catch (Exception e) {
1057: errln("Test failed. Unexpected exception is thrown: " + e);
1058: e.printStackTrace();
1059: }
1060: }
1061:
1062: /**
1063: * GregorianCalendar handling of Dates Long.MIN_VALUE and Long.MAX_VALUE is
1064: * confusing; unless the time zone has a raw offset of zero, one or the
1065: * other of these will wrap. We've modified the test given in the bug report
1066: * to therefore only check the behavior of a calendar with a zero raw offset
1067: * zone.
1068: */
1069: public void Test4145158() {
1070: GregorianCalendar calendar = new GregorianCalendar();
1071:
1072: calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
1073:
1074: calendar.setTime(new Date(Long.MIN_VALUE));
1075: int year1 = calendar.get(Calendar.YEAR);
1076: int era1 = calendar.get(Calendar.ERA);
1077:
1078: calendar.setTime(new Date(Long.MAX_VALUE));
1079: int year2 = calendar.get(Calendar.YEAR);
1080: int era2 = calendar.get(Calendar.ERA);
1081:
1082: if (year1 == year2 && era1 == era2) {
1083: errln("Fail: Long.MIN_VALUE or Long.MAX_VALUE wrapping around");
1084: }
1085: }
1086:
1087: /**
1088: * Maximum value for YEAR field wrong.
1089: */
1090: public void Test4145983() {
1091: GregorianCalendar calendar = new GregorianCalendar();
1092: calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
1093: Date[] DATES = { new Date(Long.MAX_VALUE),
1094: new Date(Long.MIN_VALUE) };
1095: for (int i = 0; i < DATES.length; ++i) {
1096: calendar.setTime(DATES[i]);
1097: int year = calendar.get(Calendar.YEAR);
1098: int maxYear = calendar.getMaximum(Calendar.YEAR);
1099: if (year > maxYear) {
1100: errln("Failed for " + DATES[i].getTime() + " ms: year="
1101: + year + ", maxYear=" + maxYear);
1102: }
1103: }
1104: }
1105:
1106: /**
1107: * This is a bug in the validation code of GregorianCalendar. As reported,
1108: * the bug seems worse than it really is, due to a bug in the way the bug
1109: * report test was written. In reality the bug is restricted to the
1110: * DAY_OF_YEAR field. - liu 6/29/98
1111: */
1112: public void Test4147269() {
1113: GregorianCalendar calendar = new GregorianCalendar();
1114: calendar.setLenient(false);
1115: java.util.Calendar tempcal = java.util.Calendar.getInstance();
1116: tempcal.clear();
1117: tempcal.set(1996, Calendar.JANUARY, 3); // Arbitrary date
1118: Date date = tempcal.getTime();
1119: for (int field = 0; field < calendar.getFieldCount(); field++) {
1120: calendar.setTime(date);
1121: // Note: In the bug report, getActualMaximum() was called instead
1122: // of getMaximum() -- this was an error. The validation code doesn't
1123: // use getActualMaximum(), since that's too costly.
1124: int max = calendar.getMaximum(field);
1125: int value = max + 1;
1126: calendar.set(field, value);
1127: try {
1128: calendar.getTime(); // Force time computation
1129: // We expect an exception to be thrown. If we fall through
1130: // to the next line, then we have a bug.
1131: errln("Test failed with field " + FIELD_NAME[field]
1132: + ", date before: " + date + ", date after: "
1133: + calendar.getTime() + ", value: " + value
1134: + " (max = " + max + ")");
1135: } catch (IllegalArgumentException e) {
1136: System.out.print("");
1137: }
1138: }
1139: }
1140:
1141: /**
1142: * Reported bug is that a GregorianCalendar with a cutover of
1143: * Date(Long.MAX_VALUE) doesn't behave as a pure Julian calendar. CANNOT
1144: * REPRODUCE THIS BUG
1145: */
1146: public void Test4149677() {
1147: TimeZone[] zones = { TimeZone.getTimeZone("GMT"),
1148: TimeZone.getTimeZone("PST"),
1149: TimeZone.getTimeZone("EAT") };
1150: for (int i = 0; i < zones.length; ++i) {
1151: GregorianCalendar calendar = new GregorianCalendar(zones[i]);
1152:
1153: // Make sure extreme values don't wrap around
1154: calendar.setTime(new Date(Long.MIN_VALUE));
1155: if (calendar.get(Calendar.ERA) != GregorianCalendar.BC) {
1156: errln("Fail: Long.MIN_VALUE ms has an AD year");
1157: }
1158: calendar.setTime(new Date(Long.MAX_VALUE));
1159: if (calendar.get(Calendar.ERA) != GregorianCalendar.AD) {
1160: errln("Fail: Long.MAX_VALUE ms has a BC year");
1161: }
1162:
1163: calendar.setGregorianChange(new Date(Long.MAX_VALUE));
1164: // to obtain a pure Julian calendar
1165:
1166: boolean is100Leap = calendar.isLeapYear(100);
1167: if (!is100Leap) {
1168: errln("test failed with zone " + zones[i].getID());
1169: errln(" cutover date is Calendar.MAX_DATE");
1170: errln(" isLeapYear(100) returns: " + is100Leap);
1171: }
1172: }
1173: }
1174:
1175: /**
1176: * Calendar and Date HOUR broken. If HOUR is out-of-range, Calendar and Date
1177: * classes will misbehave.
1178: */
1179: public void Test4162587() {
1180: TimeZone tz = TimeZone.getTimeZone("PST");
1181: TimeZone.setDefault(tz);
1182: GregorianCalendar cal = new GregorianCalendar(tz);
1183: Date d;
1184:
1185: for (int i = 0; i < 5; ++i) {
1186: if (i > 0)
1187: logln("---");
1188:
1189: cal.clear();
1190: cal.set(1998, Calendar.APRIL, 5, i, 0);
1191: d = cal.getTime();
1192: String s0 = d.toString();
1193: logln("0 " + i + ": " + s0);
1194:
1195: cal.clear();
1196: cal.set(1998, Calendar.APRIL, 4, i + 24, 0);
1197: d = cal.getTime();
1198: String sPlus = d.toString();
1199: logln("+ " + i + ": " + sPlus);
1200:
1201: cal.clear();
1202: cal.set(1998, Calendar.APRIL, 6, i - 24, 0);
1203: d = cal.getTime();
1204: String sMinus = d.toString();
1205: logln("- " + i + ": " + sMinus);
1206:
1207: if (!s0.equals(sPlus) || !s0.equals(sMinus)) {
1208: errln("Fail: All three lines must match");
1209: }
1210: }
1211: }
1212:
1213: /**
1214: * Adding 12 months behaves differently from adding 1 year
1215: */
1216: public void Test4165343() {
1217: GregorianCalendar calendar = new GregorianCalendar(1996,
1218: Calendar.FEBRUARY, 29);
1219: Date start = calendar.getTime();
1220: logln("init date: " + start);
1221: calendar.add(Calendar.MONTH, 12);
1222: Date date1 = calendar.getTime();
1223: logln("after adding 12 months: " + date1);
1224: calendar.setTime(start);
1225: calendar.add(Calendar.YEAR, 1);
1226: Date date2 = calendar.getTime();
1227: logln("after adding one year : " + date2);
1228: if (date1.equals(date2)) {
1229: logln("Test passed");
1230: } else {
1231: errln("Test failed");
1232: }
1233: }
1234:
1235: /**
1236: * GregorianCalendar.getActualMaximum() does not account for first day of
1237: * week.
1238: */
1239: public void Test4166109() {
1240: /*
1241: * Test month:
1242: *
1243: * March 1998 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1244: * 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
1245: */
1246: boolean passed = true;
1247: int field = Calendar.WEEK_OF_MONTH;
1248:
1249: GregorianCalendar calendar = new GregorianCalendar(Locale.US);
1250: calendar.set(1998, Calendar.MARCH, 1);
1251: calendar.setMinimalDaysInFirstWeek(1);
1252: logln("Date: " + calendar.getTime());
1253:
1254: int firstInMonth = calendar.get(Calendar.DAY_OF_MONTH);
1255:
1256: for (int firstInWeek = Calendar.SUNDAY; firstInWeek <= Calendar.SATURDAY; firstInWeek++) {
1257: calendar.setFirstDayOfWeek(firstInWeek);
1258: int returned = calendar.getActualMaximum(field);
1259: int expected = (31 + ((firstInMonth - firstInWeek + 7) % 7) + 6) / 7;
1260:
1261: logln("First day of week = " + firstInWeek
1262: + " getActualMaximum(WEEK_OF_MONTH) = " + returned
1263: + " expected = " + expected
1264: + ((returned == expected) ? " ok" : " FAIL"));
1265:
1266: if (returned != expected) {
1267: passed = false;
1268: }
1269: }
1270: if (!passed) {
1271: errln("Test failed");
1272: }
1273: }
1274:
1275: /**
1276: * Calendar.getActualMaximum(YEAR) works wrong.
1277: */
1278: public void Test4167060() {
1279: int field = Calendar.YEAR;
1280: DateFormat format = new SimpleDateFormat(
1281: "EEE MMM dd HH:mm:ss zzz yyyy G", Locale.US);
1282:
1283: GregorianCalendar calendars[] = {
1284: new GregorianCalendar(100, Calendar.NOVEMBER, 1),
1285: new GregorianCalendar(-99 /* 100BC */,
1286: Calendar.JANUARY, 1),
1287: new GregorianCalendar(1996, Calendar.FEBRUARY, 29), };
1288:
1289: String[] id = { "Hybrid", "Gregorian", "Julian" };
1290:
1291: for (int k = 0; k < 3; ++k) {
1292: logln("--- " + id[k] + " ---");
1293:
1294: for (int j = 0; j < calendars.length; ++j) {
1295: GregorianCalendar calendar = calendars[j];
1296: if (k == 1) {
1297: calendar
1298: .setGregorianChange(new Date(Long.MIN_VALUE));
1299: } else if (k == 2) {
1300: calendar
1301: .setGregorianChange(new Date(Long.MAX_VALUE));
1302: }
1303:
1304: format.setCalendar((Calendar) calendar.clone());
1305:
1306: Date dateBefore = calendar.getTime();
1307:
1308: int maxYear = calendar.getActualMaximum(field);
1309: logln("maxYear: " + maxYear + " for "
1310: + format.format(calendar.getTime()));
1311: logln("date before: " + format.format(dateBefore));
1312:
1313: int years[] = { 2000, maxYear - 1, maxYear, maxYear + 1 };
1314:
1315: for (int i = 0; i < years.length; i++) {
1316: boolean valid = years[i] <= maxYear;
1317: calendar.set(field, years[i]);
1318: Date dateAfter = calendar.getTime();
1319: int newYear = calendar.get(field);
1320: calendar.setTime(dateBefore); // restore calendar for next
1321: // use
1322:
1323: logln(" Year " + years[i]
1324: + (valid ? " ok " : " bad") + " => "
1325: + format.format(dateAfter));
1326: if (valid && newYear != years[i]) {
1327: errln(" FAIL: "
1328: + newYear
1329: + " should be valid; date, month and time shouldn't change");
1330: } else if (!valid && newYear == years[i]) {
1331: // We no longer require strict year maxima. That is, the
1332: // calendar
1333: // algorithm may work for values > the stated maximum.
1334: //errln(" FAIL: " + newYear + " should be invalid");
1335: logln(" Note: " + newYear
1336: + " > maximum, but still valid");
1337: }
1338: }
1339: }
1340: }
1341: }
1342:
1343: /**
1344: * Calendar.roll broken This bug relies on the TimeZone bug 4173604 to also
1345: * be fixed.
1346: */
1347: public void Test4173516() {
1348: int fieldsList[][] = {
1349: { 1997, Calendar.FEBRUARY, 1, 10, 45, 15, 900 },
1350: { 1999, Calendar.DECEMBER, 22, 23, 59, 59, 999 } };
1351: int limit = 40;
1352: GregorianCalendar cal = new GregorianCalendar();
1353:
1354: cal.setTime(new Date(0));
1355: cal.roll(Calendar.HOUR, 0x7F000000);
1356: cal.roll(Calendar.HOUR, -0x7F000000);
1357: if (cal.getTime().getTime() != 0) {
1358: errln("Hour rolling broken");
1359: }
1360:
1361: for (int op = 0; op < 2; ++op) {
1362: logln("Testing GregorianCalendar "
1363: + (op == 0 ? "add" : "roll"));
1364: for (int field = 0; field < cal.getFieldCount(); ++field) {
1365: if (field != Calendar.ZONE_OFFSET
1366: && field != Calendar.DST_OFFSET) {
1367: for (int j = 0; j < fieldsList.length; ++j) {
1368: int fields[] = fieldsList[j];
1369: cal.clear();
1370: cal.set(fields[0], fields[1], fields[2],
1371: fields[3], fields[4], fields[5]);
1372: cal.set(Calendar.MILLISECOND, fields[6]);
1373: for (int i = 0; i < 2 * limit; i++) {
1374: if (op == 0) {
1375: cal.add(field, i < limit ? 1 : -1);
1376: } else {
1377: cal.roll(field, i < limit ? 1 : -1);
1378: }
1379: }
1380: if (cal.get(Calendar.YEAR) != fields[0]
1381: || cal.get(Calendar.MONTH) != fields[1]
1382: || cal.get(Calendar.DATE) != fields[2]
1383: || cal.get(Calendar.HOUR_OF_DAY) != fields[3]
1384: || cal.get(Calendar.MINUTE) != fields[4]
1385: || cal.get(Calendar.SECOND) != fields[5]
1386: || cal.get(Calendar.MILLISECOND) != fields[6]) {
1387: errln("Field " + field + " ("
1388: + FIELD_NAME[field]
1389: + ") FAIL, expected " + fields[0]
1390: + "/" + (fields[1] + 1) + "/"
1391: + fields[2] + " " + fields[3] + ":"
1392: + fields[4] + ":" + fields[5] + "."
1393: + fields[6] + ", got "
1394: + cal.get(Calendar.YEAR) + "/"
1395: + (cal.get(Calendar.MONTH) + 1)
1396: + "/" + cal.get(Calendar.DATE)
1397: + " "
1398: + cal.get(Calendar.HOUR_OF_DAY)
1399: + ":" + cal.get(Calendar.MINUTE)
1400: + ":" + cal.get(Calendar.SECOND)
1401: + "."
1402: + cal.get(Calendar.MILLISECOND));
1403: cal.clear();
1404: cal.set(fields[0], fields[1], fields[2],
1405: fields[3], fields[4], fields[5]);
1406: cal.set(Calendar.MILLISECOND, fields[6]);
1407: logln("Start date: "
1408: + cal.get(Calendar.YEAR) + "/"
1409: + (cal.get(Calendar.MONTH) + 1)
1410: + "/" + cal.get(Calendar.DATE)
1411: + " "
1412: + cal.get(Calendar.HOUR_OF_DAY)
1413: + ":" + cal.get(Calendar.MINUTE)
1414: + ":" + cal.get(Calendar.SECOND)
1415: + "."
1416: + cal.get(Calendar.MILLISECOND));
1417: long prev = cal.getTime().getTime();
1418: for (int i = 0; i < 2 * limit; i++) {
1419: if (op == 0) {
1420: cal.add(field, i < limit ? 1 : -1);
1421: } else {
1422: cal.roll(field, i < limit ? 1 : -1);
1423: }
1424: long t = cal.getTime().getTime();
1425: long delta = t - prev;
1426: prev = t;
1427: logln((op == 0 ? "add(" : "roll(")
1428: + FIELD_NAME[field]
1429: + (i < limit ? ", +1) => "
1430: : ", -1) => ")
1431: + cal.get(Calendar.YEAR) + "/"
1432: + (cal.get(Calendar.MONTH) + 1)
1433: + "/" + cal.get(Calendar.DATE)
1434: + " "
1435: + cal.get(Calendar.HOUR_OF_DAY)
1436: + ":"
1437: + cal.get(Calendar.MINUTE)
1438: + ":"
1439: + cal.get(Calendar.SECOND)
1440: + "."
1441: + cal.get(Calendar.MILLISECOND)
1442: + " delta=" + delta + " ms");
1443: }
1444: }
1445: }
1446: }
1447: }
1448: }
1449: }
1450:
1451: public void Test4174361() {
1452: GregorianCalendar calendar = new GregorianCalendar(1996, 1, 29);
1453:
1454: calendar.add(Calendar.MONTH, 10);
1455: //Date date1 = calendar.getTime();
1456: //date1 = null;
1457: int d1 = calendar.get(Calendar.DAY_OF_MONTH);
1458:
1459: calendar = new GregorianCalendar(1996, 1, 29);
1460: calendar.add(Calendar.MONTH, 11);
1461: //Date date2 = calendar.getTime();
1462: //date2 = null;
1463: int d2 = calendar.get(Calendar.DAY_OF_MONTH);
1464:
1465: if (d1 != d2) {
1466: errln("adding months to Feb 29 broken");
1467: }
1468: }
1469:
1470: /**
1471: * Calendar does not update field values when setTimeZone is called.
1472: */
1473: public void Test4177484() {
1474: TimeZone PST = TimeZone.getTimeZone("PST");
1475: TimeZone EST = TimeZone.getTimeZone("EST");
1476:
1477: Calendar cal = Calendar.getInstance(PST, Locale.US);
1478: cal.clear();
1479: cal.set(1999, 3, 21, 15, 5, 0); // Arbitrary
1480: int h1 = cal.get(Calendar.HOUR_OF_DAY);
1481: cal.setTimeZone(EST);
1482: int h2 = cal.get(Calendar.HOUR_OF_DAY);
1483: if (h1 == h2) {
1484: errln("FAIL: Fields not updated after setTimeZone");
1485: }
1486:
1487: // getTime() must NOT change when time zone is changed.
1488: // getTime() returns zone-independent time in ms.
1489: cal.clear();
1490: cal.setTimeZone(PST);
1491: cal.set(Calendar.HOUR_OF_DAY, 10);
1492: Date pst10 = cal.getTime();
1493: cal.setTimeZone(EST);
1494: Date est10 = cal.getTime();
1495: if (!pst10.equals(est10)) {
1496: errln("FAIL: setTimeZone changed time");
1497: }
1498: }
1499:
1500: /**
1501: * Week of year is wrong at the start and end of the year.
1502: */
1503: public void Test4197699() {
1504: GregorianCalendar cal = new GregorianCalendar();
1505: cal.setFirstDayOfWeek(Calendar.MONDAY);
1506: cal.setMinimalDaysInFirstWeek(4);
1507: DateFormat fmt = new SimpleDateFormat(
1508: "E dd MMM yyyy 'DOY='D 'WOY='w");
1509: fmt.setCalendar(cal);
1510:
1511: int[] DATA = { 2000, Calendar.JANUARY, 1, 52, 2001,
1512: Calendar.DECEMBER, 31, 1, };
1513:
1514: for (int i = 0; i < DATA.length;) {
1515: cal.set(DATA[i++], DATA[i++], DATA[i++]);
1516: int expWOY = DATA[i++];
1517: int actWOY = cal.get(Calendar.WEEK_OF_YEAR);
1518: if (expWOY == actWOY) {
1519: logln("Ok: " + fmt.format(cal.getTime()));
1520: } else {
1521: errln("FAIL: " + fmt.format(cal.getTime())
1522: + ", expected WOY=" + expWOY);
1523: cal.add(Calendar.DATE, -8);
1524: for (int j = 0; j < 14; ++j) {
1525: cal.add(Calendar.DATE, 1);
1526: logln(fmt.format(cal.getTime()));
1527: }
1528: }
1529: }
1530: }
1531:
1532: /**
1533: * Calendar DAY_OF_WEEK_IN_MONTH fields->time broken. The problem is in the
1534: * field disambiguation code in GregorianCalendar. This code is supposed to
1535: * choose the most recent set of fields among the following:
1536: *
1537: * MONTH + DAY_OF_MONTH MONTH + WEEK_OF_MONTH + DAY_OF_WEEK MONTH +
1538: * DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK DAY_OF_YEAR WEEK_OF_YEAR + DAY_OF_WEEK
1539: */
1540: public void Test4209071() {
1541: Calendar cal = Calendar.getInstance(Locale.US);
1542:
1543: // General field setting test
1544: int Y = 1995;
1545:
1546: Date d[] = new Date[13];
1547: java.util.Calendar tempcal = java.util.Calendar.getInstance();
1548: tempcal.clear();
1549: tempcal.set(Y, Calendar.JANUARY, 1);
1550: d[0] = tempcal.getTime();
1551: tempcal.set(Y, Calendar.MARCH, 1);
1552: d[1] = tempcal.getTime();
1553: tempcal.set(Y, Calendar.JANUARY, 4);
1554: d[2] = tempcal.getTime();
1555: tempcal.set(Y, Calendar.JANUARY, 18);
1556: d[3] = tempcal.getTime();
1557: tempcal.set(Y, Calendar.JANUARY, 18);
1558: d[4] = tempcal.getTime();
1559: tempcal.set(Y - 1, Calendar.DECEMBER, 22);
1560: d[5] = tempcal.getTime();
1561: tempcal.set(Y, Calendar.JANUARY, 26);
1562: d[6] = tempcal.getTime();
1563: tempcal.set(Y, Calendar.JANUARY, 26);
1564: d[7] = tempcal.getTime();
1565: tempcal.set(Y, Calendar.MARCH, 1);
1566: d[8] = tempcal.getTime();
1567: tempcal.set(Y, Calendar.OCTOBER, 6);
1568: d[9] = tempcal.getTime();
1569: tempcal.set(Y, Calendar.OCTOBER, 13);
1570: d[10] = tempcal.getTime();
1571: tempcal.set(Y, Calendar.AUGUST, 10);
1572: d[11] = tempcal.getTime();
1573: tempcal.set(Y, Calendar.DECEMBER, 7);
1574: d[12] = tempcal.getTime();
1575:
1576: Object[] FIELD_DATA = {
1577: // Add new test cases as needed.
1578:
1579: // 0
1580: new int[] {},
1581: d[0],
1582: // 1
1583: new int[] { Calendar.MONTH, Calendar.MARCH },
1584: d[1],
1585: // 2
1586: new int[] { Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY },
1587: d[2],
1588: // 3
1589: new int[] { Calendar.DAY_OF_WEEK, Calendar.THURSDAY,
1590: Calendar.DAY_OF_MONTH, 18, },
1591: d[3],
1592: // 4
1593: new int[] { Calendar.DAY_OF_MONTH, 18,
1594: Calendar.DAY_OF_WEEK, Calendar.THURSDAY, },
1595: d[4],
1596: // 5 (WOM -1 is in previous month)
1597: new int[] { Calendar.DAY_OF_MONTH, 18,
1598: Calendar.WEEK_OF_MONTH, -1,
1599: Calendar.DAY_OF_WEEK, Calendar.THURSDAY, },
1600: d[5],
1601: // 6
1602: new int[] { Calendar.DAY_OF_MONTH, 18,
1603: Calendar.WEEK_OF_MONTH, 4,
1604: Calendar.DAY_OF_WEEK, Calendar.THURSDAY, },
1605: d[6],
1606: // 7 (DIM -1 is in same month)
1607: new int[] { Calendar.DAY_OF_MONTH, 18,
1608: Calendar.DAY_OF_WEEK_IN_MONTH, -1,
1609: Calendar.DAY_OF_WEEK, Calendar.THURSDAY, },
1610: d[7],
1611: // 8
1612: new int[] { Calendar.WEEK_OF_YEAR, 9,
1613: Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY, },
1614: d[8],
1615: // 9
1616: new int[] { Calendar.MONTH, Calendar.OCTOBER,
1617: Calendar.DAY_OF_WEEK_IN_MONTH, 1,
1618: Calendar.DAY_OF_WEEK, Calendar.FRIDAY, },
1619: d[9],
1620: // 10
1621: new int[] { Calendar.MONTH, Calendar.OCTOBER,
1622: Calendar.WEEK_OF_MONTH, 2,
1623: Calendar.DAY_OF_WEEK, Calendar.FRIDAY, },
1624: d[10],
1625: // 11
1626: new int[] { Calendar.MONTH, Calendar.OCTOBER,
1627: Calendar.DAY_OF_MONTH, 15,
1628: Calendar.DAY_OF_YEAR, 222, },
1629: d[11],
1630: // 12
1631: new int[] { Calendar.DAY_OF_WEEK, Calendar.THURSDAY,
1632: Calendar.MONTH, Calendar.DECEMBER, }, d[12], };
1633:
1634: for (int i = 0; i < FIELD_DATA.length; i += 2) {
1635: int[] fields = (int[]) FIELD_DATA[i];
1636: Date exp = (Date) FIELD_DATA[i + 1];
1637:
1638: cal.clear();
1639: cal.set(Calendar.YEAR, Y);
1640: for (int j = 0; j < fields.length; j += 2) {
1641: cal.set(fields[j], fields[j + 1]);
1642: }
1643:
1644: Date act = cal.getTime();
1645: if (!act.equals(exp)) {
1646: errln("FAIL: Test "
1647: + (i / 2)
1648: + " got "
1649: + act
1650: + ", want "
1651: + exp
1652: + " (see test/java/util/Calendar/CalendarRegression.java");
1653: }
1654: }
1655:
1656: tempcal.set(1997, Calendar.JANUARY, 5);
1657: d[0] = tempcal.getTime();
1658: tempcal.set(1997, Calendar.JANUARY, 26);
1659: d[1] = tempcal.getTime();
1660: tempcal.set(1997, Calendar.FEBRUARY, 23);
1661: d[2] = tempcal.getTime();
1662: tempcal.set(1997, Calendar.JANUARY, 26);
1663: d[3] = tempcal.getTime();
1664: tempcal.set(1997, Calendar.JANUARY, 5);
1665: d[4] = tempcal.getTime();
1666: tempcal.set(1996, Calendar.DECEMBER, 8);
1667: d[5] = tempcal.getTime();
1668: // Test specific failure reported in bug
1669: Object[] DATA = { new Integer(1), d[0], new Integer(4), d[1],
1670: new Integer(8), d[2], new Integer(-1), d[3],
1671: new Integer(-4), d[4], new Integer(-8), d[5], };
1672: for (int i = 0; i < DATA.length; i += 2) {
1673: cal.clear();
1674: cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, ((Number) DATA[i])
1675: .intValue());
1676: cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
1677: cal.set(Calendar.MONTH, Calendar.JANUARY);
1678: cal.set(Calendar.YEAR, 1997);
1679: Date actual = cal.getTime();
1680: if (!actual.equals(DATA[i + 1])) {
1681: errln("FAIL: Sunday " + DATA[i] + " of Jan 1997 -> "
1682: + actual + ", want " + DATA[i + 1]);
1683: }
1684: }
1685: }
1686:
1687: /**
1688: * WEEK_OF_YEAR computed incorrectly. A failure of this test can indicate a
1689: * problem in several different places in the
1690: */
1691: public void Test4288792() throws Exception {
1692: TimeZone savedTZ = TimeZone.getDefault();
1693: TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
1694: GregorianCalendar cal = new GregorianCalendar();
1695:
1696: for (int i = 1900; i < 2100; i++) {
1697: for (int j1 = 1; j1 <= 7; j1++) {
1698: // Loop for MinimalDaysInFirstWeek: 1..7
1699: for (int j = Calendar.SUNDAY; j <= Calendar.SATURDAY; j++) {
1700: // Loop for FirstDayOfWeek: SUNDAY..SATURDAY
1701: cal.clear();
1702: cal.setMinimalDaysInFirstWeek(j1);
1703: cal.setFirstDayOfWeek(j);
1704: // Set the calendar to the first day of the last week
1705: // of the year. This may overlap some of the start of
1706: // the next year; that is, the last week of 1999 may
1707: // include some of January 2000. Use the add() method
1708: // to advance through the week. For each day, call
1709: // get(WEEK_OF_YEAR). The result should be the same
1710: // for the whole week. Note that a bug in
1711: // getActualMaximum() will break this test.
1712: cal.set(Calendar.YEAR, i);
1713: int maxWeek = cal
1714: .getActualMaximum(Calendar.WEEK_OF_YEAR);
1715: cal.set(Calendar.WEEK_OF_YEAR, maxWeek);
1716: cal.set(Calendar.DAY_OF_WEEK, j);
1717: for (int k = 1; k < 7; k++) {
1718: cal.add(Calendar.DATE, 1);
1719: int WOY = cal.get(Calendar.WEEK_OF_YEAR);
1720: if (WOY != maxWeek) {
1721: errln(cal.getTime() + ",got=" + WOY
1722: + ",expected=" + maxWeek + ",min="
1723: + j1 + ",first=" + j);
1724: }
1725: }
1726: // Now advance the calendar one more day. This should
1727: // put it at the first day of week 1 of the next year.
1728: cal.add(Calendar.DATE, 1);
1729: int WOY = cal.get(Calendar.WEEK_OF_YEAR);
1730: if (WOY != 1) {
1731: errln(cal.getTime() + ",got=" + WOY
1732: + ",expected=1,min=" + j1 + ",first"
1733: + j);
1734: }
1735: }
1736: }
1737: }
1738: TimeZone.setDefault(savedTZ);
1739: }
1740:
1741: /**
1742: * Test fieldDifference().
1743: */
1744: public void TestJ438() throws Exception {
1745: int DATA[] = { 2000, Calendar.JANUARY, 20, 2010, Calendar.JUNE,
1746: 15, 2010, Calendar.JUNE, 15, 2000, Calendar.JANUARY,
1747: 20, 1964, Calendar.SEPTEMBER, 7, 1999, Calendar.JUNE,
1748: 4, 1999, Calendar.JUNE, 4, 1964, Calendar.SEPTEMBER, 7, };
1749: Calendar cal = Calendar.getInstance(Locale.US);
1750: for (int i = 0; i < DATA.length; i += 6) {
1751: int y1 = DATA[i];
1752: int m1 = DATA[i + 1];
1753: int d1 = DATA[i + 2];
1754: int y2 = DATA[i + 3];
1755: int m2 = DATA[i + 4];
1756: int d2 = DATA[i + 5];
1757:
1758: cal.clear();
1759: cal.set(y1, m1, d1);
1760: Date date1 = cal.getTime();
1761: cal.set(y2, m2, d2);
1762: Date date2 = cal.getTime();
1763:
1764: cal.setTime(date1);
1765: int dy = cal.fieldDifference(date2, Calendar.YEAR);
1766: int dm = cal.fieldDifference(date2, Calendar.MONTH);
1767: int dd = cal.fieldDifference(date2, Calendar.DATE);
1768:
1769: logln("" + date2 + " - " + date1 + " = " + dy + "y " + dm
1770: + "m " + dd + "d");
1771:
1772: cal.setTime(date1);
1773: cal.add(Calendar.YEAR, dy);
1774: cal.add(Calendar.MONTH, dm);
1775: cal.add(Calendar.DATE, dd);
1776: Date date22 = cal.getTime();
1777: if (!date2.equals(date22)) {
1778: errln("FAIL: " + date1 + " + " + dy + "y " + dm + "m "
1779: + dd + "d = " + date22 + ", exp " + date2);
1780: } else {
1781: logln("Ok: " + date1 + " + " + dy + "y " + dm + "m "
1782: + dd + "d = " + date22);
1783: }
1784: }
1785: }
1786:
1787: /**
1788: * Set behavior of DST_OFFSET field. ICU4J Jitterbug 9.
1789: */
1790: public void TestJ9() {
1791: int HOURS = 60 * 60 * 1000;
1792: Calendar cal = new GregorianCalendar(TimeZone
1793: .getTimeZone("PST"), Locale.US);
1794:
1795: final int END_FIELDS = 0x1234;
1796:
1797: int[] DATA = {
1798: // With no explicit ZONE/DST expect 12:00 am
1799: Calendar.MONTH,
1800: Calendar.JUNE,
1801: END_FIELDS,
1802: 0,
1803: 0, // expected hour, min
1804:
1805: // Normal ZONE/DST for June 1 Pacific is 8:00/1:00
1806: Calendar.MONTH,
1807: Calendar.JUNE,
1808: Calendar.ZONE_OFFSET,
1809: -8 * HOURS,
1810: Calendar.DST_OFFSET,
1811: HOURS,
1812: END_FIELDS,
1813: 0,
1814: 0, // expected hour, min
1815:
1816: // With ZONE/DST of 8:00/0:30 expect time of 12:30 am
1817: Calendar.MONTH, Calendar.JUNE, Calendar.ZONE_OFFSET,
1818: -8 * HOURS, Calendar.DST_OFFSET,
1819: HOURS / 2,
1820: END_FIELDS,
1821: 0,
1822: 30, // expected hour, min
1823:
1824: // With ZONE/DST of 8:00/UNSET expect time of 1:00 am
1825: Calendar.MONTH, Calendar.JUNE, Calendar.ZONE_OFFSET,
1826: -8 * HOURS, END_FIELDS, 1,
1827: 0, // expected hour, min
1828:
1829: // With ZONE/DST of UNSET/0:30 expect 4:30 pm (day before)
1830: Calendar.MONTH, Calendar.JUNE, Calendar.DST_OFFSET,
1831: HOURS / 2, END_FIELDS, 16, 30, // expected hour, min
1832: };
1833:
1834: for (int i = 0; i < DATA.length;) {
1835: int start = i;
1836: cal.clear();
1837:
1838: // Set fields
1839: while (DATA[i] != END_FIELDS) {
1840: cal.set(DATA[i++], DATA[i++]);
1841: }
1842: ++i; // skip over END_FIELDS
1843:
1844: // Get hour/minute
1845: int h = cal.get(Calendar.HOUR_OF_DAY);
1846: int m = cal.get(Calendar.MINUTE);
1847:
1848: // Check
1849: if (h != DATA[i] || m != DATA[i + 1]) {
1850: errln("Fail: expected " + DATA[i] + ":" + DATA[i + 1]
1851: + ", got " + h + ":" + m + " after:");
1852: while (DATA[start] != END_FIELDS) {
1853: logln("set(" + FIELD_NAME[DATA[start++]] + ", "
1854: + DATA[start++] + ");");
1855: }
1856: }
1857:
1858: i += 2; // skip over expected hour, min
1859: }
1860: }
1861:
1862: /**
1863: * DateFormat class mistakes date style and time style as follows: -
1864: * DateFormat.getDateTimeInstance takes date style as time style, and time
1865: * style as date style - If a Calendar is passed to
1866: * DateFormat.getDateInstance, it returns time instance - If a Calendar is
1867: * passed to DateFormat.getTimeInstance, it returns date instance
1868: */
1869: public void TestDateFormatFactoryJ26() {
1870: TimeZone zone = TimeZone.getDefault();
1871: try {
1872: Locale loc = Locale.US;
1873: TimeZone.setDefault(TimeZone
1874: .getTimeZone("America/Los_Angeles"));
1875: java.util.Calendar tempcal = java.util.Calendar
1876: .getInstance();
1877: tempcal.set(2001, Calendar.APRIL, 5, 17, 43, 53);
1878: Date date = tempcal.getTime();
1879: Calendar cal = Calendar.getInstance(loc);
1880: Object[] DATA = {
1881: DateFormat.getDateInstance(DateFormat.SHORT, loc),
1882: "DateFormat.getDateInstance(DateFormat.SHORT, loc)",
1883: "4/5/01",
1884:
1885: DateFormat.getTimeInstance(DateFormat.SHORT, loc),
1886: "DateFormat.getTimeInstance(DateFormat.SHORT, loc)",
1887: "5:43 PM",
1888:
1889: DateFormat.getDateTimeInstance(DateFormat.FULL,
1890: DateFormat.SHORT, loc),
1891: "DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc)",
1892: "Thursday, April 5, 2001 5:43 PM",
1893:
1894: DateFormat.getDateInstance(cal, DateFormat.SHORT,
1895: loc),
1896: "DateFormat.getDateInstance(cal, DateFormat.SHORT, loc)",
1897: "4/5/01",
1898:
1899: DateFormat.getTimeInstance(cal, DateFormat.SHORT,
1900: loc),
1901: "DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc)",
1902: "5:43 PM",
1903:
1904: DateFormat.getDateTimeInstance(cal,
1905: DateFormat.FULL, DateFormat.SHORT, loc),
1906: "DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc)",
1907: "Thursday, April 5, 2001 5:43 PM",
1908:
1909: cal.getDateTimeFormat(DateFormat.SHORT,
1910: DateFormat.FULL, loc),
1911: "cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc)",
1912: "4/5/01 5:43:53 PM PT",
1913:
1914: cal.getDateTimeFormat(DateFormat.FULL,
1915: DateFormat.SHORT, loc),
1916: "cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc)",
1917: "Thursday, April 5, 2001 5:43 PM", };
1918: for (int i = 0; i < DATA.length; i += 3) {
1919: DateFormat df = (DateFormat) DATA[i];
1920: String desc = (String) DATA[i + 1];
1921: String exp = (String) DATA[i + 2];
1922: String got = df.format(date);
1923: if (got.equals(exp)) {
1924: logln("Ok: " + desc + " => " + got);
1925: } else {
1926: errln("FAIL: " + desc + " => " + got
1927: + ", expected " + exp);
1928: }
1929: }
1930: } finally {
1931: TimeZone.setDefault(zone);
1932: }
1933: }
1934:
1935: public void TestRegistration() {
1936: /*
1937: * Set names = Calendar.getCalendarFactoryNames();
1938: *
1939: * TimeZone tz = TimeZone.getDefault(); Locale loc =
1940: * Locale.getDefault(); Iterator iter = names.iterator(); while
1941: * (iter.hasNext()) { String name = (String)iter.next(); logln("Testing
1942: * factory: " + name);
1943: *
1944: * Calendar cal = Calendar.getInstance(tz, loc, name); logln("Calendar
1945: * class: " + cal.getClass());
1946: *
1947: * DateFormat fmt = cal.getDateTimeFormat(DateFormat.LONG,
1948: * DateFormat.LONG, loc);
1949: *
1950: * logln("Date: " + fmt.format(cal.getTime())); }
1951: * // register new default for our locale logln("\nTesting
1952: * registration"); loc = new Locale("en", "US"); Object key =
1953: * Calendar.register(JapaneseCalendar.factory(), loc, true);
1954: *
1955: * loc = new Locale("en", "US", "TEST"); Calendar cal =
1956: * Calendar.getInstance(loc); logln("Calendar class: " +
1957: * cal.getClass()); DateFormat fmt =
1958: * cal.getDateTimeFormat(DateFormat.LONG, DateFormat.LONG, loc);
1959: * logln("Date: " + fmt.format(cal.getTime()));
1960: * // force to use other default anyway logln("\nOverride
1961: * registration"); cal = Calendar.getInstance(tz, loc, "Gregorian"); fmt =
1962: * cal.getDateTimeFormat(DateFormat.LONG, DateFormat.LONG, loc);
1963: * logln("Date: " + fmt.format(cal.getTime()));
1964: * // unregister default logln("\nUnregistration"); logln("Unregister
1965: * returned: " + Calendar.unregister(key)); cal =
1966: * Calendar.getInstance(tz, loc, "Gregorian"); fmt =
1967: * cal.getDateTimeFormat(DateFormat.LONG, DateFormat.LONG, loc);
1968: * logln("Date: " + fmt.format(cal.getTime()));
1969: */
1970: }
1971:
1972: }
1973:
1974: //eof
|