/** ******************************************************************************* * Copyright (C) 2000-2014, 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.text.ParsePosition; 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. // Set date to the mid year first before getActualMaximum(WEEK_OF_YEAR). // getActualMaximum(WEEK_OF_YEAR) is based on the current calendar's // year of week of year. After clear(), calendar is set to January 1st, // which may belongs to previous year of week of year. cal.set(i, Calendar.JULY, 1); 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 TestCalendarType6816() { Locale loc = new Locale("en", "TH"); Calendar cal = Calendar.getInstance(loc); String calType = cal.getType(); // Android patch: Force default Gregorian calendar. if ( !calType.equals("gregorian")) { errln("FAIL: Calendar type for en_TH should still be gregorian"); } // Android patch end. } public void TestGetKeywordValuesForLocale(){ // Android patch: Force default Gregorian calendar. final String[][] PREFERRED = { {"root", "gregorian"}, {"und", "gregorian"}, {"en_US", "gregorian"}, {"en_029", "gregorian"}, {"th_TH", "gregorian", "buddhist"}, {"und_TH", "gregorian", "buddhist"}, {"en_TH", "gregorian", "buddhist"}, {"he_IL", "gregorian", "hebrew", "islamic", "islamic-civil", "islamic-tbla"}, {"ar_EG", "gregorian", "coptic", "islamic", "islamic-civil", "islamic-tbla"}, {"ja", "gregorian", "japanese"}, {"ps_Guru_IN", "gregorian", "indian"}, {"th@calendar=gregorian", "gregorian", "buddhist"}, {"en@calendar=islamic", "gregorian"}, {"zh_TW", "gregorian", "roc", "chinese"}, {"ar_IR", "gregorian", "persian", "islamic", "islamic-civil", "islamic-tbla"}, }; // Android patch end. 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)); } } } public void TestTimeStamp() { long start = 0, time; // Create a new Gregorian Calendar. Calendar cal = Calendar.getInstance(Locale.US); for (int i = 0; i < 20000; i++) { cal.set(2009, Calendar.JULY, 3, 0, 49, 46); time = cal.getTime().getTime(); if (i == 0) { start = time; } else { if (start != time) { errln("start and time not equal"); return; } } } } /* * Test case for add/roll with non-lenient calendar reported by ticket#8057. * Calendar#add may result internal fields out of valid range. ICU used to * trigger field range validation also for internal field changes triggered * by add/roll, then throws IllegalArgumentException. The field value range * validation should be done only for fields set by user code explicitly * in non-lenient mode. */ public void TestT8057() { // Set the calendar to the last day in a leap year GregorianCalendar cal = new GregorianCalendar(); cal.setLenient(false); cal.clear(); cal.set(2008, Calendar.DECEMBER, 31); // Force calculating then fields once. long t = cal.getTimeInMillis(); long expected = 1262246400000L; // 2009-12-31 00:00 PST try { cal.add(Calendar.YEAR, 1); t = cal.getTimeInMillis(); if (t != expected) { errln("FAIL: wrong date after add: expected=" + expected + " returned=" + t); } } catch (IllegalArgumentException e) { errln("FAIL: add method should not throw IllegalArgumentException"); } } /* * Test case for ticket#8596. * Setting an year followed by getActualMaximum(Calendar.WEEK_OF_YEAR) * may result wrong maximum week. */ public void TestT8596() { GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("Etc/GMT")); gc.setFirstDayOfWeek(Calendar.MONDAY); gc.setMinimalDaysInFirstWeek(4); // Force the calender to resolve the fields once. // The maximum week number in 2011 is 52. gc.set(Calendar.YEAR, 2011); gc.get(Calendar.YEAR); // Set a date in year 2009, but not calling get to resolve // the calendar's internal field yet. gc.set(2009, Calendar.JULY, 1); // Then call getActuamMaximum for week of year. // #8596 was caused by conflict between year set // above and internal work calendar field resolution. int maxWeeks = gc.getActualMaximum(Calendar.WEEK_OF_YEAR); if (maxWeeks != 53) { errln("FAIL: Max week in 2009 in ISO calendar is 53, but got " + maxWeeks); } } /** * Test case for ticket:9019 */ public void Test9019() { GregorianCalendar cal1 = new GregorianCalendar(TimeZone.GMT_ZONE,ULocale.US); GregorianCalendar cal2 = new GregorianCalendar(TimeZone.GMT_ZONE,ULocale.US); cal1.clear(); cal2.clear(); cal1.set(2011,Calendar.MAY,06); cal2.set(2012,Calendar.JANUARY,06); cal1.setLenient(false); cal1.add(Calendar.MONTH, 8); if(!cal1.getTime().equals(cal2.getTime())) { errln("Error: Calendar is " + cal1.getTime() + " but expected " + cal2.getTime()); } else { logln("Pass: rolled calendar is " + cal1.getTime()); } } /** * Test case for ticket 9452 * Calendar addition fall onto the missing date - 2011-12-30 in Samoa */ public void TestT9452() { TimeZone samoaTZ = TimeZone.getTimeZone("Pacific/Apia"); GregorianCalendar cal = new GregorianCalendar(samoaTZ); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ"); sdf.setTimeZone(samoaTZ); // Set date to 2011-12-29 00:00 cal.clear(); cal.set(2011, Calendar.DECEMBER, 29, 0, 0, 0); Date d = cal.getTime(); String dstr = sdf.format(d); logln("Initial date: " + dstr); // Add 1 day cal.add(Calendar.DATE, 1); d = cal.getTime(); dstr = sdf.format(d); logln("+1 day: " + dstr); assertEquals("Add 1 day", "2011-12-31T00:00:00+14:00", dstr); // Subtract 1 day cal.add(Calendar.DATE, -1); d = cal.getTime(); dstr = sdf.format(d); logln("-1 day: " + dstr); assertEquals("Subtract 1 day", "2011-12-29T00:00:00-10:00", dstr); } /** * Test case for ticket 9403 * semantic API change when attempting to call setTimeInMillis(long) with a value outside the bounds. * In strict mode an IllegalIcuArgumentException will be thrown * In lenient mode the value will be pinned to the relative min/max */ public void TestT9403() { Calendar myCal = Calendar.getInstance(); long dateBit1, dateBit2, testMillis = 0L; boolean missedException = true; testMillis = -184303902611600000L; logln("Testing invalid setMillis value in lienent mode - using value: " + testMillis); try { myCal.setTimeInMillis(testMillis); } catch (IllegalArgumentException e) { logln("Fail: detected as bad millis"); missedException = false; } assertTrue("Fail: out of bound millis did not trigger exception!", missedException); dateBit1 = myCal.get(Calendar.MILLISECOND); assertNotEquals("Fail: millis not changed to MIN_MILLIS", testMillis, dateBit1); logln("Testing invalid setMillis value in strict mode - using value: " + testMillis); myCal.setLenient(false); try { myCal.setTimeInMillis(testMillis); } catch (IllegalArgumentException e) { logln("Pass: correctly detected bad millis"); missedException = false; } dateBit1 = myCal.get(Calendar.DAY_OF_MONTH); dateBit2 = myCal.getTimeInMillis(); assertFalse("Fail: error in setMillis, allowed invalid value : " + testMillis + "...returned dayOfMonth : " + dateBit1 + " millis : " + dateBit2, missedException); } /** * Test case for ticket 9968 * subparse fails to return an error indication when start pos is 0 */ public void TestT9968() { SimpleDateFormat sdf0 = new SimpleDateFormat("-MMMM"); ParsePosition pos0 = new ParsePosition(0); /* Date d0 = */ sdf0.parse("-September", pos0); logln("sdf0: " + pos0.getErrorIndex() + "/" + pos0.getIndex()); assertTrue("Fail: failed a good test", pos0.getErrorIndex() == -1); SimpleDateFormat sdf1 = new SimpleDateFormat("-MMMM"); ParsePosition pos1 = new ParsePosition(0); /* Date d1 = */ sdf1.parse("-????", pos1); logln("sdf1: " + pos1.getErrorIndex() + "/" + pos1.getIndex()); assertTrue("Fail: failed to detect bad parse", pos1.getErrorIndex() == 1); SimpleDateFormat sdf2 = new SimpleDateFormat("MMMM"); ParsePosition pos2 = new ParsePosition(0); /* Date d2 = */ sdf2.parse("????", pos2); logln("sdf2: " + pos2.getErrorIndex() + "/" + pos2.getIndex()); assertTrue("Fail: failed to detect bad parse", pos2.getErrorIndex() == 0); } public void TestWeekendData_10560() { final Calendar.WeekData worldWeekData = new Calendar.WeekData(2, 1, 7, 0, 1, 86400000); final Calendar.WeekData usWeekData = new Calendar.WeekData(1, 1, 7, 0, 1, 86400000); final Calendar.WeekData testWeekData = new Calendar.WeekData(1, 2, 3, 4, 5, 86400000); assertEquals("World", worldWeekData, Calendar.getWeekDataForRegion("001")); assertEquals("Illegal code => world", Calendar.getWeekDataForRegion("001"), Calendar.getWeekDataForRegion("xx")); assertEquals("FR = DE", Calendar.getWeekDataForRegion("FR"), Calendar.getWeekDataForRegion("DE")); assertNotEquals("IN ≠ world", Calendar.getWeekDataForRegion("001"), Calendar.getWeekDataForRegion("IN")); assertNotEquals("FR ≠ EG", Calendar.getWeekDataForRegion("FR"), Calendar.getWeekDataForRegion("EG")); Calendar aCalendar = Calendar.getInstance(Locale.US); assertEquals("US", usWeekData, aCalendar.getWeekData()); aCalendar.setWeekData(testWeekData); assertEquals("Custom", testWeekData, aCalendar.getWeekData()); } } //eof