0001: /**
0002: *******************************************************************************
0003: * Copyright (C) 2000-2005, 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: import com.ibm.icu.text.DateFormat;
0010:
0011: import java.util.Date;
0012: import java.util.Locale;
0013: import java.io.*;
0014:
0015: public class CompatibilityTest extends com.ibm.icu.dev.test.TestFmwk {
0016:
0017: public static void main(String argv[]) throws Exception {
0018: new CompatibilityTest().run(argv);
0019: }
0020:
0021: static final String[] FIELD_NAME = { "ERA", "YEAR", "MONTH",
0022: "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH",
0023: "DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH",
0024: "AM_PM", "HOUR", "HOUR_OF_DAY", "MINUTE", "SECOND",
0025: "MILLISECOND", "ZONE_OFFSET", "DST_OFFSET", "YEAR_WOY",
0026: "DOW_LOCAL", "EXTENDED_YEAR", "JULIAN_DAY",
0027: "MILLISECONDS_IN_DAY", };
0028:
0029: /**
0030: * Test the behavior of the GregorianCalendar around the changeover.
0031: */
0032: public void TestGregorianChangeover() {
0033:
0034: java.util.TimeZone jdkGMT = java.util.TimeZone
0035: .getTimeZone("GMT");
0036: java.util.Calendar jdkCal = java.util.Calendar
0037: .getInstance(jdkGMT);
0038: jdkCal.clear();
0039: jdkCal.set(1582, Calendar.OCTOBER, 15);
0040:
0041: // if(jdkCal instanceof java.util.GregorianCalendar) {
0042: // logln("jdk IS grego");
0043: // java.util.GregorianCalendar jdkgc = (java.util.GregorianCalendar)
0044: // jdkCal;
0045: // logln("jdk change at: " + jdkgc.getGregorianChange() + "(" + jdkgc.getGregorianChange().getTime() +")" );
0046: // } else {
0047: // logln("jdk NOT grego");
0048: // }
0049:
0050: long a = jdkCal.getTime().getTime();
0051: Date c = jdkCal.getTime();
0052: c.toString();
0053: long b = c.getTime();
0054: if (a != b) {
0055: logln(" " + a + " != " + b);
0056: logln("JDK has Gregorian cutover anomaly (1.5?) - skipping this test.");
0057: return;
0058: }
0059:
0060: Date co = jdkCal.getTime();
0061: logln("Change over (Oct 15 1582) = " + co + " (" + co.getTime()
0062: + ")");
0063: final int ONE_DAY = 24 * 60 * 60 * 1000;
0064: TimeZone gmt = TimeZone.getTimeZone("GMT");
0065: GregorianCalendar cal = new GregorianCalendar(gmt);
0066: /*
0067: Changeover -7 days: 1582/9/28 dow=6
0068: Changeover -6 days: 1582/9/29 dow=7
0069: Changeover -5 days: 1582/9/30 dow=1
0070: Changeover -4 days: 1582/10/1 dow=2
0071: Changeover -3 days: 1582/10/2 dow=3
0072: Changeover -2 days: 1582/10/3 dow=4
0073: Changeover -1 days: 1582/10/4 dow=5
0074: Changeover +0 days: 1582/10/15 dow=6
0075: Changeover +1 days: 1582/10/16 dow=7
0076: Changeover +2 days: 1582/10/17 dow=1
0077: Changeover +3 days: 1582/10/18 dow=2
0078: Changeover +4 days: 1582/10/19 dow=3
0079: Changeover +5 days: 1582/10/20 dow=4
0080: Changeover +6 days: 1582/10/21 dow=5
0081: Changeover +7 days: 1582/10/22 dow=6
0082: */
0083: int MON[] = { 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
0084: 10, 10 };
0085: int DOM[] = { 28, 29, 30, 1, 2, 3, 4, 15, 16, 17, 18, 19, 20,
0086: 21, 22 };
0087: int DOW[] = { 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6 };
0088: // ^ <-Changeover Fri Oct 15 1582
0089: int j = 0;
0090: for (int i = -7; i <= 7; ++i, ++j) {
0091: Date d = new Date(co.getTime() + i * ONE_DAY);
0092: cal.setTime(d);
0093: int y = cal.get(Calendar.YEAR), mon = cal
0094: .get(Calendar.MONTH)
0095: + 1 - Calendar.JANUARY, dom = cal
0096: .get(Calendar.DATE), dow = cal
0097: .get(Calendar.DAY_OF_WEEK);
0098: logln("Changeover " + (i >= 0 ? "+" : "") + i + " days: "
0099: + y + "/" + mon + "/" + dom + " dow=" + dow);
0100: if (y != 1582 || mon != MON[j] || dom != DOM[j]
0101: || dow != DOW[j])
0102: errln(" Fail: Above line is wrong");
0103: }
0104: }
0105:
0106: /**
0107: * Test the mapping between millis and fields. For the purposes
0108: * of this test, we don't care about timezones and week data
0109: * (first day of week, minimal days in first week).
0110: */
0111: public void TestMapping() {
0112: if (false) {
0113: Date PURE_GREGORIAN = new Date(Long.MIN_VALUE);
0114: Date PURE_JULIAN = new Date(Long.MAX_VALUE);
0115: GregorianCalendar cal = new GregorianCalendar(TimeZone
0116: .getTimeZone("UTC"));
0117: final int EPOCH_JULIAN = 2440588;
0118: final long ONE_DAY = 24 * 60 * 60 * 1000L;
0119: com.ibm.icu.text.SimpleDateFormat fmt = new com.ibm.icu.text.SimpleDateFormat(
0120: "EEE MMM dd yyyy G");
0121: /*HH:mm:ss.SSS z*/
0122:
0123: for (int type = 0; type < 2; ++type) {
0124: System.out.println(type == 0 ? "Gregorian" : "Julian");
0125: cal.setGregorianChange(type == 0 ? PURE_GREGORIAN
0126: : PURE_JULIAN);
0127: fmt.setCalendar(cal);
0128: int[] J = { 0x7FFFFFFF, 0x7FFFFFF0, 0x7F000000,
0129: 0x78000000, 0x70000000, 0x60000000, 0x50000000,
0130: 0x40000000, 0x30000000, 0x20000000, 0x10000000, };
0131: for (int i = 0; i < J.length; ++i) {
0132: String[] lim = new String[2];
0133: long[] ms = new long[2];
0134: int jd = J[i];
0135: for (int sign = 0; sign < 2; ++sign) {
0136: int julian = jd;
0137: if (sign == 0)
0138: julian = -julian;
0139: long millis = ((long) julian - EPOCH_JULIAN)
0140: * ONE_DAY;
0141: ms[sign] = millis;
0142: cal.setTime(new Date(millis));
0143: lim[sign] = fmt.format(cal.getTime());
0144: }
0145: System.out.println("JD +/-" + Long.toString(jd, 16)
0146: + ": " + ms[0] + ".." + ms[1] + ": "
0147: + lim[0] + ".." + lim[1]);
0148: }
0149: }
0150: }
0151:
0152: TimeZone saveZone = TimeZone.getDefault();
0153: try {
0154: TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
0155: //NEWCAL
0156: Date PURE_GREGORIAN = new Date(Long.MIN_VALUE);
0157: Date PURE_JULIAN = new Date(Long.MAX_VALUE);
0158: GregorianCalendar cal = new GregorianCalendar();
0159: final int EPOCH_JULIAN = 2440588;
0160: final long ONE_DAY = 24 * 60 * 60 * 1000L;
0161: int[] DATA = {
0162: // Julian# Year Month DOM JULIAN:Year, Month, DOM
0163: 2440588, 1970, Calendar.JANUARY, 1, 1969,
0164: Calendar.DECEMBER, 19, 2415080, 1900,
0165: Calendar.MARCH, 1, 1900, Calendar.FEBRUARY, 17,
0166: 2451604, 2000, Calendar.FEBRUARY, 29, 2000,
0167: Calendar.FEBRUARY, 16, 2452269, 2001,
0168: Calendar.DECEMBER, 25, 2001, Calendar.DECEMBER, 12,
0169: 2416526, 1904, Calendar.FEBRUARY, 15, 1904,
0170: Calendar.FEBRUARY, 2, 2416656, 1904, Calendar.JUNE,
0171: 24, 1904, Calendar.JUNE, 11, 1721426, 1,
0172: Calendar.JANUARY, 1, 1, Calendar.JANUARY, 3,
0173: 2000000, 763, Calendar.SEPTEMBER, 18, 763,
0174: Calendar.SEPTEMBER, 14, 4000000, 6239,
0175: Calendar.JULY, 12, 6239, Calendar.MAY, 28, 8000000,
0176: 17191, Calendar.FEBRUARY, 26, 17190,
0177: Calendar.OCTOBER, 22, 10000000, 22666,
0178: Calendar.DECEMBER, 20, 22666, Calendar.JULY, 5, };
0179: for (int i = 0; i < DATA.length; i += 7) {
0180: int julian = DATA[i];
0181: int year = DATA[i + 1];
0182: int month = DATA[i + 2];
0183: int dom = DATA[i + 3];
0184: int year2, month2, dom2;
0185: long millis = (julian - EPOCH_JULIAN) * ONE_DAY;
0186: String s;
0187:
0188: // Test Gregorian computation
0189: cal.setGregorianChange(PURE_GREGORIAN);
0190: cal.clear();
0191: cal.set(year, month, dom);
0192: long calMillis = cal.getTime().getTime();
0193: long delta = calMillis - millis;
0194: cal.setTime(new Date(millis));
0195: year2 = cal.get(Calendar.YEAR);
0196: month2 = cal.get(Calendar.MONTH);
0197: dom2 = cal.get(Calendar.DAY_OF_MONTH);
0198: s = "G " + year + "-" + (month + 1 - Calendar.JANUARY)
0199: + "-" + dom + " => " + calMillis + " ("
0200: + ((float) delta / ONE_DAY) + " day delta) => "
0201: + year2 + "-" + (month2 + 1 - Calendar.JANUARY)
0202: + "-" + dom2;
0203: if (delta != 0 || year != year2 || month != month2
0204: || dom != dom2)
0205: errln(s + " FAIL");
0206: else
0207: logln(s);
0208:
0209: // Test Julian computation
0210: year = DATA[i + 4];
0211: month = DATA[i + 5];
0212: dom = DATA[i + 6];
0213: cal.setGregorianChange(PURE_JULIAN);
0214: cal.clear();
0215: cal.set(year, month, dom);
0216: calMillis = cal.getTime().getTime();
0217: delta = calMillis - millis;
0218: cal.setTime(new Date(millis));
0219: year2 = cal.get(Calendar.YEAR);
0220: month2 = cal.get(Calendar.MONTH);
0221: dom2 = cal.get(Calendar.DAY_OF_MONTH);
0222: s = "J " + year + "-" + (month + 1 - Calendar.JANUARY)
0223: + "-" + dom + " => " + calMillis + " ("
0224: + ((float) delta / ONE_DAY) + " day delta) => "
0225: + year2 + "-" + (month2 + 1 - Calendar.JANUARY)
0226: + "-" + dom2;
0227: if (delta != 0 || year != year2 || month != month2
0228: || dom != dom2)
0229: errln(s + " FAIL");
0230: else
0231: logln(s);
0232: }
0233:
0234: java.util.Calendar tempcal = java.util.Calendar
0235: .getInstance();
0236: tempcal.clear();
0237: tempcal.set(1582, Calendar.OCTOBER, 15);
0238: cal.setGregorianChange(tempcal.getTime());
0239: auxMapping(cal, 1582, Calendar.OCTOBER, 4);
0240: auxMapping(cal, 1582, Calendar.OCTOBER, 15);
0241: auxMapping(cal, 1582, Calendar.OCTOBER, 16);
0242: for (int y = 800; y < 3000; y += 1 + (int) (100 * Math
0243: .random())) {
0244: for (int m = Calendar.JANUARY; m <= Calendar.DECEMBER; ++m) {
0245: auxMapping(cal, y, m, 15);
0246: }
0247: }
0248: } finally {
0249: TimeZone.setDefault(saveZone);
0250: }
0251: }
0252:
0253: private void auxMapping(Calendar cal, int y, int m, int d) {
0254: cal.clear();
0255: cal.set(y, m, d);
0256: long millis = cal.getTime().getTime();
0257: cal.setTime(new Date(millis));
0258: int year2 = cal.get(Calendar.YEAR);
0259: int month2 = cal.get(Calendar.MONTH);
0260: int dom2 = cal.get(Calendar.DAY_OF_MONTH);
0261: if (y != year2 || m != month2 || dom2 != d)
0262: errln("Round-trip failure: " + y + "-" + (m + 1) + "-" + d
0263: + " =>ms=> " + year2 + "-" + (month2 + 1) + "-"
0264: + dom2);
0265: }
0266:
0267: public void TestGenericAPI() {
0268: // not used String str;
0269:
0270: java.util.Calendar tempcal = java.util.Calendar.getInstance();
0271: tempcal.clear();
0272: tempcal.set(1990, Calendar.APRIL, 15);
0273: Date when = tempcal.getTime();
0274:
0275: String tzid = "TestZone";
0276: int tzoffset = 123400;
0277:
0278: SimpleTimeZone zone = new SimpleTimeZone(tzoffset, tzid);
0279: Calendar cal = (Calendar) Calendar
0280: .getInstance((SimpleTimeZone) zone.clone());
0281:
0282: if (!zone.equals(cal.getTimeZone()))
0283: errln("FAIL: Calendar.getTimeZone failed");
0284:
0285: Calendar cal2 = Calendar.getInstance(cal.getTimeZone());
0286:
0287: cal.setTime(when);
0288: cal2.setTime(when);
0289:
0290: if (!(cal.equals(cal2)))
0291: errln("FAIL: Calendar.operator== failed");
0292: // if ((*cal != *cal2)) errln("FAIL: Calendar.operator!= failed");
0293: if (!cal.equals(cal2) || cal.before(cal2) || cal.after(cal2))
0294: errln("FAIL: equals/before/after failed");
0295:
0296: cal2.setTime(new Date(when.getTime() + 1000));
0297: if (cal.equals(cal2) || cal2.before(cal) || cal.after(cal2))
0298: errln("FAIL: equals/before/after failed");
0299:
0300: cal.roll(Calendar.SECOND, true);
0301: if (!cal.equals(cal2) || cal.before(cal2) || cal.after(cal2))
0302: errln("FAIL: equals/before/after failed");
0303:
0304: // Roll back to January
0305: cal.roll(Calendar.MONTH, (int) (1 + Calendar.DECEMBER - cal
0306: .get(Calendar.MONTH)));
0307: if (cal.equals(cal2) || cal2.before(cal) || cal.after(cal2))
0308: errln("FAIL: equals/before/after failed");
0309:
0310: // C++ only
0311: /* TimeZone z = cal.orphanTimeZone();
0312: if (z.getID(str) != tzid ||
0313: z.getRawOffset() != tzoffset)
0314: errln("FAIL: orphanTimeZone failed");
0315: */
0316:
0317: for (int i = 0; i < 2; ++i) {
0318: boolean lenient = (i > 0);
0319: cal.setLenient(lenient);
0320: if (lenient != cal.isLenient())
0321: errln("FAIL: setLenient/isLenient failed");
0322: // Later: Check for lenient behavior
0323: }
0324:
0325: int i;
0326: for (i = Calendar.SUNDAY; i <= Calendar.SATURDAY; ++i) {
0327: cal.setFirstDayOfWeek(i);
0328: if (cal.getFirstDayOfWeek() != i)
0329: errln("FAIL: set/getFirstDayOfWeek failed");
0330: }
0331:
0332: for (i = 1; i <= 7; ++i) {
0333: cal.setMinimalDaysInFirstWeek(i);
0334: if (cal.getMinimalDaysInFirstWeek() != i)
0335: errln("FAIL: set/getFirstDayOfWeek failed");
0336: }
0337:
0338: for (i = 0; i < cal.getFieldCount(); ++i) {
0339: if (cal.getMinimum(i) != cal.getGreatestMinimum(i))
0340: errln("FAIL: getMinimum doesn't match getGreatestMinimum for field "
0341: + i);
0342: if (cal.getLeastMaximum(i) > cal.getMaximum(i))
0343: errln("FAIL: getLeastMaximum larger than getMaximum for field "
0344: + i);
0345: if (cal.getMinimum(i) >= cal.getMaximum(i))
0346: errln("FAIL: getMinimum not less than getMaximum for field "
0347: + i);
0348: }
0349:
0350: cal.setTimeZone(TimeZone.getDefault());
0351: cal.clear();
0352: cal.set(1984, 5, 24);
0353: tempcal.clear();
0354: tempcal.set(1984, 5, 24);
0355: if (cal.getTime().getTime() != tempcal.getTime().getTime()) {
0356: errln("FAIL: Calendar.set(3 args) failed");
0357: logln(" Got: " + cal.getTime() + " Expected: "
0358: + tempcal.getTime());
0359: }
0360:
0361: cal.clear();
0362: cal.set(1985, 3, 2, 11, 49);
0363: tempcal.clear();
0364: tempcal.set(1985, 3, 2, 11, 49);
0365: if (cal.getTime().getTime() != tempcal.getTime().getTime()) {
0366: errln("FAIL: Calendar.set(5 args) failed");
0367: logln(" Got: " + cal.getTime() + " Expected: "
0368: + tempcal.getTime());
0369: }
0370:
0371: cal.clear();
0372: cal.set(1995, 9, 12, 1, 39, 55);
0373: tempcal.clear();
0374: tempcal.set(1995, 9, 12, 1, 39, 55);
0375: if (cal.getTime().getTime() != tempcal.getTime().getTime()) {
0376: errln("FAIL: Calendar.set(6 args) failed");
0377: logln(" Got: " + cal.getTime() + " Expected: "
0378: + tempcal.getTime());
0379: }
0380:
0381: cal.getTime();
0382: // This test is strange -- why do we expect certain fields to be set, and
0383: // others not to be? Revisit the appropriateness of this. - Alan NEWCAL
0384: for (i = 0; i < cal.getFieldCount(); ++i) {
0385: switch (i) {
0386: case Calendar.YEAR:
0387: case Calendar.MONTH:
0388: case Calendar.DATE:
0389: case Calendar.HOUR_OF_DAY:
0390: case Calendar.MINUTE:
0391: case Calendar.SECOND:
0392: case Calendar.EXTENDED_YEAR:
0393: if (!cal.isSet(i))
0394: errln("FAIL: " + FIELD_NAME[i] + " is not set");
0395: break;
0396: default:
0397: if (cal.isSet(i))
0398: errln("FAIL: " + FIELD_NAME[i] + " is set");
0399: }
0400: cal.clear(i);
0401: if (cal.isSet(i))
0402: errln("FAIL: Calendar.clear/isSet failed");
0403: }
0404:
0405: // delete cal;
0406: // delete cal2;
0407:
0408: Locale[] loc = Calendar.getAvailableLocales();
0409: long count = loc.length;
0410: if (count < 1 || loc == null) {
0411: errln("FAIL: getAvailableLocales failed");
0412: } else {
0413: for (i = 0; i < count; ++i) {
0414: cal = Calendar.getInstance(loc[i]);
0415: // delete cal;
0416: }
0417: }
0418:
0419: cal = Calendar.getInstance(TimeZone.getDefault(),
0420: Locale.ENGLISH);
0421: // delete cal;
0422:
0423: cal = Calendar.getInstance(zone, Locale.ENGLISH);
0424: // delete cal;
0425:
0426: GregorianCalendar gc = new GregorianCalendar(zone);
0427: // delete gc;
0428:
0429: gc = new GregorianCalendar(Locale.ENGLISH);
0430: // delete gc;
0431:
0432: gc = new GregorianCalendar(Locale.ENGLISH);
0433: // delete gc;
0434:
0435: gc = new GregorianCalendar(zone, Locale.ENGLISH);
0436: // delete gc;
0437:
0438: gc = new GregorianCalendar(zone);
0439: // delete gc;
0440:
0441: gc = new GregorianCalendar(1998, 10, 14, 21, 43);
0442: tempcal.clear();
0443: tempcal.set(1998, 10, 14, 21, 43);
0444: if (gc.getTime().getTime() != tempcal.getTime().getTime())
0445: errln("FAIL: new GregorianCalendar(ymdhm) failed");
0446: // delete gc;
0447:
0448: gc = new GregorianCalendar(1998, 10, 14, 21, 43, 55);
0449: tempcal.clear();
0450: tempcal.set(1998, 10, 14, 21, 43, 55);
0451: if (gc.getTime().getTime() != tempcal.getTime().getTime())
0452: errln("FAIL: new GregorianCalendar(ymdhms) failed");
0453:
0454: // C++ only:
0455: // GregorianCalendar gc2 = new GregorianCalendar(Locale.ENGLISH);
0456: // gc2 = gc;
0457: // if (gc2 != gc || !(gc2 == gc)) errln("FAIL: GregorianCalendar assignment/operator==/operator!= failed");
0458: // delete gc;
0459: // delete z;
0460: }
0461:
0462: // Verify Roger Webster's bug
0463: public void TestRog() {
0464: GregorianCalendar gc = new GregorianCalendar();
0465:
0466: int year = 1997, month = Calendar.APRIL, date = 1;
0467: gc.set(year, month, date); // April 1, 1997
0468:
0469: gc.set(Calendar.HOUR_OF_DAY, 23);
0470: gc.set(Calendar.MINUTE, 0);
0471: gc.set(Calendar.SECOND, 0);
0472: gc.set(Calendar.MILLISECOND, 0);
0473:
0474: for (int i = 0; i < 9; i++, gc.add(Calendar.DATE, 1)) {
0475: if (gc.get(Calendar.YEAR) != year
0476: || gc.get(Calendar.MONTH) != month
0477: || gc.get(Calendar.DATE) != (date + i))
0478: errln("FAIL: Date " + gc.getTime() + " wrong");
0479: }
0480: }
0481:
0482: // Verify DAY_OF_WEEK
0483: public void TestDOW943() {
0484: dowTest(false);
0485: dowTest(true);
0486: }
0487:
0488: void dowTest(boolean lenient) {
0489: GregorianCalendar cal = new GregorianCalendar();
0490: cal.set(1997, Calendar.AUGUST, 12); // Wednesday
0491: cal.getTime(); // Force update
0492: cal.setLenient(lenient);
0493: cal.set(1996, Calendar.DECEMBER, 1); // Set the date to be December 1, 1996
0494: int dow = cal.get(Calendar.DAY_OF_WEEK);
0495: int min = cal.getMinimum(Calendar.DAY_OF_WEEK);
0496: int max = cal.getMaximum(Calendar.DAY_OF_WEEK);
0497: if (dow < min || dow > max)
0498: errln("FAIL: Day of week " + dow + " out of range");
0499: if (dow != Calendar.SUNDAY) {
0500: errln("FAIL2: Day of week should be SUNDAY; is " + dow
0501: + ": " + cal.getTime());
0502: }
0503: if (min != Calendar.SUNDAY || max != Calendar.SATURDAY)
0504: errln("FAIL: Min/max bad");
0505: }
0506:
0507: // Verify that the clone method produces distinct objects with no
0508: // unintentionally shared fields.
0509: public void TestClonesUnique908() {
0510: Calendar c = Calendar.getInstance();
0511: Calendar d = (Calendar) c.clone();
0512: c.set(Calendar.MILLISECOND, 123);
0513: d.set(Calendar.MILLISECOND, 456);
0514: if (c.get(Calendar.MILLISECOND) != 123
0515: || d.get(Calendar.MILLISECOND) != 456) {
0516: errln("FAIL: Clones share fields");
0517: }
0518: }
0519:
0520: // Verify effect of Gregorian cutoff value
0521: public void TestGregorianChange768() {
0522: boolean b;
0523: GregorianCalendar c = new GregorianCalendar();
0524: logln("With cutoff " + c.getGregorianChange());
0525: logln(" isLeapYear(1800) = " + (b = c.isLeapYear(1800)));
0526: logln(" (should be FALSE)");
0527: if (b != false)
0528: errln("FAIL");
0529: java.util.Calendar tempcal = java.util.Calendar.getInstance();
0530: tempcal.clear();
0531: tempcal.set(1900, 0, 1);
0532: c.setGregorianChange(tempcal.getTime()); // Jan 1 1900
0533: logln("With cutoff " + c.getGregorianChange());
0534: logln(" isLeapYear(1800) = " + (b = c.isLeapYear(1800)));
0535: logln(" (should be TRUE)");
0536: if (b != true)
0537: errln("FAIL");
0538: }
0539:
0540: // Test the correct behavior of the disambiguation algorithm.
0541: public void TestDisambiguation765() throws Exception {
0542: Calendar c = Calendar.getInstance();
0543: c.setLenient(false);
0544:
0545: c.clear();
0546: c.set(Calendar.YEAR, 1997);
0547: c.set(Calendar.MONTH, Calendar.JUNE);
0548: c.set(Calendar.DATE, 3);
0549:
0550: verify765("1997 third day of June = ", c, 1997, Calendar.JUNE,
0551: 3);
0552:
0553: c.clear();
0554: c.set(Calendar.YEAR, 1997);
0555: c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
0556: c.set(Calendar.MONTH, Calendar.JUNE);
0557: c.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
0558: verify765("1997 first Tuesday in June = ", c, 1997,
0559: Calendar.JUNE, 3);
0560:
0561: c.clear();
0562: c.set(Calendar.YEAR, 1997);
0563: c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
0564: c.set(Calendar.MONTH, Calendar.JUNE);
0565: c.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1);
0566: verify765("1997 last Tuesday in June = ", c, 1997,
0567: Calendar.JUNE, 24);
0568:
0569: IllegalArgumentException e = null;
0570: try {
0571: c.clear();
0572: c.set(Calendar.YEAR, 1997);
0573: c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
0574: c.set(Calendar.MONTH, Calendar.JUNE);
0575: c.set(Calendar.DAY_OF_WEEK_IN_MONTH, 0);
0576: c.getTime();
0577: } catch (IllegalArgumentException ex) {
0578: e = ex;
0579: }
0580: verify765("1997 zero-th Tuesday in June = ", e, c);
0581:
0582: c.clear();
0583: c.set(Calendar.YEAR, 1997);
0584: c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
0585: c.set(Calendar.MONTH, Calendar.JUNE);
0586: c.set(Calendar.WEEK_OF_MONTH, 1);
0587: verify765("1997 Tuesday in week 1 of June = ", c, 1997,
0588: Calendar.JUNE, 3);
0589:
0590: c.clear();
0591: c.set(Calendar.YEAR, 1997);
0592: c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
0593: c.set(Calendar.MONTH, Calendar.JUNE);
0594: c.set(Calendar.WEEK_OF_MONTH, 5);
0595: verify765("1997 Tuesday in week 5 of June = ", c, 1997,
0596: Calendar.JULY, 1);
0597:
0598: try {
0599: c.clear();
0600: c.set(Calendar.YEAR, 1997);
0601: c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
0602: c.set(Calendar.MONTH, Calendar.JUNE);
0603: c.set(Calendar.WEEK_OF_MONTH, 0);
0604: verify765("1997 Tuesday in week 0 of June = ", c, 1997,
0605: Calendar.MAY, 27);
0606: } catch (IllegalArgumentException ex) {
0607: errln("FAIL: Exception seen:");
0608: // ex.printStackTrace(log);
0609: }
0610:
0611: c.clear();
0612: c.set(Calendar.YEAR, 1997);
0613: c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
0614: c.set(Calendar.WEEK_OF_YEAR, 1);
0615: verify765("1997 Tuesday in week 1 of year = ", c, 1996,
0616: Calendar.DECEMBER, 31);
0617:
0618: c.clear();
0619: c.set(Calendar.YEAR, 1997);
0620: c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
0621: c.set(Calendar.WEEK_OF_YEAR, 10);
0622: verify765("1997 Tuesday in week 10 of year = ", c, 1997,
0623: Calendar.MARCH, 4);
0624:
0625: try {
0626: c.clear();
0627: c.set(Calendar.YEAR, 1997);
0628: c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
0629: c.set(Calendar.WEEK_OF_YEAR, 0);
0630: verify765("1997 Tuesday in week 0 of year = ", c, 1996,
0631: Calendar.DECEMBER, 24);
0632: throw new Exception(
0633: "Fail: WEEK_OF_YEAR 0 should be illegal");
0634: } catch (IllegalArgumentException ex) {
0635: System.out.print("");
0636: }
0637: }
0638:
0639: void verify765(String msg, Calendar c, int year, int month, int day) {
0640: int cy = c.get(Calendar.YEAR); // NEWCAL
0641: int cm = c.get(Calendar.MONTH);
0642: int cd = c.get(Calendar.DATE);
0643: if (cy == year && cm == month && cd == day) {
0644: logln("PASS: " + msg + c.getTime());
0645: } else {
0646: errln("FAIL: " + msg + cy + "/" + (cm + 1) + "/" + cd + "="
0647: + c.getTime() + "; expected " + year + "/"
0648: + (month + 1) + "/" + day);
0649: }
0650: }
0651:
0652: // Called when e expected to be non-null
0653: void verify765(String msg, IllegalArgumentException e, Calendar c) {
0654: if (e == null)
0655: errln("FAIL: No IllegalArgumentException for " + msg
0656: + c.getTime());
0657: else
0658: logln("PASS: " + msg + "IllegalArgument as expected");
0659: }
0660:
0661: // Test the behavior of GMT vs. local time
0662: public void TestGMTvsLocal4064654() {
0663: // Sample output 1:
0664: // % /usr/local/java/jdk1.1.3/solaris/bin/java test 1997 1 1 12 0 0
0665: // date = Wed Jan 01 04:00:00 PST 1997
0666: // offset for Wed Jan 01 04:00:00 PST 1997= -8hr
0667: aux4064654(1997, 1, 1, 12, 0, 0);
0668:
0669: // Sample output 2:
0670: // % /usr/local/java/jdk1.1.3/solaris/bin/java test 1997 4 16 18 30 0
0671: // date = Wed Apr 16 10:30:00 PDT 1997
0672: // offset for Wed Apr 16 10:30:00 PDT 1997= -7hr
0673:
0674: // Note that in sample output 2 according to the offset, the gmt time
0675: // of the result would be 1997 4 16 17 30 0 which is different from the
0676: // input of 1997 4 16 18 30 0.
0677: aux4064654(1997, 4, 16, 18, 30, 0);
0678: }
0679:
0680: void aux4064654(int yr, int mo, int dt, int hr, int mn, int sc) {
0681: Date date;
0682: Calendar gmtcal = Calendar.getInstance();
0683: gmtcal.setTimeZone(TimeZone.getTimeZone("Africa/Casablanca"));
0684: gmtcal.set(yr, mo - 1, dt, hr, mn, sc);
0685: gmtcal.set(Calendar.MILLISECOND, 0);
0686:
0687: date = gmtcal.getTime();
0688: logln("date = " + date);
0689:
0690: Calendar cal = Calendar.getInstance();
0691: cal.setTimeZone(TimeZone.getTimeZone("America/Los_Angels"));
0692: cal.setTime(date);
0693:
0694: int offset = cal.getTimeZone().getOffset(cal.get(Calendar.ERA),
0695: cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
0696: cal.get(Calendar.DATE), cal.get(Calendar.DAY_OF_WEEK),
0697: cal.get(Calendar.MILLISECOND));
0698:
0699: logln("offset for " + date + "= " + (offset / 1000 / 60 / 60.0)
0700: + "hr");
0701:
0702: int utc = ((cal.get(Calendar.HOUR_OF_DAY) * 60 + cal
0703: .get(Calendar.MINUTE)) * 60 + cal.get(Calendar.SECOND))
0704: * 1000 + cal.get(Calendar.MILLISECOND) - offset;
0705:
0706: int expected = ((hr * 60 + mn) * 60 + sc) * 1000;
0707:
0708: if (utc != expected)
0709: errln("FAIL: Discrepancy of " + (utc - expected)
0710: + " millis = "
0711: + ((utc - expected) / 1000 / 60 / 60.0) + " hr");
0712: }
0713:
0714: // Verify that add and set work regardless of the order in which
0715: // they are called.
0716: public void TestAddSetOrder621() {
0717: java.util.Calendar tempcal = java.util.Calendar.getInstance();
0718: tempcal.clear();
0719: tempcal.set(1997, 4, 14, 13, 23, 45);
0720: Date d = tempcal.getTime();
0721:
0722: Calendar cal = Calendar.getInstance();
0723: cal.setTime(d);
0724: cal.add(Calendar.DATE, -5);
0725: cal.set(Calendar.HOUR_OF_DAY, 0);
0726: cal.set(Calendar.MINUTE, 0);
0727: cal.set(Calendar.SECOND, 0);
0728: // ma feb 03 00:00:00 GMT+00:00 1997
0729: String s = cal.getTime().toString();
0730:
0731: cal = Calendar.getInstance();
0732: cal.setTime(d);
0733: cal.set(Calendar.HOUR_OF_DAY, 0);
0734: cal.set(Calendar.MINUTE, 0);
0735: cal.set(Calendar.SECOND, 0);
0736: cal.add(Calendar.DATE, -5);
0737: // ma feb 03 13:11:06 GMT+00:00 1997
0738: String s2 = cal.getTime().toString();
0739:
0740: if (s.equals(s2))
0741: logln("Pass: " + s + " == " + s2);
0742: else
0743: errln("FAIL: " + s + " != " + s2);
0744: }
0745:
0746: // Verify that add works.
0747: public void TestAdd520() {
0748: int y = 1997, m = Calendar.FEBRUARY, d = 1;
0749: GregorianCalendar temp = new GregorianCalendar(y, m, d);
0750: check520(temp, y, m, d);
0751:
0752: temp.add(Calendar.YEAR, 1);
0753: y++;
0754: check520(temp, y, m, d);
0755:
0756: temp.add(Calendar.MONTH, 1);
0757: m++;
0758: check520(temp, y, m, d);
0759:
0760: temp.add(Calendar.DATE, 1);
0761: d++;
0762: check520(temp, y, m, d);
0763:
0764: temp.add(Calendar.DATE, 2);
0765: d += 2;
0766: check520(temp, y, m, d);
0767:
0768: temp.add(Calendar.DATE, 28);
0769: d = 1;
0770: ++m;
0771: check520(temp, y, m, d);
0772: }
0773:
0774: void check520(Calendar c, int y, int m, int d) {
0775: if (c.get(Calendar.YEAR) != y || c.get(Calendar.MONTH) != m
0776: || c.get(Calendar.DATE) != d) {
0777: errln("FAILURE: Expected YEAR/MONTH/DATE of " + y + "/"
0778: + (m + 1) + "/" + d + "; got "
0779: + c.get(Calendar.YEAR) + "/"
0780: + (c.get(Calendar.MONTH) + 1) + "/"
0781: + c.get(Calendar.DATE));
0782: } else
0783: logln("Confirmed: " + y + "/" + (m + 1) + "/" + d);
0784: }
0785:
0786: // Verify that setting fields works. This test fails when an exception is thrown.
0787: public void TestFieldSet4781() {
0788: try {
0789: GregorianCalendar g = new GregorianCalendar();
0790: GregorianCalendar g2 = new GregorianCalendar();
0791: // At this point UTC value is set, various fields are not.
0792: // Now set to noon.
0793: g2.set(Calendar.HOUR, 12);
0794: g2.set(Calendar.MINUTE, 0);
0795: g2.set(Calendar.SECOND, 0);
0796: // At this point the object thinks UTC is NOT set, but fields are set.
0797: // The following line will result in IllegalArgumentException because
0798: // it thinks the YEAR is set and it is NOT.
0799: if (g2.equals(g))
0800: logln("Same");
0801: else
0802: logln("Different");
0803: } catch (IllegalArgumentException e) {
0804: errln("Unexpected exception seen: " + e);
0805: }
0806: }
0807:
0808: // Test serialization of a Calendar object
0809: public void TestSerialize337() {
0810: Calendar cal = Calendar.getInstance();
0811:
0812: boolean ok = false;
0813:
0814: try {
0815: ByteArrayOutputStream f = new ByteArrayOutputStream();
0816: ObjectOutput s = new ObjectOutputStream(f);
0817: s.writeObject(PREFIX);
0818: s.writeObject(cal);
0819: s.writeObject(POSTFIX);
0820: f.close();
0821:
0822: ByteArrayInputStream in = new ByteArrayInputStream(f
0823: .toByteArray());
0824: ObjectInputStream t = new ObjectInputStream(in);
0825: String pre = (String) t.readObject();
0826: Calendar c = (Calendar) t.readObject();
0827: String post = (String) t.readObject();
0828: in.close();
0829:
0830: ok = pre.equals(PREFIX) && post.equals(POSTFIX)
0831: && cal.equals(c);
0832: } catch (IOException e) {
0833: errln("FAIL: Exception received:");
0834: // e.printStackTrace(log);
0835: } catch (ClassNotFoundException e) {
0836: errln("FAIL: Exception received:");
0837: // e.printStackTrace(log);
0838: }
0839:
0840: if (!ok)
0841: errln("Serialization of Calendar object failed.");
0842: }
0843:
0844: static final String PREFIX = "abc";
0845: static final String POSTFIX = "def";
0846: static final String FILENAME = "tmp337.bin";
0847:
0848: // Try to zero out the seconds field
0849: public void TestSecondsZero121() {
0850: Calendar cal = new GregorianCalendar();
0851: // Initialize with current date/time
0852: cal.setTime(new Date());
0853: // Round down to minute
0854: cal.set(Calendar.SECOND, 0);
0855: Date d = cal.getTime();
0856: String s = d.toString();
0857: if (s.indexOf(":00 ") < 0)
0858: errln("Expected to see :00 in " + s);
0859: }
0860:
0861: // Try various sequences of add, set, and get method calls.
0862: public void TestAddSetGet0610() {
0863: //
0864: // Error case 1:
0865: // - Upon initialization calendar fields, millis = System.currentTime
0866: // - After set is called fields are initialized, time is not
0867: // - Addition uses millis which are still *now*
0868: //
0869: {
0870: Calendar calendar = new GregorianCalendar();
0871: calendar.set(1993, Calendar.JANUARY, 4);
0872: logln("1A) " + value(calendar));
0873: calendar.add(Calendar.DATE, 1);
0874: String v = value(calendar);
0875: logln("1B) " + v);
0876: logln("--) 1993/0/5");
0877: if (!v.equals(EXPECTED_0610))
0878: errln("Expected " + EXPECTED_0610 + "; saw " + v);
0879: }
0880:
0881: //
0882: // Error case 2:
0883: // - Upon initialization calendar fields set, millis = 0
0884: // - Addition uses millis which are still 1970, 0, 1
0885: //
0886:
0887: {
0888: Calendar calendar = new GregorianCalendar(1993,
0889: Calendar.JANUARY, 4);
0890: logln("2A) " + value(calendar));
0891: calendar.add(Calendar.DATE, 1);
0892: String v = value(calendar);
0893: logln("2B) " + v);
0894: logln("--) 1993/0/5");
0895: if (!v.equals(EXPECTED_0610))
0896: errln("Expected " + EXPECTED_0610 + "; saw " + v);
0897: }
0898:
0899: //
0900: // Error case 3:
0901: // - Upon initialization calendar fields, millis = 0
0902: // - getTime( ) is called which forces the millis to be set
0903: // - Addition uses millis which are correct
0904: //
0905:
0906: {
0907: Calendar calendar = new GregorianCalendar(1993,
0908: Calendar.JANUARY, 4);
0909: logln("3A) " + value(calendar));
0910: calendar.getTime();
0911: calendar.add(Calendar.DATE, 1);
0912: String v = value(calendar);
0913: logln("3B) " + v);
0914: logln("--) 1993/0/5");
0915: if (!v.equals(EXPECTED_0610))
0916: errln("Expected " + EXPECTED_0610 + "; saw " + v);
0917: }
0918: }
0919:
0920: static String value(Calendar calendar) {
0921: return (calendar.get(Calendar.YEAR) + "/"
0922: + calendar.get(Calendar.MONTH) + "/" + calendar
0923: .get(Calendar.DATE));
0924: }
0925:
0926: static String EXPECTED_0610 = "1993/0/5";
0927:
0928: // Test that certain fields on a certain date are as expected.
0929: public void TestFields060() {
0930: int year = 1997;
0931: int month = java.util.Calendar.OCTOBER; //october
0932: int dDate = 22; //DAYOFWEEK should return 3 for Wednesday
0933: GregorianCalendar calendar = null;
0934:
0935: calendar = new GregorianCalendar(year, month, dDate);
0936: for (int i = 0; i < EXPECTED_FIELDS.length;) {
0937: int field = EXPECTED_FIELDS[i++];
0938: int expected = EXPECTED_FIELDS[i++];
0939: if (calendar.get(field) != expected) {
0940: errln("Expected field " + field + " to have value "
0941: + expected + "; received "
0942: + calendar.get(field) + " instead");
0943: }
0944: }
0945: }
0946:
0947: static int EXPECTED_FIELDS[] = { Calendar.YEAR, 1997,
0948: Calendar.MONTH, Calendar.OCTOBER, Calendar.DAY_OF_MONTH,
0949: 22, Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY,
0950: Calendar.DAY_OF_WEEK_IN_MONTH, 4, Calendar.DAY_OF_YEAR, 295 };
0951:
0952: // Verify that the fields are as expected (mostly zero) at the epoch start.
0953: // Note that we adjust for the default timezone to get most things to zero.
0954: public void TestEpochStartFields() {
0955: TimeZone z = TimeZone.getDefault();
0956: Calendar c = Calendar.getInstance();
0957: Date d = new Date(-z.getRawOffset());
0958: if (z.inDaylightTime(d)) {
0959: logln("Warning: Skipping test because " + d + " is in DST.");
0960: } else {
0961: c.setTime(d);
0962: for (int i = 0; i < Calendar.ZONE_OFFSET; ++i) {
0963: if (c.get(i) != EPOCH_FIELDS[i])
0964: errln("Expected field " + i + " to have value "
0965: + EPOCH_FIELDS[i] + "; saw " + c.get(i)
0966: + " instead");
0967: }
0968: if (c.get(Calendar.ZONE_OFFSET) != z.getRawOffset())
0969: errln("Expected field ZONE_OFFSET to have value "
0970: + z.getRawOffset() + "; saw "
0971: + c.get(Calendar.ZONE_OFFSET) + " instead");
0972: if (c.get(Calendar.DST_OFFSET) != 0)
0973: errln("Expected field DST_OFFSET to have value 0"
0974: + "; saw " + c.get(Calendar.DST_OFFSET)
0975: + " instead");
0976: }
0977: }
0978:
0979: // These are the fields at the epoch start
0980: static int EPOCH_FIELDS[] = { 1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0,
0981: 0, 0, 0, 0, -28800000, 0 };
0982:
0983: // Verify that as you add days to the calendar (e.g., 24 day periods),
0984: // the day of the week shifts in the expected pattern.
0985: public void TestDOWProgression() {
0986: Calendar cal = new GregorianCalendar(1972, Calendar.OCTOBER, 26);
0987: marchByDelta(cal, 24); // Last parameter must be != 0 modulo 7
0988: }
0989:
0990: // Supply a delta which is not a multiple of 7.
0991: void marchByDelta(Calendar cal, int delta) {
0992: Calendar cur = (Calendar) cal.clone();
0993: int initialDOW = cur.get(Calendar.DAY_OF_WEEK);
0994: int DOW, newDOW = initialDOW;
0995: do {
0996: DOW = newDOW;
0997: logln("DOW = " + DOW + " " + cur.getTime());
0998:
0999: cur.add(Calendar.DAY_OF_WEEK, delta);
1000: newDOW = cur.get(Calendar.DAY_OF_WEEK);
1001: int expectedDOW = 1 + (DOW + delta - 1) % 7;
1002: if (newDOW != expectedDOW) {
1003: errln("Day of week should be " + expectedDOW
1004: + " instead of " + newDOW + " on "
1005: + cur.getTime());
1006: return;
1007: }
1008: } while (newDOW != initialDOW);
1009: }
1010:
1011: public void TestActualMinMax() {
1012: Calendar cal = new GregorianCalendar(1967, Calendar.MARCH, 10);
1013: cal.setFirstDayOfWeek(Calendar.SUNDAY);
1014: cal.setMinimalDaysInFirstWeek(3);
1015:
1016: if (cal.getActualMinimum(Calendar.DAY_OF_MONTH) != 1)
1017: errln("Actual minimum date for 3/10/1967 should have been 1; got "
1018: + cal.getActualMinimum(Calendar.DAY_OF_MONTH));
1019: if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 31)
1020: errln("Actual maximum date for 3/10/1967 should have been 31; got "
1021: + cal.getActualMaximum(Calendar.DAY_OF_MONTH));
1022:
1023: cal.set(Calendar.MONTH, Calendar.FEBRUARY);
1024: if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 28)
1025: errln("Actual maximum date for 2/10/1967 should have been 28; got "
1026: + cal.getActualMaximum(Calendar.DAY_OF_MONTH));
1027: if (cal.getActualMaximum(Calendar.DAY_OF_YEAR) != 365)
1028: errln("Number of days in 1967 should have been 365; got "
1029: + cal.getActualMaximum(Calendar.DAY_OF_YEAR));
1030:
1031: cal.set(Calendar.YEAR, 1968);
1032: if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 29)
1033: errln("Actual maximum date for 2/10/1968 should have been 29; got "
1034: + cal.getActualMaximum(Calendar.DAY_OF_MONTH));
1035: if (cal.getActualMaximum(Calendar.DAY_OF_YEAR) != 366)
1036: errln("Number of days in 1968 should have been 366; got "
1037: + cal.getActualMaximum(Calendar.DAY_OF_YEAR));
1038: // Using week settings of SUNDAY/3 (see above)
1039: if (cal.getActualMaximum(Calendar.WEEK_OF_YEAR) != 52)
1040: errln("Number of weeks in 1968 should have been 52; got "
1041: + cal.getActualMaximum(Calendar.WEEK_OF_YEAR));
1042:
1043: cal.set(Calendar.YEAR, 1976);
1044: cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); // Added - Liu 11/6/00
1045: // Using week settings of SUNDAY/3 (see above)
1046: if (cal.getActualMaximum(Calendar.WEEK_OF_YEAR) != 53)
1047: errln("Number of weeks in 1976 should have been 53; got "
1048: + cal.getActualMaximum(Calendar.WEEK_OF_YEAR));
1049: }
1050:
1051: public void TestRoll() {
1052: Calendar cal = new GregorianCalendar(1997, Calendar.JANUARY, 31);
1053:
1054: int[] dayValues = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
1055: 31, 31 };
1056: for (int i = 0; i < dayValues.length; i++) {
1057: Calendar cal2 = (Calendar) cal.clone();
1058: cal2.roll(Calendar.MONTH, i);
1059: if (cal2.get(Calendar.DAY_OF_MONTH) != dayValues[i])
1060: errln("Rolling the month in 1/31/1997 up by " + i
1061: + " should have yielded " + ((i + 1) % 12)
1062: + "/" + dayValues[i]
1063: + "/1997, but actually yielded "
1064: + ((i + 1) % 12) + "/"
1065: + cal2.get(Calendar.DAY_OF_MONTH) + "/1997.");
1066: }
1067:
1068: cal.set(1996, Calendar.FEBRUARY, 29);
1069:
1070: //int[] monthValues = { 1, 2, 2, 2, 1, 2, 2, 2, 1, 2 };
1071: //int[] dayValues2 = { 29, 1, 1, 1, 29, 1, 1, 1, 29, 1 };
1072:
1073: // I've revised the expected values to make more sense -- rolling
1074: // the year should pin the DAY_OF_MONTH. - Liu 11/6/00
1075: int[] monthValues = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
1076: int[] dayValues2 = { 29, 28, 28, 28, 29, 28, 28, 28, 29, 28 };
1077:
1078: for (int i = 0; i < dayValues2.length; i++) {
1079: Calendar cal2 = (Calendar) cal.clone();
1080: cal2.roll(Calendar.YEAR, i);
1081: if (cal2.get(Calendar.DAY_OF_MONTH) != dayValues2[i]
1082: || cal2.get(Calendar.MONTH) != monthValues[i])
1083: errln("Roll 2/29/1996 by " + i + " year: expected "
1084: + (monthValues[i] + 1) + "/" + dayValues2[i]
1085: + "/" + (1996 + i) + ", got "
1086: + (cal2.get(Calendar.MONTH) + 1) + "/"
1087: + cal2.get(Calendar.DAY_OF_MONTH) + "/"
1088: + cal2.get(Calendar.YEAR));
1089: }
1090:
1091: // Test rolling hour of day
1092: cal.set(Calendar.HOUR_OF_DAY, 0);
1093: cal.roll(Calendar.HOUR_OF_DAY, -2);
1094: int f = cal.get(Calendar.HOUR_OF_DAY);
1095: if (f != 22)
1096: errln("Rolling HOUR_OF_DAY=0 delta=-2 gave " + f
1097: + " Wanted 22");
1098: cal.roll(Calendar.HOUR_OF_DAY, 5);
1099: f = cal.get(Calendar.HOUR_OF_DAY);
1100: if (f != 3)
1101: errln("Rolling HOUR_OF_DAY=22 delta=5 gave " + f
1102: + " Wanted 3");
1103: cal.roll(Calendar.HOUR_OF_DAY, 21);
1104: f = cal.get(Calendar.HOUR_OF_DAY);
1105: if (f != 0)
1106: errln("Rolling HOUR_OF_DAY=3 delta=21 gave " + f
1107: + " Wanted 0");
1108:
1109: // Test rolling hour
1110: cal.set(Calendar.HOUR_OF_DAY, 0);
1111: cal.roll(Calendar.HOUR, -2);
1112: f = cal.get(Calendar.HOUR);
1113: if (f != 10)
1114: errln("Rolling HOUR=0 delta=-2 gave " + f + " Wanted 10");
1115: cal.roll(Calendar.HOUR, 5);
1116: f = cal.get(Calendar.HOUR);
1117: if (f != 3)
1118: errln("Rolling HOUR=10 delta=5 gave " + f + " Wanted 3");
1119: cal.roll(Calendar.HOUR, 9);
1120: f = cal.get(Calendar.HOUR);
1121: if (f != 0)
1122: errln("Rolling HOUR=3 delta=9 gave " + f + " Wanted 0");
1123: }
1124:
1125: public void TestComputeJulianDay4406() {
1126: // jb4406 is probably not a bug, this is to document the behavior
1127: GregorianCalendar cal = new GregorianCalendar();
1128: final int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
1129:
1130: logln("julian day value jumps at changeover");
1131: for (int day = 12; day < 18; ++day) {
1132: cal.set(1582, 9, day);
1133: logln("[" + day + "] "
1134: + (cal.getTimeInMillis() / MILLIS_IN_DAY));
1135: }
1136:
1137: logln("\njulian days not accurate before 1 March 0004");
1138: for (int day = 1; day < 3; ++day) {
1139: cal.set(1, 0, day);
1140: logln("[" + day + "] "
1141: + (cal.getTimeInMillis() / MILLIS_IN_DAY));
1142: }
1143:
1144: DateFormat fmt = cal.getDateTimeFormat(DateFormat.LONG, 0,
1145: Locale.getDefault());
1146:
1147: logln("\nswitchover in 1582");
1148: cal.set(1582, 9, 4);
1149: logln(fmt.format(cal));
1150: cal.add(Calendar.DATE, 1);
1151: logln(fmt.format(cal));
1152: cal.set(Calendar.JULIAN_DAY, 1721426);
1153: logln(fmt.format(cal));
1154:
1155: logln("\nlate switchover - proleptic Julian");
1156: cal.set(1582, 9, 4);
1157: cal.setGregorianChange(new Date(Long.MAX_VALUE));
1158: logln(fmt.format(cal));
1159: cal.add(Calendar.DATE, 1);
1160: logln(fmt.format(cal));
1161: cal.set(Calendar.JULIAN_DAY, 1721426);
1162: logln(fmt.format(cal));
1163:
1164: logln("\nearly switchover - proleptic Gregorian");
1165: cal.set(1582, 9, 4);
1166: cal.setGregorianChange(new Date(Long.MIN_VALUE));
1167: logln(fmt.format(cal));
1168: cal.add(Calendar.DATE, 1);
1169: logln(fmt.format(cal));
1170: cal.set(Calendar.JULIAN_DAY, 1721426);
1171: logln(fmt.format(cal));
1172: }
1173: }
1174:
1175: //eof
|