/* * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @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 4328747 4413980 4546637 4623997 * 4685354 4655637 4683492 4080631 4080631 4167995 4340146 4639407 * 4652815 4652830 4740554 4936355 4738710 4633646 4846659 4822110 4960642 * 4973919 4980088 4965624 5013094 5006864 8152077 * @library /java/text/testlib */ import java.lang.reflect.*; import java.io.*; import java.util.*; import java.text.*; public class CalendarRegression extends IntlTest { public static void main(String[] args) throws Exception { new CalendarRegression().run(args); } /* 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() { // 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 date 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() { Calendar cal = Calendar.getInstance(); cal.setTime(new Date(98, Calendar.JUNE, 24)); cal.set(Calendar.MONTH, Calendar.NOVEMBER); // change a field logln(cal.getTime().toString()); if (!cal.getTime().equals(new Date(98, Calendar.NOVEMBER, 24))) 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(); String summerTime = "British Summer Time"; String standardTime = "Greenwich Mean Time"; try { Locale.setDefault(Locale.UK); TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); date = new SimpleDateFormat("zzzz"); Calendar cal=Calendar.getInstance(); cal.set(1997,Calendar.SEPTEMBER,30); Date now=cal.getTime(); String formattedDate = date.format(now); if (!formattedDate.equals(summerTime)) { errln("Wrong display name \"" + formattedDate + "\" for <" + now + ">"); } int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR); if (weekOfYear != 40) { errln("Wrong week-of-year " + weekOfYear + " for <" + now + ">"); } cal.set(1996,Calendar.DECEMBER,31); now=cal.getTime(); formattedDate = date.format(now); if (!formattedDate.equals(standardTime)) { errln("Wrong display name \"" + formattedDate + "\" for <" + now + ">"); } weekOfYear = cal.get(Calendar.WEEK_OF_YEAR); if (weekOfYear != 1) { errln("Wrong week-of-year " + weekOfYear + " for <" + now + ">"); } cal.set(1997,Calendar.JANUARY,1); now=cal.getTime(); formattedDate = date.format(now); if (!formattedDate.equals(standardTime)) { errln("Wrong display name \"" + formattedDate + "\" for <" + now + ">"); } weekOfYear = cal.get(Calendar.WEEK_OF_YEAR); if (weekOfYear != 1) { errln("Wrong week-of-year " + weekOfYear + " for <" + now + ">"); } cal.set(1997,Calendar.JANUARY,8); now=cal.getTime(); formattedDate = date.format(now); if (!formattedDate.equals(standardTime)) { errln("Wrong display name \"" + formattedDate + "\" for <" + now + ">"); } weekOfYear = cal.get(Calendar.WEEK_OF_YEAR); if (weekOfYear != 2) { errln("Wrong week-of-year " + weekOfYear + " for <" + now + ">"); } } finally { Locale.setDefault(saveLocale); TimeZone.setDefault(saveZone); } } 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++ ); 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(cal1.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(cal1.HOUR_OF_DAY))); cal1.setTime(new Date(880698639000L)); logln("GMT 2 is: " + (h2=cal1.get(cal1.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(cal1.YEAR), cal1.get(cal1.MONTH), cal1.get(cal1.DAY_OF_MONTH), cal1.get(cal1.HOUR_OF_DAY), cal1.get(cal1.MINUTE), cal1.get(cal1.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(cal1.YEAR), cal1.get(cal1.MONTH), cal1.get(cal1.DAY_OF_MONTH), cal1.get(cal1.HOUR_OF_DAY), cal1.get(cal1.MINUTE), cal1.get(cal1.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() { if (Locale.getDefault().equals(new Locale("th", "TH"))) { return; } 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("\tFAIL\n\t\texp: " + after); 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 + "\n\t\t => " + testCal.getTime()); if (!before.equals(testCal.getTime())) { logln("\tFAIL\n\t\texp: " + before); fail = true; } else logln("\tOK"); } if (fail) { errln("Fail: Week of year misbehaving"); } } public void Test4106136() { Locale saveLocale = Locale.getDefault(); try { Locale[] locales = { Locale.CHINESE, Locale.CHINA }; 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() { final String[] fieldName = { "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" }; GregorianCalendar calendar = new GregorianCalendar(); calendar.setLenient(false); Date date = new Date(1996-1900, Calendar.JANUARY, 3); // Arbitrary date for (int field = 0; field < Calendar.FIELD_COUNT; 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 " + fieldName[field] + ", date before: " + date + ", date after: " + calendar.getTime() + ", value: " + value + " (max = " + max +")"); } catch (IllegalArgumentException e) {} } } /** * 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"); } } } finally { TimeZone.setDefault(savedTz); } } /** * 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. * * Note: Before 1.5, this test case assumed that * setGregorianChange didn't change object's date. But it was * changed. See 4928615. */ public void Test4167060() { int field = Calendar.YEAR; DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy G", Locale.US); int[][] dates = { // year, month, day of month { 100, Calendar.NOVEMBER, 1 }, { -99 /*100BC*/, Calendar.JANUARY, 1 }, { 1996, Calendar.FEBRUARY, 29 }}; String[] id = { "Hybrid", "Gregorian", "Julian" }; for (int k=0; k<3; ++k) { logln("--- " + id[k] + " ---"); for (int j = 0; j < dates.length; ++j) { GregorianCalendar calendar = new GregorianCalendar(); if (k == 1) { calendar.setGregorianChange(new Date(Long.MIN_VALUE)); } else if (k == 2) { calendar.setGregorianChange(new Date(Long.MAX_VALUE)); } calendar.set(dates[j][0], dates[j][1], dates[j][2]); format.setCalendar((Calendar)calendar.clone()); Date dateBefore = calendar.getTime(); int maxYear = calendar.getActualMaximum(field); logln("maxYear: " + maxYear + " for " + format.format(calendar.getTime())); logln("date before: " + format.format(dateBefore)); int years[] = {2000, maxYear-1, maxYear, maxYear+1}; for (int i = 0; i < years.length; i++) { boolean valid = years[i] <= maxYear; calendar.set(field, years[i]); Date dateAfter = calendar.getTime(); int newYear = calendar.get(field); calendar.setTime(dateBefore); // restore calendar for next use logln(" Year " + years[i] + (valid? " ok " : " bad") + " => " + 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]) { errln(" FAIL: " + newYear + " should be invalid"); } } } } } /** * Calendar.roll broken * This bug relies on the TimeZone bug 4173604 to also be fixed. */ public void Test4173516() { if (Locale.getDefault().equals(new Locale("th", "TH"))) { return; } int fieldsList[][] = { { 1997, Calendar.FEBRUARY, 1, 10, 45, 15, 900 }, { 1999, Calendar.DECEMBER, 22, 23, 59, 59, 999 }, // test case for 4960642 with default cutover { 1582, Calendar.OCTOBER, 4, 23, 59, 59, 999 }, }; String[] fieldNames = { "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" }; Locale savedLocale = Locale.getDefault(); Locale.setDefault(Locale.US); int limit = 40; try { 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. expected 0, got "+cal.getTime().getTime()); } for (int op=0; op<2; ++op) { logln("Testing GregorianCalendar " + (op==0 ? "add" : "roll")); for (int field=0; field < Calendar.FIELD_COUNT; ++field) { if (field != Calendar.ZONE_OFFSET && field != Calendar.DST_OFFSET) { for (int j=0; j " + 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) + " d=" + delta); } } } } } } } finally { Locale.setDefault(savedLocale); } } public void Test4174361() { GregorianCalendar calendar = new GregorianCalendar(1996, 1, 29); calendar.add(Calendar.MONTH, 10); Date date1 = calendar.getTime(); int d1 = calendar.get(Calendar.DAY_OF_MONTH); calendar = new GregorianCalendar(1996, 1, 29); calendar.add(Calendar.MONTH, 11); Date date2 = calendar.getTime(); 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]); } } } public void Test4288792() throws Exception { TimeZone savedTZ = TimeZone.getDefault(); TimeZone.setDefault(TimeZone.getTimeZone("GMT")); GregorianCalendar cal = new GregorianCalendar(); try { 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); 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); } } 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); } } } } } finally { TimeZone.setDefault(savedTZ); } } public void Test4328747() throws Exception { Calendar c = (Calendar)Calendar.getInstance(Locale.US); c.clear(); c.set(1966,0,1); // 1 jan 1966 // serialize ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream s = new ObjectOutputStream(out); s.writeObject(c); s.flush(); // deserialize ObjectInputStream t = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())); Calendar result = (Calendar)t.readObject(); // let recalculate fields with the same UTC time result.setTime(result.getTime()); // Bug gives 1965 11 19 if ((result.get(c.YEAR) != 1966) || (result.get(c.MONTH) != 0) || (result.get(c.DATE) != 1)) { errln("deserialized Calendar returned wrong date field(s): " + result.get(c.YEAR) + "/" + result.get(c.MONTH) + "/" + result.get(c.DATE) + ", expected 1966/0/1"); } } /** * Test whether Calendar can be serialized/deserialized correctly * even if invalid/customized TimeZone is used. */ public void Test4413980() { TimeZone savedTimeZone = TimeZone.getDefault(); try { boolean pass = true; String[] IDs = new String[] {"Undefined", "PST", "US/Pacific", "GMT+3:00", "GMT-01:30"}; for (int i = 0; i < IDs.length; i++) { TimeZone tz = TimeZone.getTimeZone(IDs[i]); TimeZone.setDefault(tz); Calendar c = (Calendar)Calendar.getInstance(); // serialize ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream s = new ObjectOutputStream(out); s.writeObject(c); s.flush(); // deserialize ObjectInputStream t = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())); if (!c.equals(t.readObject())) { pass = false; logln("Calendar instance which uses TimeZone <" + IDs[i] + "> is incorrectly serialized/deserialized."); } else { logln("Calendar instance which uses TimeZone <" + IDs[i] + "> is correctly serialized/deserialized."); } } if (!pass) { errln("Fail: Calendar serialization/equality bug"); } } catch (IOException e) { errln("Fail: " + e); e.printStackTrace(); } catch (ClassNotFoundException e) { errln("Fail: " + e); e.printStackTrace(); } finally { TimeZone.setDefault(savedTimeZone); } } /** * 4546637: Incorrect WEEK_OF_MONTH after changing First Day Of Week */ public void Test4546637() { GregorianCalendar day = new GregorianCalendar (2001, Calendar.NOVEMBER, 04); day.setMinimalDaysInFirstWeek(1); int wom = day.get(Calendar.WEEK_OF_MONTH); day.setFirstDayOfWeek(Calendar.MONDAY); if (day.get(Calendar.WEEK_OF_MONTH) != 1) { errln("Fail: 2001/11/4 must be the first week of the month."); } } /** * 4623997: GregorianCalendar returns bad WEEK_OF_YEAR */ public void Test4623997() { GregorianCalendar cal = new GregorianCalendar(2000, GregorianCalendar.JANUARY, 1); int dow = cal.get(GregorianCalendar.DAY_OF_WEEK); cal.setFirstDayOfWeek(GregorianCalendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); if (cal.get(GregorianCalendar.WEEK_OF_YEAR) != 52) { errln("Fail: 2000/1/1 must be the 52nd week of the year."); } } /** * 4685354: Handling of Calendar fields setting state is broken * *

Need to use SimpleDateFormat to test because a call to * get(int) changes internal states of a Calendar. */ public void Test4685354() { if (Locale.getDefault().equals(new Locale("hi", "IN"))) { return; } Calendar calendar = Calendar.getInstance(Locale.US); DateFormat df = new SimpleDateFormat("yyyy/MM/dd", Locale.US); String expected = "1999/12/31"; Date t; String s; try { calendar.setTime(df.parse(expected)); } catch (Exception e) { throw new RuntimeException("Unexpected parse exception", e); } t = calendar.getTime(); calendar.set(Calendar.DAY_OF_MONTH, 33); t = calendar.getTime(); calendar.set(Calendar.DAY_OF_MONTH, 0); s = df.format(calendar.getTime()); if (!expected.equals(s)) { errln("DAY_OF_MONTH w/o ZONE_OFFSET: expected: " + expected + ", got: " + s); } // The same thing must work with ZONE_OFFSET set try { calendar.setTime(df.parse(expected)); } catch (Exception e) { throw new RuntimeException("Unexpected parse exception", e); } t = calendar.getTime(); calendar.set(calendar.ZONE_OFFSET, calendar.get(calendar.ZONE_OFFSET)); calendar.set(Calendar.DAY_OF_MONTH, 33); t = calendar.getTime(); calendar.set(Calendar.DAY_OF_MONTH, 0); s = df.format(calendar.getTime()); if (!expected.equals(s)) { errln("DAY_OF_MONTH: expected: " + expected + ", got: " + s); } expected = "1999/12/24"; // 0th week of 2000 calendar.clear(); Date initialDate = null; try { initialDate = df.parse(expected); calendar.setTime(initialDate); } catch (Exception e) { throw new RuntimeException("Unexpected parse exception", e); } t = calendar.getTime(); calendar.set(calendar.ZONE_OFFSET, calendar.get(calendar.ZONE_OFFSET)); // jump to the next year calendar.set(Calendar.WEEK_OF_YEAR, 100); t = calendar.getTime(); calendar.set(Calendar.WEEK_OF_YEAR, 0); s = df.format(calendar.getTime()); if (!expected.equals(s)) { errln("WEEK_OF_YEAR: expected: " + expected + ", got: " + s); } // change the state back calendar.clear(); calendar.setTime(initialDate); calendar.set(calendar.ZONE_OFFSET, calendar.get(calendar.ZONE_OFFSET)); // jump to next month calendar.set(Calendar.WEEK_OF_MONTH, 7); t = calendar.getTime(); calendar.set(Calendar.WEEK_OF_MONTH, 0); s = df.format(calendar.getTime()); if (!expected.equals(s)) { errln("WEEK_OF_MONTH: expected: " + expected + ", got: " + s); } // Make sure the time fields work correctly. calendar.clear(); df = new SimpleDateFormat("HH:mm:ss"); TimeZone tz = TimeZone.getTimeZone("GMT"); df.setTimeZone(tz); calendar.setTimeZone(tz); expected = "22:59:59"; try { calendar.setTime(df.parse(expected)); } catch (Exception e) { throw new RuntimeException("Unexpected parse exception", e); } t = calendar.getTime(); // time should be 22:59:59. calendar.set(Calendar.MINUTE, 61); // time should be 23:01:59. t = calendar.getTime(); calendar.set(Calendar.MINUTE, -1); // time should be back to 22:59:59. s = df.format(calendar.getTime()); if (!expected.equals(s)) { errln("MINUTE: expected: " + expected + ", got: " + s); } } /** * 4655637: Calendar.set() for DAY_OF_WEEK does not return the right value * *

Need to use SimpleDateFormat to test because a call to * get(int) changes internal states of a Calendar. */ public void Test4655637() { // Skip this test case if it's Thai locale if (Locale.getDefault().equals(new Locale("th", "TH"))) { return; } Calendar cal = Calendar.getInstance(); cal.setTime(new Date(1029814211523L)); cal.set(Calendar.YEAR, 2001); Date t = cal.getTime(); cal.set(Calendar.MONTH, Calendar.JANUARY); t = cal.getTime(); cal.set(Calendar.DAY_OF_MONTH, 8); t = cal.getTime(); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); DateFormat df = new SimpleDateFormat("yyyy/MM/dd", Locale.US); String expected = "2001/01/08"; String s = df.format(cal.getTime()); if (!expected.equals(s)) { errln("expected: " + expected + ", got: " + s); } } /** * 4683492: Invalid value for MONTH in GregorianCalendar causes exception in getTime(). * *

Need to use SimpleDateFormat to test because a call to * get(int) changes internal states of a Calendar. * *

This test case throws ArrayIndexOutOfBoundsException without the fix. */ public void Test4683492() { Calendar cal = new GregorianCalendar(2002, 3, 29, 10, 0, 0); cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1); cal.set(Calendar.MONTH, 12); DateFormat df = new SimpleDateFormat("yyyy/MM/dd", Locale.US); String expected = "2003/01/31"; String s = df.format(cal.getTime()); if (!expected.equals(s)) { errln("expected: " + expected + ", got: " + s); } } /** * 4080631: Calendar.hashCode is amazingly bad */ public void Test4080631() { Calendar cal = Calendar.getInstance(); int h1 = cal.hashCode(); cal.add(cal.SECOND, +1); int h2 = cal.hashCode(); Calendar cal2 = (Calendar) cal.clone(); cal.add(cal.MILLISECOND, +1); int h3 = cal.hashCode(); logln("hash code: h1="+h1+", h2="+h2+", h3="+h3); if (h1 == h2 || h1 == h3 || h2 == h3) { errln("hash code is poor: hashCode="+h1); } h2 = cal2.hashCode(); cal.add(cal.MILLISECOND, -1); int h4 = cal.hashCode(); logln("hash code: h2="+h2+", h4="+h4); if (cal.equals(cal2) && h2 != h4) { errln("broken hash code: h2="+h2+", h4="+h4); } int x = cal.getFirstDayOfWeek() + 3; if (x > cal.SATURDAY) { x -= 7; } cal.setFirstDayOfWeek(x); int h5 = cal.hashCode(); logln("hash code: h4="+h4+", h5="+h5); if (h4 == h5) { errln("has code is poor with first day of week param: hashCode="+h4); } } /** * 4125161: RFE: GregorianCalendar needs more era names (BCE and CE) */ /* public void Test4125161() throws Exception { Class gc = GregorianCalendar.class; Field f; int mod; f = gc.getDeclaredField("BCE"); mod = f.getModifiers(); if (!Modifier.isStatic(mod) || !Modifier.isFinal(mod)) { errln("BCE: wrong modifiers: " + mod); } f = gc.getDeclaredField("CE"); mod = f.getModifiers(); if (!Modifier.isStatic(mod) || !Modifier.isFinal(mod)) { errln("CE: wrong modifiers: " + mod); } if (GregorianCalendar.BCE != GregorianCalendar.BC || GregorianCalendar.CE != GregorianCalendar.AD) { errln("Wrong BCE and/or CE values"); } } */ /** * 4167995: GregorianCalendar.setGregorianChange() not to spec */ public void Test4167995() { Koyomi gc = new Koyomi(TimeZone.getTimeZone("GMT")); logln("Hybrid: min date"); gc.setTime(new Date(Long.MIN_VALUE)); if (!gc.checkDate(292269055, gc.DECEMBER, 2, gc.SUNDAY) || !gc.checkFieldValue(gc.ERA, gc.BC)) { errln(gc.getMessage()); } logln("Hybrid: max date"); gc.setTime(new Date(Long.MAX_VALUE)); if (!gc.checkDate(292278994, gc.AUGUST, 17, gc.SUNDAY) || !gc.checkFieldValue(gc.ERA, gc.AD)) { errln(gc.getMessage()); } gc.setGregorianChange(new Date(Long.MIN_VALUE)); logln("Gregorian: min date"); gc.setTime(new Date(Long.MIN_VALUE)); if (!gc.checkDate(292275056, gc.MAY, 16, gc.SUNDAY) || !gc.checkFieldValue(gc.ERA, gc.BC)) { errln(gc.getMessage()); } logln("Gregorian: max date"); gc.setTime(new Date(Long.MAX_VALUE)); if (!gc.checkDate(292278994, gc.AUGUST, 17, gc.SUNDAY) || !gc.checkFieldValue(gc.ERA, gc.AD)) { errln(gc.getMessage()); } gc.setGregorianChange(new Date(Long.MAX_VALUE)); logln("Julian: min date"); gc.setTime(new Date(Long.MIN_VALUE)); if (!gc.checkDate(292269055, gc.DECEMBER, 2, gc.SUNDAY) || !gc.checkFieldValue(gc.ERA, gc.BC)) { errln(gc.getMessage()); } logln("Julian: max date"); gc.setTime(new Date(Long.MAX_VALUE)); if (!gc.checkDate(292272993, gc.JANUARY, 4, gc.SUNDAY) || !gc.checkFieldValue(gc.ERA, gc.AD)) { errln(gc.getMessage()); } } /** * 4340146: Calendar.equals modifies state */ public void Test4340146() { Koyomi cal = new Koyomi(); cal.clear(); cal.set(2003, cal.OCTOBER, 32); cal.equals(new Koyomi()); if (!cal.checkInternalDate(2003, cal.OCTOBER, 32)) { errln(cal.getMessage()); } new Koyomi().equals(cal); if (!cal.checkInternalDate(2003, cal.OCTOBER, 32)) { errln(cal.getMessage()); } } /** * 4639407: GregorianCalendar doesn't work in non-lenient due to timezone bounds checking */ public void Test4639407() { // The following operations in non-lenient mode shouldn't // throw IllegalArgumentException. Koyomi cal = new Koyomi(TimeZone.getTimeZone("Pacific/Kiritimati")); cal.setLenient(false); cal.set(2003, cal.OCTOBER, 10); cal.getTime(); cal.setTimeZone(TimeZone.getTimeZone("Pacific/Tongatapu")); cal.set(2003, cal.OCTOBER, 10); cal.getTime(); } /** * 4652815: rolling week-of-year back hundreds of weeks changes year */ public void Test4652815() { Koyomi cal = new Koyomi(Locale.US); testRoll(cal, 2003, cal.SEPTEMBER, 29); testRoll(cal, 2003, cal.DECEMBER, 24); testRoll(cal, 1582, cal.DECEMBER, 19); testRoll(cal, 1582, cal.DECEMBER, 20); } private void testRoll(Koyomi cal, int year, int month, int dayOfMonth) { cal.clear(); cal.set(year, month, dayOfMonth); cal.getTime(); // normalize fields logln("Roll backwards from " + cal.toDateString()); for (int i = 0; i < 1000; i++) { cal.roll(cal.WEEK_OF_YEAR, -i); if (!cal.checkFieldValue(cal.YEAR, year)) { errln(cal.getMessage()); } } logln("Roll forewards from " + cal.toDateString()); for (int i = 0; i < 1000; i++) { cal.roll(cal.WEEK_OF_YEAR, +i); if (!cal.checkFieldValue(cal.YEAR, year)) { errln(cal.getMessage()); } } } /** * 4652830: GregorianCalendar roll behaves unexpectedly for dates in BC era */ public void Test4652830() { Koyomi cal = new Koyomi(Locale.US); cal.clear(); logln("BCE 9-2-28 (leap year) roll DAY_OF_MONTH++ twice"); cal.set(cal.ERA, cal.BC); cal.set(9, cal.FEBRUARY, 28); if (cal.getActualMaximum(cal.DAY_OF_YEAR) != 366) { errln(" wrong actual max of DAY_OF_YEAR: got " + cal.getActualMaximum(cal.DAY_OF_YEAR) + " expected " + 366); } cal.roll(cal.DAY_OF_MONTH, +1); if (!cal.checkFieldValue(cal.ERA, cal.BC) || !cal.checkDate(9, cal.FEBRUARY, 29)) { errln(cal.getMessage()); } cal.roll(cal.DAY_OF_MONTH, +1); if (!cal.checkFieldValue(cal.ERA, cal.BC) || !cal.checkDate(9, cal.FEBRUARY, 1)) { errln(cal.getMessage()); } } /** * 4740554: GregorianCalendar.getActualMaximum is inconsistent with normalization */ public void Test4740554() { logln("1999/(Feb+12)/1 should be normalized to 2000/Feb/1 for getActualMaximum"); Koyomi cal = new Koyomi(Locale.US); cal.clear(); cal.set(1999, cal.FEBRUARY + 12, 1); if (!cal.checkActualMaximum(cal.DAY_OF_YEAR, 366)) { errln(cal.getMessage()); } if (!cal.checkActualMaximum(cal.DAY_OF_MONTH, 29)) { errln(cal.getMessage()); } } /** * 4936355: GregorianCalendar causes overflow/underflow with time of day calculation */ public void Test4936355() { Koyomi cal = new Koyomi(TimeZone.getTimeZone("GMT")); cal.clear(); cal.set(1970, cal.JANUARY, 1); checkTimeCalculation(cal, cal.HOUR_OF_DAY, Integer.MAX_VALUE, (long)Integer.MAX_VALUE * 60 * 60 * 1000); cal.clear(); cal.set(1970, cal.JANUARY, 1); checkTimeCalculation(cal, cal.HOUR, Integer.MAX_VALUE, (long)Integer.MAX_VALUE * 60 * 60 * 1000); cal.clear(); cal.set(1970, cal.JANUARY, 1); checkTimeCalculation(cal, cal.MINUTE, Integer.MAX_VALUE, (long)Integer.MAX_VALUE * 60 * 1000); cal.clear(); // Make sure to use Gregorian dates (before and after the // set() call) for testing cal.set(250000, cal.JANUARY, 1); checkTimeCalculation(cal, cal.HOUR_OF_DAY, Integer.MIN_VALUE, (long)Integer.MIN_VALUE * 60 * 60 * 1000); cal.clear(); cal.set(250000, cal.JANUARY, 1); checkTimeCalculation(cal, cal.HOUR, Integer.MIN_VALUE, (long)Integer.MIN_VALUE * 60 * 60 * 1000); cal.clear(); cal.set(250000, cal.JANUARY, 1); checkTimeCalculation(cal, cal.MINUTE, Integer.MIN_VALUE, (long)Integer.MIN_VALUE * 60 * 1000); } private void checkTimeCalculation(Koyomi cal, int field, int value, long expectedDelta) { long time = cal.getTimeInMillis(); cal.set(field, value); long time2 = cal.getTimeInMillis(); if ((time + expectedDelta) != time2) { String s = value == Integer.MAX_VALUE ? "Integer.MAX_VALUE" : "Integer.MIN_VALUE"; errln("set(" + Koyomi.getFieldName(field) + ", " + s + ") failed." + " got " + time2 + ", expected " + (time+expectedDelta)); } } /** * 4722650: Calendar.equals can throw an exception in non-lenient * (piggy-back tests for compareTo() which is new in 1.5) */ public void Test4722650() { Calendar cal1 = new GregorianCalendar(); cal1.clear(); Calendar cal2 = new GregorianCalendar(); cal2.clear(); cal2.setLenient(false); cal1.set(2003, Calendar.OCTOBER, 31); cal2.set(2003, Calendar.OCTOBER, 31); try { if (cal1.equals(cal2)) { errln("lenient and non-lenient shouldn't be equal. (2003/10/31)"); } if (cal1.compareTo(cal2) != 0) { errln("cal1 and cal2 should represent the same time. (2003/10/31)"); } } catch (IllegalArgumentException e) { errln("equals threw IllegalArugumentException with non-lenient"); } cal1.set(2003, Calendar.OCTOBER, 32); cal2.set(2003, Calendar.OCTOBER, 32); try { if (cal1.equals(cal2)) { errln("lenient and non-lenient shouldn't be equal. (2003/10/32)"); } if (cal1.compareTo(cal2) != 0) { errln("cal1 and cal2 should represent the same time. (2003/10/32)"); } } catch (IllegalArgumentException e) { errln("equals threw IllegalArugumentException with non-lenient"); } cal1 = Calendar.getInstance(new Locale("th", "TH")); cal1.setTimeInMillis(0L); cal2 = Calendar.getInstance(Locale.US); cal2.setTimeInMillis(0L); if (cal1.equals(cal2)) { errln("Buddhist.equals(Gregorian) shouldn't be true. (millis=0)"); } if (cal1.compareTo(cal2) != 0) { errln("cal1 (Buddhist) and cal2 (Gregorian) should represent the same time. (millis=0)"); } } /** * 4738710: API: Calendar comparison methods should be improved */ public void Test4738710() { Calendar cal0 = new GregorianCalendar(2003, Calendar.SEPTEMBER, 30); Comparable cal1 = new GregorianCalendar(2003, Calendar.OCTOBER, 1); Calendar cal2 = new GregorianCalendar(2003, Calendar.OCTOBER, 2); if (!(cal1.compareTo(cal0) > 0)) { errln("!(cal1 > cal0)"); } if (!(cal1.compareTo(cal2) < 0)) { errln("!(cal1 < cal2)"); } if (cal1.compareTo(new GregorianCalendar(2003, Calendar.OCTOBER, 1)) != 0) { errln("cal1 != new GregorianCalendar(2003, Calendar.OCTOBER, 1)"); } if (cal0.after(cal2)) { errln("cal0 shouldn't be after cal2"); } if (cal2.before(cal0)) { errln("cal2 shouldn't be before cal0"); } if (cal0.after(new Integer(0))) { errln("cal0.after() returned true with an Integer."); } if (cal0.before(new Integer(0))) { errln("cal0.before() returned true with an Integer."); } if (cal0.after(null)) { errln("cal0.after() returned true with null."); } if (cal0.before(null)) { errln("cal0.before() returned true with null."); } } /** * 4633646: Setting WEEK_OF_MONTH to 1 results in incorrect date */ public void Test4633646() { Koyomi cal = new Koyomi(Locale.US); cal.setTime(new Date(2002-1900, 1-1, 28)); sub4633646(cal); cal.setLenient(false); cal.setTime(new Date(2002-1900, 1-1, 28)); sub4633646(cal); cal = new Koyomi(Locale.US); cal.clear(); cal.set(2002, cal.JANUARY, 28); sub4633646(cal); cal.clear(); cal.setLenient(false); cal.set(2002, cal.JANUARY, 28); sub4633646(cal); } void sub4633646(Koyomi cal) { cal.getTime(); cal.set(cal.WEEK_OF_MONTH, 1); if (cal.isLenient()) { if (!cal.checkDate(2001, cal.DECEMBER, 31)) { errln(cal.getMessage()); } if (!cal.checkFieldValue(cal.WEEK_OF_MONTH, 6)) { errln(cal.getMessage()); } } else { try { Date d = cal.getTime(); errln("didn't throw IllegalArgumentException in non-lenient"); } catch (IllegalArgumentException e) { } } } /** * 4846659: Calendar: Both set() and roll() don't work for AM_PM time field * (Partially fixed only roll as of 1.5) */ public void Test4846659() { Koyomi cal = new Koyomi(); cal.clear(); cal.set(2003, cal.OCTOBER, 31, 10, 30, 30); cal.getTime(); // Test roll() cal.roll(cal.AM_PM, +1); // should turn to PM if (!cal.checkFieldValue(cal.HOUR_OF_DAY, 10+12)) { errln("roll: AM_PM didn't change to PM"); } cal.clear(); cal.set(2003, cal.OCTOBER, 31, 10, 30, 30); cal.getTime(); // Test set() cal.set(cal.AM_PM, cal.PM); // should turn to PM if (!cal.checkFieldValue(cal.HOUR_OF_DAY, 10+12)) { errln("set: AM_PM didn't change to PM"); } cal.clear(); cal.set(2003, cal.OCTOBER, 31, 10, 30, 30); cal.getTime(); cal.set(cal.AM_PM, cal.PM); cal.set(cal.HOUR, 9); if (!cal.checkFieldValue(cal.HOUR_OF_DAY, 9+12)) { errln("set: both AM_PM and HOUT didn't change to PM"); } } /** * 4822110: GregorianCalendar.get() returns an incorrect date after setFirstDayOfWeek() */ public void Test4822110() { Koyomi cal = new Koyomi(Locale.US); // June 2003 // S M Tu W Th F S // 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 cal.clear(); // 6/1 to 6/7 should be the 1st week of June. cal.set(2003, cal.JUNE, 2); cal.getTime(); // Let cal calculate time. cal.setFirstDayOfWeek(cal.MONDAY); // Now 6/2 to 6/8 should be the 2nd week of June. Sunday of // that week is 6/8. logln("1: " +cal.get(cal.WEEK_OF_MONTH)+", "+cal.get(cal.DAY_OF_MONTH)); cal.set(cal.DAY_OF_WEEK, cal.SUNDAY); logln("1st Sunday of June 2003 with FirstDayOfWeek=MONDAY"); if (!cal.checkDate(2003, cal.JUNE, 8)) { errln(cal.getMessage()); } } /** * 4973919: Inconsistent GregorianCalendar hashCode before and after serialization */ public void Test4966499() throws Exception { GregorianCalendar date1 = new GregorianCalendar(2004, Calendar.JANUARY, 7); // Serialize date1 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(date1); byte[] buffer = baos.toByteArray(); // Deserialize it ByteArrayInputStream bais = new ByteArrayInputStream(buffer); ObjectInputStream ois = new ObjectInputStream(bais); GregorianCalendar date2 = (GregorianCalendar)ois.readObject(); if (!date1.equals(date2)) { errln("date1.equals(date2) != true"); } if (date1.hashCode() != date2.hashCode()) { errln("inconsistent hashCode() value (before=0x" +Integer.toHexString(date1.hashCode())+ ", after=0x"+Integer.toHexString(date2.hashCode())+")"); } } /** * 4980088: GregorianCalendar.getActualMaximum doesn't throw exception */ public void Test4980088() { GregorianCalendar cal = new GregorianCalendar(); try { int x = cal.getMaximum(100); errln("getMaximum(100) didn't throw an exception."); } catch (IndexOutOfBoundsException e) { logln("getMaximum: " + e.getClass().getName() + ": " + e.getMessage()); } try { int x = cal.getLeastMaximum(100); errln("getLeastMaximum(100) didn't throw an exception."); } catch (IndexOutOfBoundsException e) { logln("getLeastMaximum: " + e.getClass().getName() + ": " + e.getMessage()); } try { int x = cal.getActualMaximum(100); errln("getActualMaximum(100) didn't throw an exception."); } catch (IndexOutOfBoundsException e) { logln("getActualMaximum: " + e.getClass().getName() + ": " + e.getMessage()); } try { int x = cal.getMinimum(100); errln("getMinimum(100) didn't throw an exception."); } catch (IndexOutOfBoundsException e) { logln("getMinimum: " + e.getClass().getName() + ": " + e.getMessage()); } try { int x = cal.getGreatestMinimum(100); errln("getGreatestMinimum(100) didn't throw an exception."); } catch (IndexOutOfBoundsException e) { logln("getGreatestMinimum: " + e.getClass().getName() + ": " + e.getMessage()); } try { int x = cal.getActualMinimum(100); errln("getActualMinimum(100) didn't throw an exception."); } catch (IndexOutOfBoundsException e) { logln("getActualMinimum: " + e.getClass().getName() + ": " + e.getMessage()); } } /** * 4965624: GregorianCalendar.isLeapYear(1000) returns incorrect value */ public void Test4965624() { // 5013094: This test case needs to use "GMT" to specify // Gregorian cutover dates. TimeZone savedZone = TimeZone.getDefault(); TimeZone.setDefault(TimeZone.getTimeZone("GMT")); try { Map data = new HashMap(); data.put(getGregorianDate(999, Calendar.OCTOBER, 1), Boolean.FALSE); data.put(getGregorianDate(1000, Calendar.JANUARY, 1), Boolean.FALSE); data.put(getGregorianDate(1000, Calendar.FEBRUARY, 1), Boolean.FALSE); data.put(getGregorianDate(1000, Calendar.FEBRUARY, 28), Boolean.FALSE); data.put(getGregorianDate(1000, Calendar.MARCH, 1), Boolean.TRUE); data.put(getGregorianDate(1001, Calendar.JANUARY, 1), Boolean.TRUE); data.put(getGregorianDate(1001, Calendar.JANUARY, 6), Boolean.TRUE); data.put(getGregorianDate(1001, Calendar.MARCH, 1), Boolean.TRUE); Iterator itr = data.keySet().iterator(); while (itr.hasNext()) { Date d = itr.next(); boolean expected = data.get(d).booleanValue(); GregorianCalendar cal = new GregorianCalendar(); cal.setGregorianChange(d); if (cal.isLeapYear(1000) != expected) { errln("isLeapYear(1000) returned " + cal.isLeapYear(1000) + " with cutover date (Julian) " + d); } } } finally { TimeZone.setDefault(savedZone); } } // Note that we can't use Date to produce Gregorian calendar dates // before the default cutover date. static Date getGregorianDate(int year, int month, int dayOfMonth) { GregorianCalendar g = new GregorianCalendar(); // Make g a pure Gregorian calendar g.setGregorianChange(new Date(Long.MIN_VALUE)); g.clear(); g.set(year, month, dayOfMonth); return g.getTime(); } /** * 5006864: Define the minimum value of DAY_OF_WEEK_IN_MONTH as 1 */ public void Test5006864() { GregorianCalendar cal = new GregorianCalendar(); int min = cal.getMinimum(cal.DAY_OF_WEEK_IN_MONTH); if (min != 1) { errln("GregorianCalendar.getMinimum(DAY_OF_WEEK_IN_MONTH) returned " + min + ", expected 1."); } min = cal.getGreatestMinimum(cal.DAY_OF_WEEK_IN_MONTH); if (min != 1) { errln("GregorianCalendar.getGreatestMinimum(DAY_OF_WEEK_IN_MONTH) returned " + min + ", expected 1."); } } }