Path: blob/master/test/jdk/java/util/Calendar/GregorianCutoverTest.java
41149 views
/*1* Copyright (c) 2003, 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 4359204 4928615 4743587 4956232 6459836 654995326* @library /java/text/testlib27* @build Koyomi28* @run main GregorianCutoverTest29* @summary Unit tests related to the Gregorian cutover support.30*/3132import java.util.Date;33import java.util.Locale;34import java.util.TimeZone;3536import static java.util.GregorianCalendar.*;3738public class GregorianCutoverTest extends IntlTest {3940public static void main(String[] args) throws Exception {41TimeZone tz = TimeZone.getDefault();42Locale lc = Locale.getDefault();43try {44TimeZone.setDefault(TimeZone.getTimeZone("GMT"));45Locale.setDefault(Locale.US);4647new GregorianCutoverTest().run(args);48} finally {49TimeZone.setDefault(tz);50Locale.setDefault(lc);51}52}5354/**55* 4359204: GregorianCalendar.get(cal.DAY_OF_YEAR) is inconsistent for year 158256*/57public void Test4359204() {58Koyomi cal = new Koyomi();5960cal.set(1582, JANUARY, 1);61checkContinuity(cal, DAY_OF_YEAR);62checkContinuity(cal, WEEK_OF_YEAR);63cal.set(1582, OCTOBER, 1);64checkContinuity(cal, WEEK_OF_MONTH);6566// JCK tests the cutover date 1970-1-1 (Epoch)67cal.setGregorianChange(new Date(0));68cal.set(1969, JANUARY, 1);69checkContinuity(cal, DAY_OF_YEAR);70checkContinuity(cal, WEEK_OF_YEAR);71cal.set(1969, DECEMBER, 1);72checkContinuity(cal, WEEK_OF_MONTH);73cal.set(1970, JANUARY, 1);74checkContinuity(cal, DAY_OF_YEAR);75checkContinuity(cal, WEEK_OF_YEAR);7677// Use large date (year >= 50000)78@SuppressWarnings("deprecation")79Date d = new Date(50000 - 1900, JANUARY, 20);80cal.setGregorianChange(d);81cal.set(49998, JANUARY, 1);82checkContinuity(cal, DAY_OF_YEAR);83checkContinuity(cal, WEEK_OF_YEAR);84cal.set(49999, JANUARY, 1);85checkContinuity(cal, DAY_OF_YEAR);86checkContinuity(cal, WEEK_OF_YEAR);87cal.set(50000, JANUARY, 20);88checkContinuity(cal, DAY_OF_YEAR);89checkContinuity(cal, WEEK_OF_YEAR);9091// Handling of "overlapping" dates may still be incorrect as92// of 1.5. Also, there's no way to disambiguate "overlapping"93// dates.94// millis=-112033929600000: date=-1581-10-15T00:00:00.000Z95cal.setGregorianChange(new Date(-112033929600000L));96cal.set(ERA, AD);97cal.set(-1581, JANUARY, 1);98// The year should have 379 days.99checkContinuity(cal, DAY_OF_YEAR);100checkContinuity(cal, WEEK_OF_YEAR);101102logln("Default cutover");103cal = new Koyomi();104cal.set(1582, OCTOBER, 1);105logln(" roll --DAY_OF_MONTH from 1582/10/01");106cal.roll(DAY_OF_MONTH, -1);107if (!cal.checkDate(1582, OCTOBER, 31)) {108errln(cal.getMessage());109}110logln(" roll DAY_OF_MONTH+10 from 1582/10/31");111cal.roll(DAY_OF_MONTH, +10);112if (!cal.checkDate(1582, OCTOBER, 20)) {113errln(cal.getMessage());114}115logln(" roll DAY_OF_MONTH-10 from 1582/10/20");116cal.roll(DAY_OF_MONTH, -10);117if (!cal.checkDate(1582, OCTOBER, 31)) {118errln(cal.getMessage());119}120logln(" roll back one day further");121cal.roll(DAY_OF_MONTH, +1);122if (!cal.checkDate(1582, OCTOBER, 1)) {123errln(cal.getMessage());124}125126// should handle the gap between 1969/12/22 (Julian) to 1970/1/5 (Gregorian)127logln("Cutover date is 1970/1/5");128@SuppressWarnings("deprecation")129Date d1 = new Date(1970 - 1900, JANUARY, 5);130cal.setGregorianChange(d1);131cal.set(ERA, AD);132cal.set(YEAR, 1970);133logln(" Set DAY_OF_YEAR to the 28th day of 1970");134cal.set(DAY_OF_YEAR, 28);135if (!cal.checkDate(1970, FEBRUARY, 1)) {136errln(cal.getMessage());137}138if (!cal.checkFieldValue(WEEK_OF_YEAR, 5)) {139errln(cal.getMessage());140}141logln(" 1969/12/22 should be the 356th day of the year.");142cal.set(1969, DECEMBER, 22);143if (!cal.checkFieldValue(DAY_OF_YEAR, 356)) {144errln(cal.getMessage());145}146logln(" Set DAY_OF_YEAR to autual maximum.");147int actualMaxDayOfYear = cal.getActualMaximum(DAY_OF_YEAR);148if (actualMaxDayOfYear != 356) {149errln("actual maximum of DAY_OF_YEAR: got " + actualMaxDayOfYear + ", expected 356");150}151cal.set(DAY_OF_YEAR, actualMaxDayOfYear);152if (!cal.checkDate(1969, DECEMBER, 22)) {153errln(cal.getMessage());154}155cal.set(1969, DECEMBER, 22);156cal.roll(DAY_OF_YEAR, +1);157logln(" Set to 1969/12/22 and roll DAY_OF_YEAR++");158if (!cal.checkDate(1969, JANUARY, 1)) {159errln(cal.getMessage());160}161logln(" 1970/1/5 should be the first day of the year.");162cal.set(1970, JANUARY, 5);163if (!cal.checkFieldValue(DAY_OF_YEAR, 1)) {164errln(cal.getMessage());165}166logln(" roll --DAY_OF_MONTH from 1970/1/5");167cal.roll(DAY_OF_MONTH, -1);168if (!cal.checkDate(1970, JANUARY, 31)) {169errln(cal.getMessage());170}171logln(" roll back one day of month");172cal.roll(DAY_OF_MONTH, +1);173if (!cal.checkDate(1970, JANUARY, 5)) {174errln(cal.getMessage());175}176177// Test "missing" dates in non-lenient.178cal = new Koyomi(); // new instance for the default cutover179cal.setLenient(false);180try {181// the next day of 1582/10/4 (Julian) is 1582/10/15 (Gregorian)182logln("1582/10/10 doesn't exit with the default cutover.");183cal.set(1582, OCTOBER, 10);184cal.getTime();185errln(" Didn't throw IllegalArgumentException in non-lenient.");186} catch (IllegalArgumentException e) {187}188}189190private void checkContinuity(Koyomi cal, int field) {191cal.getTime();192logln(Koyomi.getFieldName(field) + " starting on " + cal.toDateString());193int max = cal.getActualMaximum(field);194for (int i = 1; i <= max; i++) {195logln(i + " " + cal.toDateString());196if (!cal.checkFieldValue(field, i)) {197errln(" " + cal.toDateString() + ":\t" + cal.getMessage());198}199cal.add(field, +1);200}201}202203/**204* 4928615: GregorianCalendar returns wrong dates after setGregorianChange205*/206public void Test4928615() {207Koyomi cal = new Koyomi();208logln("Today is 2003/10/1 Gregorian.");209@SuppressWarnings("deprecation")210Date x = new Date(2003 - 1900, 10 - 1, 1);211cal.setTime(x);212213logln(" Changing the cutover date to yesterday...");214cal.setGregorianChange(new Date(x.getTime() - (24 * 3600 * 1000)));215if (!cal.checkDate(2003, OCTOBER, 1)) {216errln(" " + cal.getMessage());217}218logln(" Changing the cutover date to tomorrow...");219cal.setGregorianChange(new Date(x.getTime() + (24 * 3600 * 1000)));220if (!cal.checkDate(2003, SEPTEMBER, 18)) {221errln(" " + cal.getMessage());222}223}224225/**226* 4743587: GregorianCalendar.getLeastMaximum() returns wrong values227*/228public void Test4743587() {229Koyomi cal = new Koyomi();230Koyomi cal2 = (Koyomi) cal.clone();231logln("getLeastMaximum should handle cutover year.\n"232+ " default cutover date");233if (!cal.checkLeastMaximum(DAY_OF_YEAR, 365 - 10)) {234errln(" " + cal.getMessage());235}236if (!cal.checkLeastMaximum(WEEK_OF_YEAR, 52 - ((10 + 6) / 7))) {237errln(" " + cal.getMessage());238}239// Corrected for 4956232240if (!cal.checkLeastMaximum(DAY_OF_MONTH, 28)) {241errln(" " + cal.getMessage());242}243if (!cal.checkLeastMaximum(WEEK_OF_MONTH, 3)) {244errln(" " + cal.getMessage());245}246if (!cal.checkLeastMaximum(DAY_OF_WEEK_IN_MONTH, 3)) {247errln(" " + cal.getMessage());248}249// make sure that getLeastMaximum calls didn't affect the date250if (!cal.equals(cal2)) {251errln(" getLeastMaximum calls modified the object.");252}253if (!cal.checkGreatestMinimum(DAY_OF_MONTH, 1)) {254errln(" " + cal.getMessage());255}256257logln(" changing the date to 1582/10/20 for actual min/max tests");258cal.set(1582, OCTOBER, 20);259if (!cal.checkActualMinimum(DAY_OF_MONTH, 1)) {260errln(" " + cal.getMessage());261}262if (!cal.checkActualMaximum(DAY_OF_MONTH, 31)) {263errln(" " + cal.getMessage());264}265266cal = new Koyomi();267logln("Change the cutover date to 1970/1/5.");268@SuppressWarnings("deprecation")269Date d = new Date(1970 - 1900, 0, 5);270cal.setGregorianChange(d);271if (!cal.checkLeastMaximum(DAY_OF_YEAR, 356)) {272errln(" " + cal.getMessage());273}274if (!cal.checkLeastMaximum(DAY_OF_MONTH, 22)) {275errln(" " + cal.getMessage());276}277if (!cal.checkGreatestMinimum(DAY_OF_MONTH, 5)) {278errln(" " + cal.getMessage());279}280cal.set(1970, JANUARY, 10);281if (!cal.checkActualMinimum(DAY_OF_MONTH, 5)) {282errln(" " + cal.getMessage());283}284if (!cal.checkActualMaximum(DAY_OF_MONTH, 31)) {285errln(" " + cal.getMessage());286}287}288289/**290* 6459836: (cal) GregorianCalendar set method provides wrong result291*/292public void Test6459836() {293int hour = 13865672;294Koyomi gc1 = new Koyomi();295gc1.clear();296gc1.set(1, JANUARY, 1, 0, 0, 0);297gc1.set(HOUR_OF_DAY, hour);298if (!gc1.checkDate(1582, OCTOBER, 4)) {299errln("test case 1: " + gc1.getMessage());300}301gc1.clear();302gc1.set(1, JANUARY, 1, 0, 0, 0);303gc1.set(HOUR_OF_DAY, hour + 24);304if (!gc1.checkDate(1582, OCTOBER, 15)) {305errln("test case 2: " + gc1.getMessage());306}307}308309/**310* 6549953 (cal) WEEK_OF_YEAR and DAY_OF_YEAR calculation problems around Gregorian cutover311*/312public void Test6549953() {313Koyomi cal = new Koyomi();314315cal.set(YEAR, 1582);316cal.set(WEEK_OF_YEAR, 42);317cal.set(DAY_OF_WEEK, FRIDAY);318cal.checkFieldValue(WEEK_OF_YEAR, 42);319cal.checkFieldValue(DAY_OF_WEEK, FRIDAY);320if (!cal.checkDate(1582, OCTOBER, 29)) {321errln(cal.getMessage());322}323cal.clear();324cal.set(1582, OCTOBER, 1);325cal.set(DAY_OF_YEAR, 292);326if (!cal.checkDate(1582, OCTOBER, 29)) {327errln(cal.getMessage());328}329}330}331332333