Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Calendar/CalendarTest.java
47182 views
/*1* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4064654 4374886 4984320 4984574 494479526* @library /java/text/testlib27* @summary test for Calendar28* @key randomness29*/3031import java.util.*;32import java.text.*;33import java.io.*;3435public class CalendarTest extends IntlTest {36static final int ONE_DAY = 24*60*60*1000;37static final int EPOCH_JULIAN = 2440588;3839public static void main(String argv[]) throws Exception {40new CalendarTest().run(argv);41}4243/**44* Test the behavior of the GregorianCalendar around the changeover.45*/46public void TestGregorianChangeover() {47TimeZone savedZone = TimeZone.getDefault();48/*49Changeover -7 days: 1582/9/28 dow=650Changeover -6 days: 1582/9/29 dow=751Changeover -5 days: 1582/9/30 dow=152Changeover -4 days: 1582/10/1 dow=253Changeover -3 days: 1582/10/2 dow=354Changeover -2 days: 1582/10/3 dow=455Changeover -1 days: 1582/10/4 dow=556Changeover +0 days: 1582/10/15 dow=657Changeover +1 days: 1582/10/16 dow=758Changeover +2 days: 1582/10/17 dow=159Changeover +3 days: 1582/10/18 dow=260Changeover +4 days: 1582/10/19 dow=361Changeover +5 days: 1582/10/20 dow=462Changeover +6 days: 1582/10/21 dow=563Changeover +7 days: 1582/10/22 dow=664*/65int MON[] = { 9, 9, 9,10,10,10,10, 10, 10, 10, 10, 10, 10, 10, 10 };66int DOM[] = { 28, 29, 30, 1, 2, 3, 4, 15, 16, 17, 18, 19, 20, 21, 22 };67int DOW[] = { 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6 };68// ^ <-Changeover Fri Oct 15 15826970try {71TimeZone.setDefault(TimeZone.getTimeZone("GMT"));72Date co = new Date(1582-1900, Calendar.OCTOBER, 15);73GregorianCalendar cal = new GregorianCalendar();74int j = 0;75for (int i = -7; i <= 7; ++i, ++j) {76Date d = new Date(co.getTime() + i*ONE_DAY);77cal.setTime(d);78int y = cal.get(Calendar.YEAR);79int mon = cal.get(Calendar.MONTH)+1-Calendar.JANUARY;80int dom = cal.get(Calendar.DATE);81int dow = cal.get(Calendar.DAY_OF_WEEK);8283logln("Changeover " + (i>=0?"+":"") + i +84" days: " + y + "/" + mon + "/" + dom + " dow=" + dow);85if (y != 1582 || mon != MON[j] || dom != DOM[j] || dow != DOW[j]) {86errln(" Fail: Above line is wrong");87}88}89}90finally {91TimeZone.setDefault(savedZone);92}93}9495/**96* Test the mapping between millis and fields. For the purposes97* of this test, we don't care about timezones and week data98* (first day of week, minimal days in first week).99*/100public void TestMapping() {101TimeZone saveZone = TimeZone.getDefault();102int[] DATA = {103// Julian# Year Month DOM JULIAN:Year, Month, DOM1042440588, 1970, Calendar.JANUARY, 1, 1969, Calendar.DECEMBER, 19,1052415080, 1900, Calendar.MARCH, 1, 1900, Calendar.FEBRUARY, 17,1062451604, 2000, Calendar.FEBRUARY, 29, 2000, Calendar.FEBRUARY, 16,1072452269, 2001, Calendar.DECEMBER, 25, 2001, Calendar.DECEMBER, 12,1082416526, 1904, Calendar.FEBRUARY, 15, 1904, Calendar.FEBRUARY, 2,1092416656, 1904, Calendar.JUNE, 24, 1904, Calendar.JUNE, 11,1101721426, 1, Calendar.JANUARY, 1, 1, Calendar.JANUARY, 3,1112000000, 763, Calendar.SEPTEMBER, 18, 763, Calendar.SEPTEMBER, 14,1124000000, 6239, Calendar.JULY, 12, 6239, Calendar.MAY, 28,1138000000, 17191, Calendar.FEBRUARY, 26, 17190, Calendar.OCTOBER, 22,11410000000, 22666, Calendar.DECEMBER, 20, 22666, Calendar.JULY, 5,115};116117try {118TimeZone.setDefault(TimeZone.getTimeZone("UTC"));119Date PURE_GREGORIAN = new Date(Long.MIN_VALUE);120Date PURE_JULIAN = new Date(Long.MAX_VALUE);121GregorianCalendar cal = new GregorianCalendar();122for (int i = 0; i < DATA.length; i += 7) {123int julian = DATA[i];124int year = DATA[i+1];125int month = DATA[i+2];126int dom = DATA[i+3];127int year2, month2, dom2;128long millis = ((long)julian - EPOCH_JULIAN) * ONE_DAY;129String s;130131// Test Gregorian computation132cal.setGregorianChange(PURE_GREGORIAN);133cal.clear();134cal.set(year, month, dom);135long calMillis = cal.getTime().getTime();136long delta = calMillis - millis;137cal.setTime(new Date(millis));138year2 = cal.get(Calendar.YEAR);139month2 = cal.get(Calendar.MONTH);140dom2 = cal.get(Calendar.DAY_OF_MONTH);141s = "G " + year + "-" + (month+1-Calendar.JANUARY) + "-" + dom +142" => " + calMillis +143" (" + ((float)delta/ONE_DAY) + " day delta) => " +144year2 + "-" + (month2+1-Calendar.JANUARY) + "-" + dom2;145if (delta != 0 || year != year2 || month != month2 ||146dom != dom2) {147errln(s + " FAIL");148} else {149logln(s);150}151152// Test Julian computation153year = DATA[i+4];154month = DATA[i+5];155dom = DATA[i+6];156cal.setGregorianChange(PURE_JULIAN);157cal.clear();158cal.set(year, month, dom);159calMillis = cal.getTime().getTime();160delta = calMillis - millis;161cal.setTime(new Date(millis));162year2 = cal.get(Calendar.YEAR);163month2 = cal.get(Calendar.MONTH);164dom2 = cal.get(Calendar.DAY_OF_MONTH);165s = "J " + year + "-" + (month+1-Calendar.JANUARY) + "-" + dom +166" => " + calMillis +167" (" + ((float)delta/ONE_DAY) + " day delta) => " +168year2 + "-" + (month2+1-Calendar.JANUARY) + "-" + dom2;169if (delta != 0 || year != year2 || month != month2 ||170dom != dom2) {171errln(s + " FAIL");172} else {173logln(s);174}175}176177cal.setGregorianChange(new Date(1582-1900, Calendar.OCTOBER, 15));178auxMapping(cal, 1582, Calendar.OCTOBER, 4);179auxMapping(cal, 1582, Calendar.OCTOBER, 15);180auxMapping(cal, 1582, Calendar.OCTOBER, 16);181for (int y = 800; y < 3000; y += 1+(int)100*Math.random()) {182for (int m = Calendar.JANUARY; m <= Calendar.DECEMBER; ++m) {183auxMapping(cal, y, m, 15);184}185}186}187finally {188TimeZone.setDefault(saveZone);189}190}191private void auxMapping(Calendar cal, int y, int m, int d) {192cal.clear();193cal.set(y, m, d);194long millis = cal.getTime().getTime();195cal.setTime(new Date(millis));196int year2 = cal.get(Calendar.YEAR);197int month2 = cal.get(Calendar.MONTH);198int dom2 = cal.get(Calendar.DAY_OF_MONTH);199if (y != year2 || m != month2 || dom2 != d)200errln("Round-trip failure: " + y + "-" + (m+1) + "-"+d+" =>ms=> " +201year2 + "-" + (month2+1) + "-" + dom2);202}203204public void TestGenericAPI() {205if (Locale.getDefault().equals(new Locale("th", "TH"))) {206return;207}208209String str;210211Date when = new Date(90, Calendar.APRIL, 15);212213String tzid = "TestZone";214int tzoffset = 123400;215216SimpleTimeZone zone = new SimpleTimeZone(tzoffset, tzid);217Calendar cal = (Calendar)Calendar.getInstance((SimpleTimeZone)zone.clone());218219if (!zone.equals(cal.getTimeZone())) errln("FAIL: Calendar.getTimeZone failed");220221Calendar cal2 = Calendar.getInstance(cal.getTimeZone());222223cal.setTime(when);224cal2.setTime(when);225226if (!(cal.equals(cal2))) errln("FAIL: Calendar.operator== failed");227// if ((*cal != *cal2)) errln("FAIL: Calendar.operator!= failed");228if (!cal.equals(cal2) ||229cal.before(cal2) ||230cal.after(cal2)) errln("FAIL: equals/before/after failed");231232cal2.setTime(new Date(when.getTime() + 1000));233if (cal.equals(cal2) ||234cal2.before(cal) ||235cal.after(cal2)) errln("FAIL: equals/before/after failed");236237cal.roll(Calendar.SECOND, true);238if (!cal.equals(cal2) ||239cal.before(cal2) ||240cal.after(cal2)) errln("FAIL: equals/before/after failed");241242// Roll back to January243cal.roll(Calendar.MONTH, (int)(1 + Calendar.DECEMBER - cal.get(Calendar.MONTH)));244if (cal.equals(cal2) ||245cal2.before(cal) ||246cal.after(cal2)) errln("FAIL: equals/before/after failed");247248// C++ only249/* TimeZone z = cal.orphanTimeZone();250if (z.getID(str) != tzid ||251z.getRawOffset() != tzoffset)252errln("FAIL: orphanTimeZone failed");253*/254255for (int i = 0; i < 2; ++i) {256boolean lenient = ( i > 0 );257cal.setLenient(lenient);258if (lenient != cal.isLenient()) errln("FAIL: setLenient/isLenient failed");259// Later: Check for lenient behavior260}261262int i;263for (i = Calendar.SUNDAY; i <= Calendar.SATURDAY; ++i) {264cal.setFirstDayOfWeek(i);265if (cal.getFirstDayOfWeek() != i) errln("FAIL: set/getFirstDayOfWeek failed");266}267268for (i = 0; i <= 7; ++i) {269cal.setMinimalDaysInFirstWeek(i);270if (cal.getMinimalDaysInFirstWeek() != i) errln("FAIL: set/getFirstDayOfWeek failed");271}272273for (i = 0; i < Calendar.FIELD_COUNT; ++i) {274if (cal.getMinimum(i) != cal.getGreatestMinimum(i))275errln("FAIL: getMinimum doesn't match getGreatestMinimum for field " + i);276if (cal.getLeastMaximum(i) > cal.getMaximum(i))277errln("FAIL: getLeastMaximum larger than getMaximum for field " + i);278if (cal.getMinimum(i) >= cal.getMaximum(i))279errln("FAIL: getMinimum not less than getMaximum for field " + i);280}281282cal.setTimeZone(TimeZone.getDefault());283cal.clear();284cal.set(1984, 5, 24);285if (cal.getTime().getTime() != new Date(84, 5, 24).getTime()) {286errln("FAIL: Calendar.set(3 args) failed");287logln(" Got: " + cal.getTime() + " Expected: " + new Date(84, 5, 24));288}289290cal.clear();291cal.set(1985, 3, 2, 11, 49);292if (cal.getTime().getTime() != new Date(85, 3, 2, 11, 49).getTime()) {293errln("FAIL: Calendar.set(5 args) failed");294logln(" Got: " + cal.getTime() + " Expected: " + new Date(85, 3, 2, 11, 49));295}296297cal.clear();298cal.set(1995, 9, 12, 1, 39, 55);299if (cal.getTime().getTime() != new Date(95, 9, 12, 1, 39, 55).getTime()) {300errln("FAIL: Calendar.set(6 args) failed");301logln(" Got: " + cal.getTime() + " Expected: " + new Date(95, 9, 12, 1, 39, 55));302}303304cal.getTime();305for (i = 0; i < Calendar.FIELD_COUNT; ++i) {306switch(i) {307case Calendar.YEAR: case Calendar.MONTH: case Calendar.DATE:308case Calendar.HOUR_OF_DAY: case Calendar.MINUTE: case Calendar.SECOND:309if (!cal.isSet(i))310errln("FAIL: !Calendar.isSet test failed: " + calendarFieldNames[i]);311break;312default:313if (cal.isSet(i))314errln("FAIL: Calendar.isSet test failed: " + calendarFieldNames[i]);315}316cal.clear(i);317if (cal.isSet(i)) errln("FAIL: Calendar.clear/isSet failed");318}319320// delete cal;321// delete cal2;322323Locale[] loc = Calendar.getAvailableLocales();324long count = loc.length;325if (count < 1 || loc == null) {326errln("FAIL: getAvailableLocales failed");327}328else {329for (i = 0; i < count; ++i) {330cal = Calendar.getInstance(loc[i]);331// delete cal;332}333}334335cal = Calendar.getInstance(TimeZone.getDefault(), Locale.ENGLISH);336// delete cal;337338cal = Calendar.getInstance(zone, Locale.ENGLISH);339// delete cal;340341GregorianCalendar gc = new GregorianCalendar(zone);342// delete gc;343344gc = new GregorianCalendar(Locale.ENGLISH);345// delete gc;346347gc = new GregorianCalendar(Locale.ENGLISH);348// delete gc;349350gc = new GregorianCalendar(zone, Locale.ENGLISH);351// delete gc;352353gc = new GregorianCalendar(zone);354// delete gc;355356gc = new GregorianCalendar(1998, 10, 14, 21, 43);357if (gc.getTime().getTime() != new Date(98, 10, 14, 21, 43).getTime())358errln("FAIL: new GregorianCalendar(ymdhm) failed");359// delete gc;360361gc = new GregorianCalendar(1998, 10, 14, 21, 43, 55);362if (gc.getTime().getTime() != new Date(98, 10, 14, 21, 43, 55).getTime())363errln("FAIL: new GregorianCalendar(ymdhms) failed");364365// C++ only:366// GregorianCalendar gc2 = new GregorianCalendar(Locale.ENGLISH);367// gc2 = gc;368// if (gc2 != gc || !(gc2 == gc)) errln("FAIL: GregorianCalendar assignment/operator==/operator!= failed");369// delete gc;370// delete z;371}372373// Verify Roger Webster's bug374public void TestRog() {375GregorianCalendar gc = new GregorianCalendar();376377int year = 1997, month = Calendar.APRIL, date = 1;378gc.set(year, month, date); // April 1, 1997379380gc.set(Calendar.HOUR_OF_DAY, 23);381gc.set(Calendar.MINUTE, 0);382gc.set(Calendar.SECOND, 0);383gc.set(Calendar.MILLISECOND, 0);384385for (int i = 0; i < 9; i++, gc.add(Calendar.DATE, 1)) {386if (gc.get(Calendar.YEAR) != year ||387gc.get(Calendar.MONTH) != month ||388gc.get(Calendar.DATE) != (date + i))389errln("FAIL: Date " + gc.getTime() + " wrong");390}391}392393// Verify DAY_OF_WEEK394public void TestDOW943() {395dowTest(false);396dowTest(true);397}398399void dowTest(boolean lenient) {400GregorianCalendar cal = new GregorianCalendar();401cal.set(1997, Calendar.AUGUST, 12); // Wednesday402cal.getTime(); // Force update403cal.setLenient(lenient);404cal.set(1996, Calendar.DECEMBER, 1); // Set the date to be December 1, 1996405int dow = cal.get(Calendar.DAY_OF_WEEK);406int min = cal.getMinimum(Calendar.DAY_OF_WEEK);407int max = cal.getMaximum(Calendar.DAY_OF_WEEK);408if (dow < min || dow > max) errln("FAIL: Day of week " + dow + " out of range");409if (dow != Calendar.SUNDAY) {410errln("FAIL2: Day of week should be SUNDAY; is " + dow + ": " + cal.getTime());411}412if (min != Calendar.SUNDAY || max != Calendar.SATURDAY) errln("FAIL: Min/max bad");413}414415// Verify that the clone method produces distinct objects with no416// unintentionally shared fields.417public void TestClonesUnique908() {418Calendar c = Calendar.getInstance();419Calendar d = (Calendar)c.clone();420c.set(Calendar.MILLISECOND, 123);421d.set(Calendar.MILLISECOND, 456);422if (c.get(Calendar.MILLISECOND) != 123 ||423d.get(Calendar.MILLISECOND) != 456) {424errln("FAIL: Clones share fields");425}426}427428// Verify effect of Gregorian cutoff value429public void TestGregorianChange768() {430boolean b;431GregorianCalendar c = new GregorianCalendar();432logln("With cutoff " + c.getGregorianChange());433logln(" isLeapYear(1800) = " + (b=c.isLeapYear(1800)));434logln(" (should be FALSE)");435if (b != false) errln("FAIL");436c.setGregorianChange(new Date(0, 0, 1)); // Jan 1 1900437logln("With cutoff " + c.getGregorianChange());438logln(" isLeapYear(1800) = " + (b=c.isLeapYear(1800)));439logln(" (should be TRUE)");440if (b != true) errln("FAIL");441}442443// Test the correct behavior of the disambiguation algorithm.444public void TestDisambiguation765() throws Exception {445Locale savedLocale = Locale.getDefault();446try {447Locale.setDefault(Locale.US);448Calendar c = Calendar.getInstance();449c.setLenient(false);450451c.clear();452c.set(Calendar.YEAR, 1997);453c.set(Calendar.MONTH, Calendar.JUNE);454c.set(Calendar.DATE, 3);455456verify765("1997 third day of June = ", c, 1997, Calendar.JUNE, 3);457458c.clear();459c.set(Calendar.YEAR, 1997);460c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);461c.set(Calendar.MONTH, Calendar.JUNE);462c.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);463verify765("1997 first Tuesday in June = ", c, 1997, Calendar.JUNE, 3);464465c.setLenient(true); // for 4944795466c.clear();467c.set(Calendar.YEAR, 1997);468c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);469c.set(Calendar.MONTH, Calendar.JUNE);470c.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1);471verify765("1997 last Tuesday in June = ", c, 1997, Calendar.JUNE, 24);472473c.setLenient(false);474IllegalArgumentException e = null;475try {476c.clear();477c.set(Calendar.YEAR, 1997);478c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);479c.set(Calendar.MONTH, Calendar.JUNE);480c.set(Calendar.DAY_OF_WEEK_IN_MONTH, 0);481c.getTime();482}483catch (IllegalArgumentException ex) {484e = ex;485}486verify765("1997 zero-th Tuesday in June = ", e);487488c.clear();489c.set(Calendar.YEAR, 1997);490c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);491c.set(Calendar.MONTH, Calendar.JUNE);492c.set(Calendar.WEEK_OF_MONTH, 1);493verify765("1997 Tuesday in week 1 of June = ", c, 1997, Calendar.JUNE, 3);494495c.clear();496c.set(Calendar.YEAR, 1997);497c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);498c.set(Calendar.MONTH, Calendar.JUNE);499c.set(Calendar.WEEK_OF_MONTH, 4);500verify765("1997 Tuesday in week 4 of June = ", c, 1997, Calendar.JUNE, 24);501502try {503c.clear();504c.set(Calendar.YEAR, 1997);505c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);506c.set(Calendar.MONTH, Calendar.JUNE);507c.set(Calendar.WEEK_OF_MONTH, 1);508verify765("1997 Tuesday in week 0 of June = ", c, 1997, Calendar.JUNE, 3);509}510catch (IllegalArgumentException ex) {511errln("FAIL: Exception seen: " + ex.getMessage());512// ex.printStackTrace(log);513}514515c.clear();516c.set(Calendar.YEAR, 1997);517c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);518c.set(Calendar.WEEK_OF_YEAR, 2);519verify765("1997 Tuesday in week 2 of year = ", c, 1997, Calendar.JANUARY, 7);520521c.clear();522c.set(Calendar.YEAR, 1997);523c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);524c.set(Calendar.WEEK_OF_YEAR, 10);525verify765("1997 Tuesday in week 10 of year = ", c, 1997, Calendar.MARCH, 4);526527try {528c.clear();529c.set(Calendar.YEAR, 1997);530c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);531c.set(Calendar.WEEK_OF_YEAR, 0);532verify765("1997 Tuesday in week 0 of year = ", c, 1996, Calendar.DECEMBER, 24);533throw new Exception("Fail: WEEK_OF_YEAR 0 should be illegal");534}535catch (IllegalArgumentException ex) {}536}537finally {538Locale.setDefault(savedLocale);539}540}541void verify765(String msg, Calendar c, int year, int month, int day) {542if (c.get(Calendar.YEAR) == year &&543c.get(Calendar.MONTH) == month &&544c.get(Calendar.DATE) == day) {545logln("PASS: " + msg + c.getTime());546}547else {548errln("FAIL: " + msg + c.getTime() +549"; expected " +550year + "/" + (month+1) + "/" + day);551}552}553// Called when e expected to be non-null554void verify765(String msg, IllegalArgumentException e) {555if (e == null) errln("FAIL: No IllegalArgumentException for " + msg);556else logln("PASS: " + msg + "IllegalArgument as expected");557}558559// Test the behavior of GMT vs. local time560public void TestGMTvsLocal4064654() {561if (Locale.getDefault().equals(new Locale("th", "TH"))) {562return;563}564565// Sample output 1:566// % /usr/local/java/jdk1.1.3/solaris/bin/java test 1997 1 1 12 0 0567// date = Wed Jan 01 04:00:00 PST 1997568// offset for Wed Jan 01 04:00:00 PST 1997= -8hr569test4064654(1997, 1, 1, 12, 0, 0);570571// Sample output 2:572// % /usr/local/java/jdk1.1.3/solaris/bin/java test 1997 4 16 18 30 0573// date = Wed Apr 16 10:30:00 PDT 1997574// offset for Wed Apr 16 10:30:00 PDT 1997= -7hr575576// Note that in sample output 2 according to the offset, the gmt time577// of the result would be 1997 4 16 17 30 0 which is different from the578// input of 1997 4 16 18 30 0.579test4064654(1997, 4, 16, 18, 30, 0);580}581void test4064654(int yr, int mo, int dt, int hr, int mn, int sc) {582Date date;583Calendar gmtcal = Calendar.getInstance();584gmtcal.setTimeZone(TimeZone.getTimeZone("Africa/Casablanca"));585gmtcal.set(yr, mo-1, dt, hr, mn, sc);586gmtcal.set(Calendar.MILLISECOND, 0);587588date = gmtcal.getTime();589logln("date = "+date);590591Calendar cal = Calendar.getInstance();592cal.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));593cal.setTime(date);594595int offset = cal.getTimeZone().getOffset(cal.get(Calendar.ERA),596cal.get(Calendar.YEAR),597cal.get(Calendar.MONTH),598cal.get(Calendar.DATE),599cal.get(Calendar.DAY_OF_WEEK),600cal.get(Calendar.MILLISECOND));601602logln("offset for "+date+"= "+(offset/1000/60/60.0) + "hr");603604int utc = ((cal.get(Calendar.HOUR_OF_DAY) * 60 +605cal.get(Calendar.MINUTE)) * 60 +606cal.get(Calendar.SECOND)) * 1000 +607cal.get(Calendar.MILLISECOND) - offset;608609int expected = ((hr * 60 + mn) * 60 + sc) * 1000;610611if (utc != expected)612errln("FAIL: Discrepancy of " +613(utc - expected) + " millis = " +614((utc-expected)/1000/60/60.0) + " hr");615}616617// Verify that add and set work regardless of the order in which618// they are called.619public void TestAddSetOrder621() {620Date d = new Date(97, 4, 14, 13, 23, 45);621622Calendar cal = Calendar.getInstance ();623cal.setTime (d);624cal.add (Calendar.DATE, -5);625cal.set (Calendar.HOUR_OF_DAY, 0);626cal.set (Calendar.MINUTE, 0);627cal.set (Calendar.SECOND, 0);628// ma feb 03 00:00:00 GMT+00:00 1997629String s = cal.getTime ().toString ();630631cal = Calendar.getInstance ();632cal.setTime (d);633cal.set (Calendar.HOUR_OF_DAY, 0);634cal.set (Calendar.MINUTE, 0);635cal.set (Calendar.SECOND, 0);636cal.add (Calendar.DATE, -5);637// ma feb 03 13:11:06 GMT+00:00 1997638String s2 = cal.getTime ().toString ();639640if (s.equals(s2))641logln("Pass: " + s + " == " + s2);642else643errln("FAIL: " + s + " != " + s2);644}645646// Verify that add works.647public void TestAdd520() {648int y = 1997, m = Calendar.FEBRUARY, d = 1;649GregorianCalendar temp = new GregorianCalendar( y, m, d );650check520(temp, y, m, d);651652temp.add( temp.YEAR, 1 );653y++;654check520(temp, y, m, d);655656temp.add( temp.MONTH, 1 );657m++;658check520(temp, y, m, d);659660temp.add( temp.DATE, 1 );661d++;662check520(temp, y, m, d);663664temp.add( temp.DATE, 2 );665d += 2;666check520(temp, y, m, d);667668temp.add( temp.DATE, 28 );669d = 1; ++m;670check520(temp, y, m, d);671}672void check520(Calendar c, int y, int m, int d) {673if (c.get(Calendar.YEAR) != y ||674c.get(Calendar.MONTH) != m ||675c.get(Calendar.DATE) != d) {676errln("FAILURE: Expected YEAR/MONTH/DATE of " +677y + "/" + (m+1) + "/" + d +678"; got " +679c.get(Calendar.YEAR) + "/" +680(c.get(Calendar.MONTH)+1) + "/" +681c.get(Calendar.DATE));682}683else logln("Confirmed: " +684y + "/" + (m+1) + "/" + d);685}686687// Verify that setting fields works. This test fails when an exception is thrown.688public void TestFieldSet4781() {689try {690GregorianCalendar g = new GregorianCalendar();691GregorianCalendar g2 = new GregorianCalendar();692// At this point UTC value is set, various fields are not.693// Now set to noon.694g2.set(Calendar.HOUR, 12);695g2.set(Calendar.MINUTE, 0);696g2.set(Calendar.SECOND, 0);697// At this point the object thinks UTC is NOT set, but fields are set.698// The following line will result in IllegalArgumentException because699// it thinks the YEAR is set and it is NOT.700if (g2.equals(g))701logln("Same");702else703logln("Different");704}705catch (IllegalArgumentException e) {706errln("Unexpected exception seen: " + e);707}708}709710// Test serialization of a Calendar object711public void TestSerialize337() {712Calendar cal = Calendar.getInstance();713714boolean ok = false;715716try {717FileOutputStream f = new FileOutputStream(FILENAME);718ObjectOutput s = new ObjectOutputStream(f);719s.writeObject(PREFIX);720s.writeObject(cal);721s.writeObject(POSTFIX);722f.close();723724FileInputStream in = new FileInputStream(FILENAME);725ObjectInputStream t = new ObjectInputStream(in);726String pre = (String)t.readObject();727Calendar c = (Calendar)t.readObject();728String post = (String)t.readObject();729in.close();730731ok = pre.equals(PREFIX) &&732post.equals(POSTFIX) &&733cal.equals(c);734735File fl = new File(FILENAME);736fl.delete();737}738catch (IOException e) {739errln("FAIL: Exception received:");740// e.printStackTrace(log);741}742catch (ClassNotFoundException e) {743errln("FAIL: Exception received:");744// e.printStackTrace(log);745}746747if (!ok) errln("Serialization of Calendar object failed.");748}749static final String PREFIX = "abc";750static final String POSTFIX = "def";751static final String FILENAME = "tmp337.bin";752753// Try to zero out the seconds field754public void TestSecondsZero121() {755Calendar cal = new GregorianCalendar();756// Initialize with current date/time757cal.setTime(new Date());758// Round down to minute759cal.set(Calendar.SECOND, 0);760Date d = cal.getTime();761String s = d.toString();762if (s.indexOf(":00 ") < 0) errln("Expected to see :00 in " + s);763}764765// Try various sequences of add, set, and get method calls.766public void TestAddSetGet0610() {767//768// Error case 1:769// - Upon initialization calendar fields, millis = System.currentTime770// - After set is called fields are initialized, time is not771// - Addition uses millis which are still *now*772//773{774Calendar calendar = new GregorianCalendar( ) ;775calendar.set( 1993, Calendar.JANUARY, 4 ) ;776logln( "1A) " + value( calendar ) ) ;777calendar.add( Calendar.DATE, 1 ) ;778String v = value(calendar);779logln( "1B) " + v );780logln( "--) 1993/0/5" ) ;781if (!v.equals(EXPECTED_0610)) errln("Expected " + EXPECTED_0610 +782"; saw " + v);783}784785//786// Error case 2:787// - Upon initialization calendar fields set, millis = 0788// - Addition uses millis which are still 1970, 0, 1789//790791{792Calendar calendar = new GregorianCalendar( 1993, Calendar.JANUARY, 4 ) ;793logln( "2A) " + value( calendar ) ) ;794calendar.add( Calendar.DATE, 1 ) ;795String v = value(calendar);796logln( "2B) " + v );797logln( "--) 1993/0/5" ) ;798if (!v.equals(EXPECTED_0610)) errln("Expected " + EXPECTED_0610 +799"; saw " + v);800}801802//803// Error case 3:804// - Upon initialization calendar fields, millis = 0805// - getTime( ) is called which forces the millis to be set806// - Addition uses millis which are correct807//808809{810Calendar calendar = new GregorianCalendar( 1993, Calendar.JANUARY, 4 ) ;811logln( "3A) " + value( calendar ) ) ;812calendar.getTime( ) ;813calendar.add( Calendar.DATE, 1 ) ;814String v = value(calendar);815logln( "3B) " + v ) ;816logln( "--) 1993/0/5" ) ;817if (!v.equals(EXPECTED_0610)) errln("Expected " + EXPECTED_0610 +818"; saw " + v);819}820}821static String value( Calendar calendar ) {822return( calendar.get( Calendar.YEAR ) + "/" +823calendar.get( Calendar.MONTH ) + "/" +824calendar.get( Calendar.DATE ) ) ;825}826static String EXPECTED_0610 = "1993/0/5";827828// Test that certain fields on a certain date are as expected.829public void TestFields060() {830int year = 1997;831int month = java.util.Calendar.OCTOBER; //october832int dDate = 22; //DAYOFWEEK should return 3 for Wednesday833GregorianCalendar calendar = null;834835calendar = new GregorianCalendar( year, month, dDate);836for (int i = 0; i < EXPECTED_FIELDS.length; ) {837int field = EXPECTED_FIELDS[i++];838int expected = EXPECTED_FIELDS[i++];839if (calendar.get(field) != expected) {840errln("Expected field " + field + " to have value " + expected +841"; received " + calendar.get(field) + " instead");842}843}844}845static int EXPECTED_FIELDS[] = {846Calendar.YEAR, 1997,847Calendar.MONTH, Calendar.OCTOBER,848Calendar.DAY_OF_MONTH, 22,849Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY,850Calendar.DAY_OF_WEEK_IN_MONTH, 4,851Calendar.DAY_OF_YEAR, 295852};853854static final String[] calendarFieldNames = {855/* 0 */ "ERA",856/* 1 */ "YEAR",857/* 2 */ "MONTH",858/* 3 */ "WEEK_OF_YEAR",859/* 4 */ "WEEK_OF_MONTH",860/* 5 */ "DAY_OF_MONTH",861/* 6 */ "DAY_OF_YEAR",862/* 7 */ "DAY_OF_WEEK",863/* 8 */ "DAY_OF_WEEK_IN_MONTH",864/* 9 */ "AM_PM",865/* 10 */ "HOUR",866/* 11 */ "HOUR_OF_DAY",867/* 12 */ "MINUTE",868/* 13 */ "SECOND",869/* 14 */ "MILLISECOND",870/* 15 */ "ZONE_OFFSET",871/* 16 */ "DST_OFFSET"872};873874// Verify that the fields are as expected (mostly zero) at the epoch start.875// Note that we adjust for the default timezone to get most things to zero.876public void TestEpochStartFields() {877if (Locale.getDefault().equals(new Locale("th", "TH"))) {878return;879}880881String[][] lt = {882{"en", "US", "US/Pacific"}, /* First day = 1, Minimum day = 1 */883{"en", "US", "America/Anchorage"}, /* First day = 1, Minimum day = 1 */884{"en", "TO", "Pacific/Tongatapu"}, /* First day = 1, Minimum day = 1 */885{"en", "MH", "Pacific/Majuro"}, /* First day = 1, Minimum day = 1 */886{"ja", "JP", "Asia/Tokyo"}, /* First day = 1, Minimum day = 1 */887{"iw", "IL", "Asia/Jerusalem"}, /* First day = 1, Minimum day = 1 */888{"hi", "IN", "Asia/Jakarta"}, /* First day = 1, Minimum day = 1 */889{"en", "GB", "Europe/London"}, /* First day = 2, Minimum day = 1 */890{"en", "GB", "GMT"}, /* First day = 2, Minimum day = 1 */891{"de", "DE", "Europe/Berlin"}, /* First day = 2, Minimum day = 4 */892{"ar", "EG", "Africa/Cairo"}, /* First day = 7, Minimum day = 1 */893};894895int[][] goldenData = {896{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, -28800000, 0},897{1, 1969, 11, 1, 5, 31, 365, 4, 5, 1, 11, 23, 0, 0, 0, -36000000, 0},898{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, 46800000, 0},899{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, 43200000, 0},900{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, 32400000, 0},901{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, 7200000, 0},902{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, 25200000, 0},903{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 1, 1, 0, 0, 0, 3600000, 0},904{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0},905{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, 3600000, 0},906{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, 7200000, 0},907};908909Locale savedLocale = Locale.getDefault();910TimeZone savedTimeZone = TimeZone.getDefault();911912try {913for (int j = 0; j < lt.length; j++) {914Locale l = new Locale(lt[j][0], lt[j][1]);915TimeZone z = TimeZone.getTimeZone(lt[j][2]);916Locale.setDefault(l);917TimeZone.setDefault(z);918Calendar c = Calendar.getInstance();919Date d = new Date(-z.getRawOffset());920921int val;922int[] EPOCH_FIELDS = goldenData[j];923c.setTime(d);924925boolean err = false;926for (int i = 0; i < calendarFieldNames.length; ++i) {927if ((val = c.get(i)) != EPOCH_FIELDS[i]) {928errln("Wrong value: " + val +929" for field(" + calendarFieldNames[i] +930"), expected: " + EPOCH_FIELDS[i]);931err = true;932}933}934if (err) {935errln("Failed: \n\tDate=" + d + "\n\tTimeZone=" + z +936"\n\tLocale=" + l + "\n\tCalendar=" + c);937}938}939}940finally {941Locale.setDefault(savedLocale);942TimeZone.setDefault(savedTimeZone);943}944}945946// Verify that as you add days to the calendar (e.g., 24 day periods),947// the day of the week shifts in the expected pattern.948public void TestDOWProgression() {949Calendar cal =950new GregorianCalendar(1972, Calendar.OCTOBER, 26);951marchByDelta(cal, 24); // Last parameter must be != 0 modulo 7952}953954// Supply a delta which is not a multiple of 7.955void marchByDelta(Calendar cal, int delta) {956Calendar cur = (Calendar)cal.clone();957int initialDOW = cur.get(Calendar.DAY_OF_WEEK);958int DOW, newDOW = initialDOW;959do {960DOW = newDOW;961logln("DOW = " + DOW + " " + cur.getTime());962963cur.add(Calendar.DAY_OF_WEEK, delta);964newDOW = cur.get(Calendar.DAY_OF_WEEK);965int expectedDOW = 1 + (DOW + delta - 1) % 7;966if (newDOW != expectedDOW) {967errln("Day of week should be " + expectedDOW +968" instead of " + newDOW + " on " + cur.getTime());969return;970}971}972while (newDOW != initialDOW);973}974975public void TestActualMinMax() {976Calendar cal = new GregorianCalendar(1967, Calendar.MARCH, 10);977cal.setFirstDayOfWeek(Calendar.SUNDAY);978cal.setMinimalDaysInFirstWeek(3);979980if (cal.getActualMinimum(Calendar.DAY_OF_MONTH) != 1)981errln("Actual minimum date for 3/10/1967 should have been 1; got " +982cal.getActualMinimum(Calendar.DAY_OF_MONTH));983if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 31)984errln("Actual maximum date for 3/10/1967 should have been 31; got " +985cal.getActualMaximum(Calendar.DAY_OF_MONTH));986987cal.set(Calendar.MONTH, Calendar.FEBRUARY);988if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 28)989errln("Actual maximum date for 2/10/1967 should have been 28; got " +990cal.getActualMaximum(Calendar.DAY_OF_MONTH));991if (cal.getActualMaximum(Calendar.DAY_OF_YEAR) != 365)992errln("Number of days in 1967 should have been 365; got " +993cal.getActualMaximum(Calendar.DAY_OF_YEAR));994995cal.set(Calendar.YEAR, 1968);996if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 29)997errln("Actual maximum date for 2/10/1968 should have been 29; got " +998cal.getActualMaximum(Calendar.DAY_OF_MONTH));999if (cal.getActualMaximum(Calendar.DAY_OF_YEAR) != 366)1000errln("Number of days in 1968 should have been 366; got " +1001cal.getActualMaximum(Calendar.DAY_OF_YEAR));1002// Using week settings of SUNDAY/3 (see above)1003if (cal.getActualMaximum(Calendar.WEEK_OF_YEAR) != 52)1004errln("Number of weeks in 1968 should have been 52; got " +1005cal.getActualMaximum(Calendar.WEEK_OF_YEAR));10061007cal.set(Calendar.YEAR, 1976);1008// Using week settings of SUNDAY/3 (see above)1009if (cal.getActualMaximum(Calendar.WEEK_OF_YEAR) != 53)1010errln("Number of weeks in 1976 should have been 53; got " +1011cal.getActualMaximum(Calendar.WEEK_OF_YEAR));1012}10131014public void TestRoll() {1015Calendar cal = new GregorianCalendar(1997, Calendar.JANUARY, 31);10161017int[] dayValues = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 };10181019for (int i = 0; i < dayValues.length; i++) {1020Calendar cal2 = (Calendar)cal.clone();1021cal2.roll(Calendar.MONTH, i);1022if (cal2.get(Calendar.DAY_OF_MONTH) != dayValues[i])1023errln("Rolling the month in 1/31/1997 up by " + i + " should have yielded "1024+ ((i + 1) % 12) + "/" + dayValues[i] + "/1997, but actually yielded "1025+ ((i + 1) % 12) + "/" + cal2.get(Calendar.DAY_OF_MONTH) + "/1997.");1026}10271028cal.set(1996, Calendar.FEBRUARY, 29);10291030int[] monthValues = { 1, 2, 2, 2, 1, 2, 2, 2, 1, 2 };1031int[] dayValues2 = { 29, 1, 1, 1, 29, 1, 1, 1, 29, 1 };10321033for (int i = 0; i < dayValues2.length; i++) {1034Calendar cal2 = (Calendar)cal.clone();1035cal2.roll(Calendar.YEAR, i);1036if (cal2.get(Calendar.DAY_OF_MONTH) != dayValues2[i] || cal2.get(Calendar.MONTH)1037!= monthValues[i])1038errln("Rolling the year in 2/29/1996 up by " + i + " should have yielded "1039+ (monthValues[i] + 1) + "/" + dayValues2[i] + "/"1040+ (1996 + i) + ", but actually yielded "1041+ (cal2.get(Calendar.MONTH) + 1) + "/" +1042cal2.get(Calendar.DAY_OF_MONTH) + "/" + (1996 + i) + ".");1043}10441045// Test rolling hour of day1046cal.set(Calendar.HOUR_OF_DAY, 0);1047cal.roll(Calendar.HOUR_OF_DAY, -2);1048int f = cal.get(Calendar.HOUR_OF_DAY);1049if (f != 22) errln("Rolling HOUR_OF_DAY=0 delta=-2 gave " + f + " Wanted 22");1050cal.roll(Calendar.HOUR_OF_DAY, 5);1051f = cal.get(Calendar.HOUR_OF_DAY);1052if (f != 3) errln("Rolling HOUR_OF_DAY=22 delta=5 gave " + f + " Wanted 3");1053cal.roll(Calendar.HOUR_OF_DAY, 21);1054f = cal.get(Calendar.HOUR_OF_DAY);1055if (f != 0) errln("Rolling HOUR_OF_DAY=3 delta=21 gave " + f + " Wanted 0");10561057// Test rolling hour1058cal.set(Calendar.HOUR_OF_DAY, 0);1059cal.roll(Calendar.HOUR, -2);1060f = cal.get(Calendar.HOUR);1061if (f != 10) errln("Rolling HOUR=0 delta=-2 gave " + f + " Wanted 10");1062cal.roll(Calendar.HOUR, 5);1063f = cal.get(Calendar.HOUR);1064if (f != 3) errln("Rolling HOUR=10 delta=5 gave " + f + " Wanted 3");1065cal.roll(Calendar.HOUR, 9);1066f = cal.get(Calendar.HOUR);1067if (f != 0) errln("Rolling HOUR=3 delta=9 gave " + f + " Wanted 0");1068}10691070/*1071* Confirm that multiple calls to Calendar.set() works correctly.1072*/1073public void Test4374886() {1074Locale savedLocale = Locale.getDefault();1075TimeZone savedTimeZone = TimeZone.getDefault();10761077try {1078Locale.setDefault(Locale.US);1079TimeZone.setDefault(TimeZone.getTimeZone("PST"));10801081Calendar cal = Calendar.getInstance();1082cal.set(Calendar.YEAR, 2001);1083cal.set(Calendar.MONTH, Calendar.OCTOBER);1084cal.set(Calendar.WEEK_OF_YEAR, 4);1085cal.set(Calendar.DAY_OF_WEEK, 2);10861087if (cal.get(Calendar.YEAR) != 2001 ||1088cal.get(Calendar.MONTH) != Calendar.JANUARY ||1089cal.get(Calendar.DATE) != 22 ||1090cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {1091errln("Failed : got " + cal.getTime() + ", expected Mon Jan 22, 2001");1092}1093}1094finally {1095Locale.setDefault(savedLocale);1096TimeZone.setDefault(savedTimeZone);1097}1098}1099}11001101//eof110211031104