/** ******************************************************************************* * Copyright (C) 2000-2009, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ package com.ibm.icu.dev.test.calendar; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Arrays; import java.util.Date; import java.util.HashSet; import java.util.Locale; import java.util.MissingResourceException; import com.ibm.icu.text.DateFormat; import com.ibm.icu.text.NumberFormat; import com.ibm.icu.text.SimpleDateFormat; import com.ibm.icu.util.Calendar; import com.ibm.icu.util.GregorianCalendar; import com.ibm.icu.util.SimpleTimeZone; import com.ibm.icu.util.TimeZone; import com.ibm.icu.util.ULocale; /** * @test 1.32 99/11/14 * @bug 4031502 4035301 4040996 4051765 4059654 4061476 4070502 4071197 4071385 * 4073929 4083167 4086724 4092362 4095407 4096231 4096539 4100311 4103271 * 4106136 4108764 4114578 4118384 4125881 4125892 4136399 4141665 4142933 * 4145158 4145983 4147269 4149677 4162587 4165343 4166109 4167060 4173516 * 4174361 4177484 4197699 4209071 4288792 */ public class CalendarRegression extends com.ibm.icu.dev.test.TestFmwk { public static void main(String[] args) throws Exception { new CalendarRegression().run(args); } static final String[] FIELD_NAME = { "ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY", "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET", "DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR", "JULIAN_DAY", "MILLISECONDS_IN_DAY" }; /* Synopsis: java.sql.Timestamp constructor works wrong on Windows 95 ==== Here is the test ==== public static void main (String args[]) { java.sql.Timestamp t= new java.sql.Timestamp(0,15,5,5,8,13,123456700); logln("expected=1901-04-05 05:08:13.1234567"); logln(" result="+t); } ==== Here is the output of the test on Solaris or NT ==== expected=1901-04-05 05:08:13.1234567 result=1901-04-05 05:08:13.1234567 ==== Here is the output of the test on Windows95 ==== expected=1901-04-05 05:08:13.1234567 result=1901-04-05 06:08:13.1234567 */ public void Test4031502() { try{ // This bug actually occurs on Windows NT as well, and doesn't // require the host zone to be set; it can be set in Java. String[] ids = TimeZone.getAvailableIDs(); boolean bad = false; for (int i=0; i * * @param d * The date to start from */ public static Date getAssociatedDate(Date d) { GregorianCalendar cal = new GregorianCalendar(); cal.setTime(d); //cal.add(field, amount); //<-- PROBLEM SEEN WITH field = DATE,MONTH // cal.getTime(); // <--- REMOVE THIS TO SEE BUG while (true) { int wd = cal.get(Calendar.DAY_OF_WEEK); if (wd == Calendar.SATURDAY || wd == Calendar.SUNDAY) { cal.add(Calendar.DATE, 1); // cal.getTime(); } else break; } return cal.getTime(); } public void Test4071197() { dowTest(false); dowTest(true); } void dowTest(boolean lenient) { GregorianCalendar cal = new GregorianCalendar(); cal.set(1997, Calendar.AUGUST, 12); // Wednesday // cal.getTime(); // Force update cal.setLenient(lenient); cal.set(1996, Calendar.DECEMBER, 1); // Set the date to be December 1, // 1996 int dow = cal.get(Calendar.DAY_OF_WEEK); int min = cal.getMinimum(Calendar.DAY_OF_WEEK); int max = cal.getMaximum(Calendar.DAY_OF_WEEK); logln(cal.getTime().toString()); if (min != Calendar.SUNDAY || max != Calendar.SATURDAY) errln("FAIL: Min/max bad"); if (dow < min || dow > max) errln("FAIL: Day of week " + dow + " out of range"); if (dow != Calendar.SUNDAY) errln("FAIL: Day of week should be SUNDAY Got " + dow); } public void Test4071385() { // work around bug for jdk1.4 on solaris 2.6, which uses funky timezone // names // jdk1.4.1 will drop support for 2.6 so we should be ok when it comes out java.util.TimeZone javazone = java.util.TimeZone.getTimeZone("GMT"); TimeZone icuzone = TimeZone.getTimeZone("GMT"); Calendar cal = Calendar.getInstance(icuzone); java.util.Calendar tempcal = java.util.Calendar.getInstance(javazone); tempcal.clear(); tempcal.set(1998, Calendar.JUNE, 24); cal.setTime(tempcal.getTime()); cal.set(Calendar.MONTH, Calendar.NOVEMBER); // change a field logln(cal.getTime().toString()); tempcal.set(1998, Calendar.NOVEMBER, 24); if (!cal.getTime().equals(tempcal.getTime())) errln("Fail"); } public void Test4073929() { GregorianCalendar foo1 = new GregorianCalendar(1997, 8, 27); foo1.add(Calendar.DAY_OF_MONTH, +1); int testyear = foo1.get(Calendar.YEAR); int testmonth = foo1.get(Calendar.MONTH); int testday = foo1.get(Calendar.DAY_OF_MONTH); if (testyear != 1997 || testmonth != 8 || testday != 28) errln("Fail: Calendar not initialized"); } public void Test4083167() { TimeZone saveZone = TimeZone.getDefault(); try { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); Date firstDate = new Date(); Calendar cal = new GregorianCalendar(); cal.setTime(firstDate); long firstMillisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L + cal.get(Calendar.MINUTE) * 60000L + cal.get(Calendar.SECOND) * 1000L + cal.get(Calendar.MILLISECOND); logln("Current time: " + firstDate.toString()); for (int validity=0; validity<30; validity++) { Date lastDate = new Date(firstDate.getTime() + (long)validity*1000*24*60*60); cal.setTime(lastDate); long millisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L + cal.get(Calendar.MINUTE) * 60000L + cal.get(Calendar.SECOND) * 1000L + cal.get(Calendar.MILLISECOND); if (firstMillisInDay != millisInDay) errln("Day has shifted " + lastDate); } } finally { TimeZone.setDefault(saveZone); } } public void Test4086724() { SimpleDateFormat date; TimeZone saveZone = TimeZone.getDefault(); Locale saveLocale = Locale.getDefault(); try { Locale.setDefault(Locale.UK); TimeZone.setDefault(TimeZone.getTimeZone("GMT")); date=new SimpleDateFormat("dd MMM yyy (zzzz) 'is in week' ww"); Calendar cal=Calendar.getInstance(); cal.set(1997,Calendar.SEPTEMBER,30); Date now=cal.getTime(); logln(date.format(now)); cal.set(1997,Calendar.JANUARY,1); now=cal.getTime(); logln(date.format(now)); cal.set(1997,Calendar.JANUARY,8); now=cal.getTime(); logln(date.format(now)); cal.set(1996,Calendar.DECEMBER,31); now=cal.getTime(); logln(date.format(now)); } finally { Locale.setDefault(saveLocale); TimeZone.setDefault(saveZone); } logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***"); } public void Test4092362() { GregorianCalendar cal1 = new GregorianCalendar(1997, 10, 11, 10, 20, 40); /* * cal1.set( Calendar.YEAR, 1997 ); cal1.set( Calendar.MONTH, 10 ); * cal1.set( Calendar.DATE, 11 ); cal1.set( Calendar.HOUR, 10 ); * cal1.set( Calendar.MINUTE, 20 ); cal1.set( Calendar.SECOND, 40 ); */ logln( " Cal1 = " + cal1.getTime().getTime() ); logln( " Cal1 time in ms = " + cal1.get(Calendar.MILLISECOND) ); for( int k = 0; k < 100 ; k++ ) { System.out.print(""); } GregorianCalendar cal2 = new GregorianCalendar(1997, 10, 11, 10, 20, 40); /* * cal2.set( Calendar.YEAR, 1997 ); cal2.set( Calendar.MONTH, 10 ); * cal2.set( Calendar.DATE, 11 ); cal2.set( Calendar.HOUR, 10 ); * cal2.set( Calendar.MINUTE, 20 ); cal2.set( Calendar.SECOND, 40 ); */ logln( " Cal2 = " + cal2.getTime().getTime() ); logln( " Cal2 time in ms = " + cal2.get(Calendar.MILLISECOND) ); if( !cal1.equals( cal2 ) ) errln("Fail: Milliseconds randomized"); } public void Test4095407() { GregorianCalendar a = new GregorianCalendar(1997,Calendar.NOVEMBER, 13); int dow = a.get(Calendar.DAY_OF_WEEK); if (dow != Calendar.THURSDAY) errln("Fail: Want THURSDAY Got " + dow); } public void Test4096231() { TimeZone GMT = TimeZone.getTimeZone("GMT"); TimeZone PST = TimeZone.getTimeZone("PST"); int sec = 0, min = 0, hr = 0, day = 1, month = 10, year = 1997; Calendar cal1 = new GregorianCalendar(PST); cal1.setTime(new Date(880698639000L)); int p; logln("PST 1 is: " + (p=cal1.get(Calendar.HOUR_OF_DAY))); cal1.setTimeZone(GMT); // Issue 1: Changing the timezone doesn't change the // represented time. int h1,h2; logln("GMT 1 is: " + (h1=cal1.get(Calendar.HOUR_OF_DAY))); cal1.setTime(new Date(880698639000L)); logln("GMT 2 is: " + (h2=cal1.get(Calendar.HOUR_OF_DAY))); // Note: This test had a bug in it. It wanted h1!=h2, when // what was meant was h1!=p. Fixed this concurrent with fix // to 4177484. if (p == h1 || h1 != h2) errln("Fail: Hour same in different zones"); Calendar cal2 = new GregorianCalendar(GMT); Calendar cal3 = new GregorianCalendar(PST); cal2.set(Calendar.MILLISECOND, 0); cal3.set(Calendar.MILLISECOND, 0); cal2.set(cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH), cal1.get(Calendar.DAY_OF_MONTH), cal1.get(Calendar.HOUR_OF_DAY), cal1.get(Calendar.MINUTE), cal1.get(Calendar.SECOND)); long t1,t2,t3,t4; logln("RGMT 1 is: " + (t1=cal2.getTime().getTime())); cal3.set(year, month, day, hr, min, sec); logln("RPST 1 is: " + (t2=cal3.getTime().getTime())); cal3.setTimeZone(GMT); logln("RGMT 2 is: " + (t3=cal3.getTime().getTime())); cal3.set(cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH), cal1.get(Calendar.DAY_OF_MONTH), cal1.get(Calendar.HOUR_OF_DAY), cal1.get(Calendar.MINUTE), cal1.get(Calendar.SECOND)); // Issue 2: Calendar continues to use the timezone in its // constructor for set() conversions, regardless // of calls to setTimeZone() logln("RGMT 3 is: " + (t4=cal3.getTime().getTime())); if (t1 == t2 || t1 != t4 || t2 != t3) errln("Fail: Calendar zone behavior faulty"); } public void Test4096539() { int[] y = {31,28,31,30,31,30,31,31,30,31,30,31}; for (int x=0;x<12;x++) { GregorianCalendar gc = new GregorianCalendar(1997,x,y[x]); int m1,m2; log((m1=gc.get(Calendar.MONTH)+1)+"/"+ gc.get(Calendar.DATE)+"/"+gc.get(Calendar.YEAR)+ " + 1mo = "); gc.add(Calendar.MONTH, 1); logln((m2=gc.get(Calendar.MONTH)+1)+"/"+ gc.get(Calendar.DATE)+"/"+gc.get(Calendar.YEAR) ); int m = (m1 % 12) + 1; if (m2 != m) errln("Fail: Want " + m + " Got " + m2); } } public void Test4100311() { GregorianCalendar cal = (GregorianCalendar)Calendar.getInstance(); cal.set(Calendar.YEAR, 1997); cal.set(Calendar.DAY_OF_YEAR, 1); Date d = cal.getTime(); // Should be Jan 1 logln(d.toString()); if (cal.get(Calendar.DAY_OF_YEAR) != 1) errln("Fail: DAY_OF_YEAR not set"); } public void Test4103271() { SimpleDateFormat sdf = new SimpleDateFormat(); int numYears=40, startYear=1997, numDays=15; String output, testDesc; GregorianCalendar testCal = (GregorianCalendar)Calendar.getInstance(); testCal.clear(); sdf.setCalendar(testCal); sdf.applyPattern("d MMM yyyy"); boolean fail = false; for (int firstDay=1; firstDay<=2; firstDay++) { for (int minDays=1; minDays<=7; minDays++) { testCal.setMinimalDaysInFirstWeek(minDays); testCal.setFirstDayOfWeek(firstDay); testDesc = ("Test" + String.valueOf(firstDay) + String.valueOf(minDays)); logln(testDesc + " => 1st day of week=" + String.valueOf(firstDay) + ", minimum days in first week=" + String.valueOf(minDays)); for (int j=startYear; j<=startYear+numYears; j++) { testCal.set(j,11,25); for(int i=0; i 53) { Date d = testCal.getTime(); calWOY = String.valueOf(actWOY); output = testDesc + " - " + sdf.format(d) + "\t"; output = output + "\t" + calWOY; logln(output); fail = true; } } } } } int[] DATA = { 3, 52, 52, 52, 52, 52, 52, 52, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 4, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 1, 1, 1, 1, 1, 1, 1, }; testCal.setFirstDayOfWeek(Calendar.SUNDAY); for (int j=0; j " + testCal.getTime()); if (!after.equals(testCal.getTime())) { logln(" exp:" + after + " FAIL"); fail = true; } else logln(" ok"); testCal.setTime(after); if (ADDROLL[i] == ADD) testCal.add(Calendar.WEEK_OF_YEAR, -amount); else testCal.roll(Calendar.WEEK_OF_YEAR, -amount); log((ADDROLL[i]==ADD?"add(WOY,":"roll(WOY,") + (-amount) + ") " + after + " => " + testCal.getTime()); if (!before.equals(testCal.getTime())) { logln(" exp:" + before + " FAIL"); fail = true; } else logln(" ok"); } if (fail) errln("Fail: Week of year misbehaving"); } public void Test4106136() { Locale saveLocale = Locale.getDefault(); String[] names = { "Calendar", "DateFormat", "NumberFormat" }; try { Locale[] locales = { Locale.CHINESE, Locale.CHINA }; for (int i=0; i expect " + d01 + ", got " + cal.getTime()); } cal.set( Calendar.SECOND, 0 ); logln(cal.getTime().toString()); if (!cal.getTime().equals(d00)) errln("Fail: set(SECOND, 0) broken"); cal.setTime(d11); cal.set( Calendar.SECOND, 0 ); logln(cal.getTime().toString()); if (!cal.getTime().equals(d10)) errln("Fail: set(SECOND, 0) broken #2"); cal.clear( Calendar.MINUTE ); logln(cal.getTime().toString()); if (!cal.getTime().equals(d00)) errln("Fail: clear(MINUTE) broken #2"); cal.clear(); logln(cal.getTime().toString()); if (!cal.getTime().equals(epoch)) errln("Fail: after clear() expect " + epoch + ", got " + cal.getTime()); cal.setTime(d11); cal.clear( Calendar.MONTH ); logln(cal.getTime().toString()); if (!cal.getTime().equals(dM)) { errln("Fail: " + d11 + " clear(MONTH) => expect " + dM + ", got " + cal.getTime()); } } public void Test4114578() { int ONE_HOUR = 60*60*1000; Calendar cal = Calendar.getInstance(); cal.setTimeZone(TimeZone.getTimeZone("PST")); java.util.Calendar tempcal = java.util.Calendar.getInstance(); tempcal.clear(); tempcal.set(1998, Calendar.APRIL, 5, 1, 0); long onset = tempcal.getTime().getTime() + ONE_HOUR; tempcal.set(1998, Calendar.OCTOBER, 25, 0, 0); long cease = tempcal.getTime().getTime() + 2*ONE_HOUR; boolean fail = false; final int ADD = 1; final int ROLL = 2; long[] DATA = { // Start Action Amt Expected_change onset - ONE_HOUR, ADD, 1, ONE_HOUR, onset, ADD, -1, -ONE_HOUR, onset - ONE_HOUR, ROLL, 1, ONE_HOUR, onset, ROLL, -1, -ONE_HOUR, cease - ONE_HOUR, ADD, 1, ONE_HOUR, cease, ADD, -1, -ONE_HOUR, cease - ONE_HOUR, ROLL, 1, ONE_HOUR, cease, ROLL, -1, -ONE_HOUR, }; for (int i=0; i maxYear) { errln("Failed for "+DATES[i].getTime()+" ms: year=" + year + ", maxYear=" + maxYear); } } } /** * This is a bug in the validation code of GregorianCalendar. As reported, * the bug seems worse than it really is, due to a bug in the way the bug * report test was written. In reality the bug is restricted to the * DAY_OF_YEAR field. - liu 6/29/98 */ public void Test4147269() { GregorianCalendar calendar = new GregorianCalendar(); calendar.setLenient(false); java.util.Calendar tempcal = java.util.Calendar.getInstance(); tempcal.clear(); tempcal.set(1996, Calendar.JANUARY, 3); // Arbitrary date Date date = tempcal.getTime(); for (int field = 0; field < calendar.getFieldCount(); field++) { calendar.setTime(date); // Note: In the bug report, getActualMaximum() was called instead // of getMaximum() -- this was an error. The validation code doesn't // use getActualMaximum(), since that's too costly. int max = calendar.getMaximum(field); int value = max+1; calendar.set(field, value); try { calendar.getTime(); // Force time computation // We expect an exception to be thrown. If we fall through // to the next line, then we have a bug. errln("Test failed with field " + FIELD_NAME[field] + ", date before: " + date + ", date after: " + calendar.getTime() + ", value: " + value + " (max = " + max +")"); } catch (IllegalArgumentException e) { System.out.print(""); } } } /** * Reported bug is that a GregorianCalendar with a cutover of * Date(Long.MAX_VALUE) doesn't behave as a pure Julian calendar. CANNOT * REPRODUCE THIS BUG */ public void Test4149677() { TimeZone[] zones = { TimeZone.getTimeZone("GMT"), TimeZone.getTimeZone("PST"), TimeZone.getTimeZone("EAT") }; for (int i=0; i0) logln("---"); cal.clear(); cal.set(1998, Calendar.APRIL, 5, i, 0); d = cal.getTime(); String s0 = d.toString(); logln("0 " + i + ": " + s0); cal.clear(); cal.set(1998, Calendar.APRIL, 4, i+24, 0); d = cal.getTime(); String sPlus = d.toString(); logln("+ " + i + ": " + sPlus); cal.clear(); cal.set(1998, Calendar.APRIL, 6, i-24, 0); d = cal.getTime(); String sMinus = d.toString(); logln("- " + i + ": " + sMinus); if (!s0.equals(sPlus) || !s0.equals(sMinus)) { errln("Fail: All three lines must match"); } } } /** * Adding 12 months behaves differently from adding 1 year */ public void Test4165343() { GregorianCalendar calendar = new GregorianCalendar(1996, Calendar.FEBRUARY, 29); Date start = calendar.getTime(); logln("init date: " + start); calendar.add(Calendar.MONTH, 12); Date date1 = calendar.getTime(); logln("after adding 12 months: " + date1); calendar.setTime(start); calendar.add(Calendar.YEAR, 1); Date date2 = calendar.getTime(); logln("after adding one year : " + date2); if (date1.equals(date2)) { logln("Test passed"); } else { errln("Test failed"); } } /** * GregorianCalendar.getActualMaximum() does not account for first day of * week. */ public void Test4166109() { /* * Test month: * * March 1998 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 * 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 */ boolean passed = true; int field = Calendar.WEEK_OF_MONTH; GregorianCalendar calendar = new GregorianCalendar(Locale.US); calendar.set(1998, Calendar.MARCH, 1); calendar.setMinimalDaysInFirstWeek(1); logln("Date: " + calendar.getTime()); int firstInMonth = calendar.get(Calendar.DAY_OF_MONTH); for (int firstInWeek = Calendar.SUNDAY; firstInWeek <= Calendar.SATURDAY; firstInWeek++) { calendar.setFirstDayOfWeek(firstInWeek); int returned = calendar.getActualMaximum(field); int expected = (31 + ((firstInMonth - firstInWeek + 7)% 7) + 6) / 7; logln("First day of week = " + firstInWeek + " getActualMaximum(WEEK_OF_MONTH) = " + returned + " expected = " + expected + ((returned == expected) ? " ok" : " FAIL")); if (returned != expected) { passed = false; } } if (!passed) { errln("Test failed"); } } /** * Calendar.getActualMaximum(YEAR) works wrong. */ public void Test4167060() { int field = Calendar.YEAR; DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy G", Locale.US); GregorianCalendar calendars[] = { new GregorianCalendar(100, Calendar.NOVEMBER, 1), new GregorianCalendar(-99 /* 100BC */, Calendar.JANUARY, 1), new GregorianCalendar(1996, Calendar.FEBRUARY, 29), }; String[] id = { "Hybrid", "Gregorian", "Julian" }; for (int k=0; k<3; ++k) { logln("--- " + id[k] + " ---"); for (int j=0; j " + format.format(dateAfter)); if (valid && newYear != years[i]) { errln(" FAIL: " + newYear + " should be valid; date, month and time shouldn't change"); } else if (!valid && newYear == years[i]) { // We no longer require strict year maxima. That is, the // calendar // algorithm may work for values > the stated maximum. //errln(" FAIL: " + newYear + " should be invalid"); logln(" Note: " + newYear + " > maximum, but still valid"); } } } } } /** * Calendar.roll broken This bug relies on the TimeZone bug 4173604 to also * be fixed. */ public void Test4173516() { int fieldsList[][] = { { 1997, Calendar.FEBRUARY, 1, 10, 45, 15, 900 }, { 1999, Calendar.DECEMBER, 22, 23, 59, 59, 999 } }; int limit = 40; GregorianCalendar cal = new GregorianCalendar(); cal.setTime(new Date(0)); cal.roll(Calendar.HOUR, 0x7F000000); cal.roll(Calendar.HOUR, -0x7F000000); if (cal.getTime().getTime() != 0) { errln("Hour rolling broken"); } for (int op=0; op<2; ++op) { logln("Testing GregorianCalendar " + (op==0 ? "add" : "roll")); for (int field=0; field < cal.getFieldCount(); ++field) { if (field != Calendar.ZONE_OFFSET && field != Calendar.DST_OFFSET && field != Calendar.IS_LEAP_MONTH ) { for (int j=0; j " : ", -1) => ") + cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND) + "." + cal.get(Calendar.MILLISECOND) + " delta=" + delta + " ms"); } } } } } } } public void Test4174361() { GregorianCalendar calendar = new GregorianCalendar(1996, 1, 29); calendar.add(Calendar.MONTH, 10); //Date date1 = calendar.getTime(); //date1 = null; int d1 = calendar.get(Calendar.DAY_OF_MONTH); calendar = new GregorianCalendar(1996, 1, 29); calendar.add(Calendar.MONTH, 11); //Date date2 = calendar.getTime(); //date2 = null; int d2 = calendar.get(Calendar.DAY_OF_MONTH); if (d1 != d2) { errln("adding months to Feb 29 broken"); } } /** * Calendar does not update field values when setTimeZone is called. */ public void Test4177484() { TimeZone PST = TimeZone.getTimeZone("PST"); TimeZone EST = TimeZone.getTimeZone("EST"); Calendar cal = Calendar.getInstance(PST, Locale.US); cal.clear(); cal.set(1999, 3, 21, 15, 5, 0); // Arbitrary int h1 = cal.get(Calendar.HOUR_OF_DAY); cal.setTimeZone(EST); int h2 = cal.get(Calendar.HOUR_OF_DAY); if (h1 == h2) { errln("FAIL: Fields not updated after setTimeZone"); } // getTime() must NOT change when time zone is changed. // getTime() returns zone-independent time in ms. cal.clear(); cal.setTimeZone(PST); cal.set(Calendar.HOUR_OF_DAY, 10); Date pst10 = cal.getTime(); cal.setTimeZone(EST); Date est10 = cal.getTime(); if (!pst10.equals(est10)) { errln("FAIL: setTimeZone changed time"); } } /** * Week of year is wrong at the start and end of the year. */ public void Test4197699() { GregorianCalendar cal = new GregorianCalendar(); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); DateFormat fmt = new SimpleDateFormat("E dd MMM yyyy 'DOY='D 'WOY='w"); fmt.setCalendar(cal); int[] DATA = { 2000, Calendar.JANUARY, 1, 52, 2001, Calendar.DECEMBER, 31, 1, }; for (int i=0; i " + actual + ", want " + DATA[i+1]); } } } /** * WEEK_OF_YEAR computed incorrectly. A failure of this test can indicate a * problem in several different places in the */ public void Test4288792() throws Exception { TimeZone savedTZ = TimeZone.getDefault(); TimeZone.setDefault(TimeZone.getTimeZone("GMT")); GregorianCalendar cal = new GregorianCalendar(); for (int i = 1900; i < 2100; i++) { for (int j1 = 1; j1 <= 7; j1++) { // Loop for MinimalDaysInFirstWeek: 1..7 for (int j = Calendar.SUNDAY; j <= Calendar.SATURDAY; j++) { // Loop for FirstDayOfWeek: SUNDAY..SATURDAY cal.clear(); cal.setMinimalDaysInFirstWeek(j1); cal.setFirstDayOfWeek(j); // Set the calendar to the first day of the last week // of the year. This may overlap some of the start of // the next year; that is, the last week of 1999 may // include some of January 2000. Use the add() method // to advance through the week. For each day, call // get(WEEK_OF_YEAR). The result should be the same // for the whole week. Note that a bug in // getActualMaximum() will break this test. cal.set(Calendar.YEAR, i); int maxWeek = cal.getActualMaximum(Calendar.WEEK_OF_YEAR); cal.set(Calendar.WEEK_OF_YEAR, maxWeek); cal.set(Calendar.DAY_OF_WEEK, j); for (int k = 1; k < 7; k++) { cal.add(Calendar.DATE, 1); int WOY = cal.get(Calendar.WEEK_OF_YEAR); if (WOY != maxWeek) { errln(cal.getTime() + ",got=" + WOY + ",expected=" + maxWeek + ",min=" + j1 + ",first=" + j); } } // Now advance the calendar one more day. This should // put it at the first day of week 1 of the next year. cal.add(Calendar.DATE, 1); int WOY = cal.get(Calendar.WEEK_OF_YEAR); if (WOY != 1) { errln(cal.getTime() + ",got=" + WOY + ",expected=1,min=" + j1 + ",first" + j); } } } } TimeZone.setDefault(savedTZ); } /** * Test fieldDifference(). */ public void TestJ438() throws Exception { int DATA[] = { 2000, Calendar.JANUARY, 20, 2010, Calendar.JUNE, 15, 2010, Calendar.JUNE, 15, 2000, Calendar.JANUARY, 20, 1964, Calendar.SEPTEMBER, 7, 1999, Calendar.JUNE, 4, 1999, Calendar.JUNE, 4, 1964, Calendar.SEPTEMBER, 7, }; Calendar cal = Calendar.getInstance(Locale.US); for (int i=0; i " + got); } else { errln("FAIL: " + desc + " => " + got + ", expected " + exp); } } } finally { TimeZone.setDefault(zone); } } public void TestRegistration() { /* * Set names = Calendar.getCalendarFactoryNames(); * * TimeZone tz = TimeZone.getDefault(); Locale loc = * Locale.getDefault(); Iterator iter = names.iterator(); while * (iter.hasNext()) { String name = (String)iter.next(); logln("Testing * factory: " + name); * * Calendar cal = Calendar.getInstance(tz, loc, name); logln("Calendar * class: " + cal.getClass()); * * DateFormat fmt = cal.getDateTimeFormat(DateFormat.LONG, * DateFormat.LONG, loc); * * logln("Date: " + fmt.format(cal.getTime())); } * // register new default for our locale logln("\nTesting * registration"); loc = new Locale("en", "US"); Object key = * Calendar.register(JapaneseCalendar.factory(), loc, true); * * loc = new Locale("en", "US", "TEST"); Calendar cal = * Calendar.getInstance(loc); logln("Calendar class: " + * cal.getClass()); DateFormat fmt = * cal.getDateTimeFormat(DateFormat.LONG, DateFormat.LONG, loc); * logln("Date: " + fmt.format(cal.getTime())); * // force to use other default anyway logln("\nOverride * registration"); cal = Calendar.getInstance(tz, loc, "Gregorian"); fmt = * cal.getDateTimeFormat(DateFormat.LONG, DateFormat.LONG, loc); * logln("Date: " + fmt.format(cal.getTime())); * // unregister default logln("\nUnregistration"); logln("Unregister * returned: " + Calendar.unregister(key)); cal = * Calendar.getInstance(tz, loc, "Gregorian"); fmt = * cal.getDateTimeFormat(DateFormat.LONG, DateFormat.LONG, loc); * logln("Date: " + fmt.format(cal.getTime())); */ } /** * test serialize-and-modify. * @throws ClassNotFoundException */ public void TestSerialization3474() { try { ByteArrayOutputStream icuStream = new ByteArrayOutputStream(); logln("icu Calendar"); com.ibm.icu.util.GregorianCalendar icuCalendar = new com.ibm.icu.util.GregorianCalendar(); icuCalendar.setTimeInMillis(1187912555931L); long expectMillis = 1187912520931L; // with seconds (not ms) cleared. logln("instantiated: "+icuCalendar); logln("getMillis: "+icuCalendar.getTimeInMillis()); icuCalendar.set(com.ibm.icu.util.GregorianCalendar.SECOND, 0); logln("setSecond=0: "+icuCalendar); { long gotMillis = icuCalendar.getTimeInMillis(); if(gotMillis != expectMillis) { errln("expect millis "+expectMillis+" but got "+gotMillis); } else { logln("getMillis: "+gotMillis); } } ObjectOutputStream icuOut = new ObjectOutputStream(icuStream); icuOut.writeObject(icuCalendar); icuOut.flush(); icuOut.close(); ObjectInputStream icuIn = new ObjectInputStream(new ByteArrayInputStream(icuStream.toByteArray())); icuCalendar = null; icuCalendar = (com.ibm.icu.util.GregorianCalendar)icuIn.readObject(); logln("serialized back in: "+icuCalendar); { long gotMillis = icuCalendar.getTimeInMillis(); if(gotMillis != expectMillis) { errln("expect millis "+expectMillis+" but got "+gotMillis); } else { logln("getMillis: "+gotMillis); } } icuCalendar.set(com.ibm.icu.util.GregorianCalendar.SECOND, 0); logln("setSecond=0: "+icuCalendar); { long gotMillis = icuCalendar.getTimeInMillis(); if(gotMillis != expectMillis) { errln("expect millis "+expectMillis+" after stream and setSecond but got "+gotMillis); } else { logln("getMillis after stream and setSecond: "+gotMillis); } } } catch(IOException e) { errln(e.toString()); e.printStackTrace(); } catch(ClassNotFoundException cnf) { errln(cnf.toString()); cnf.printStackTrace(); } // JDK works correctly, etc etc. // ByteArrayOutputStream jdkStream = new ByteArrayOutputStream(); // logln("\nSUN Calendar"); // // java.util.GregorianCalendar sunCalendar = // new java.util.GregorianCalendar(); // // logln("instanzieren: "+sunCalendar); // logln("getMillis: "+sunCalendar.getTimeInMillis()); // sunCalendar.set(java.util.GregorianCalendar.SECOND, 0); // logln("setSecond=0: "+sunCalendar); // logln("getMillis: "+sunCalendar.getTimeInMillis()); // // ObjectOutputStream sunOut = // new ObjectOutputStream(jdkStream); // sunOut.writeObject(sunCalendar); // sunOut.flush(); // sunOut.close(); // // ObjectInputStream sunIn = // new ObjectInputStream(new ByteArrayInputStream(jdkStream.toByteArray())); // sunCalendar = null; // sunCalendar = (java.util.GregorianCalendar)sunIn.readObject(); // // logln("serialized: "+sunCalendar); // logln("getMillis: "+sunCalendar.getTimeInMillis()); // // sunCalendar.set(java.util.GregorianCalendar.SECOND, 0); // logln("setSecond=0: "+sunCalendar); // logln("getMillis: "+sunCalendar.getTimeInMillis()); } public void TestYearJump3279() { final long time = 1041148800000L; Calendar c = new GregorianCalendar(); DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US); c.setTimeInMillis(time); int year1 = c.get(Calendar.YEAR); logln("time: " + fmt.format(new Date(c.getTimeInMillis()))); logln("setting DOW to " + c.getFirstDayOfWeek()); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); logln("week: " + c.getTime()); logln("week adjust: " + fmt.format(new Date(c.getTimeInMillis()))); int year2 = c.get(Calendar.YEAR); if(year1 != year2) { errln("Error: adjusted day of week, and year jumped from " + year1 + " to " + year2); } else { logln("Year remained " + year2 + " - PASS."); } } public void TestGetKeywordValuesForLocale(){ final String[][] PREFERRED = { {"root", "gregorian"}, {"und", "gregorian"}, {"en_US", "gregorian"}, {"en_029", "gregorian"}, {"th_TH", "buddhist", "gregorian"}, {"und_TH", "buddhist", "gregorian"}, {"en_TH", "buddhist", "gregorian"}, {"he_IL", "gregorian", "hebrew", "islamic", "islamic-civil"}, {"ar_EG", "gregorian", "coptic", "islamic", "islamic-civil"}, {"ja", "gregorian", "japanese"}, {"ps_Guru_IN", "gregorian", "indian"}, {"th@calendar=gregorian", "buddhist", "gregorian"}, {"en@calendar=islamic", "gregorian"}, {"zh_TW", "gregorian", "roc", "chinese"}, {"ar_IR", "gregorian", "persian", "islamic", "islamic-civil"}, }; String[] ALL = Calendar.getKeywordValuesForLocale("calendar", ULocale.getDefault(), false); HashSet ALLSET = new HashSet(); for (int i = 0; i < ALL.length; i++) { ALLSET.add(ALL[i]); } for (int i = 0; i < PREFERRED.length; i++) { ULocale loc = new ULocale(PREFERRED[i][0]); String[] expected = new String[PREFERRED[i].length - 1]; System.arraycopy(PREFERRED[i], 1, expected, 0, expected.length); String[] pref = Calendar.getKeywordValuesForLocale("calendar", loc, true); boolean matchPref = false; if (pref.length == expected.length) { matchPref = true; for (int j = 0; j < pref.length; j++) { if (!pref[j].equals(expected[j])) { matchPref = false; } } } if (!matchPref) { errln("FAIL: Preferred values for locale " + loc + " got:" + Arrays.toString(pref) + " expected:" + Arrays.toString(expected)); } String[] all = Calendar.getKeywordValuesForLocale("calendar", loc, false); boolean matchAll = false; if (all.length == ALLSET.size()) { matchAll = true; for (int j = 0; j < all.length; j++) { if (!ALLSET.contains(all[j])) { matchAll = false; break; } } } if (!matchAll) { errln("FAIL: All values for locale " + loc + " got:" + Arrays.toString(all)); } } } } //eof