Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Calendar/CalendarLimitTest.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 403366226* @library /java/text/testlib27* @summary test for limit on Calendar28* @run main CalendarLimitTest -verbose29*/3031import java.util.*;32import java.text.*;3334/**35* This test verifies the behavior of Calendar around the very earliest limits36* which it can handle. It also verifies the behavior for large values of millis.37*38* Note: There used to be a limit, due to a bug, for early times. There is39* currently no limit.40*41* March 17, 1998: Added code to make sure big + dates are big + AD years, and42* big - dates are big + BC years.43*/44public class CalendarLimitTest extends IntlTest45{46// This number determined empirically; this is the old limit,47// which we test for to make sure it isn't there anymore.48static final long EARLIEST_SUPPORTED_MILLIS = -210993120000000L;4950static final int EPOCH_JULIAN_DAY = 2440588; // Jaunary 1, 1970 (Gregorian)51static final int JAN_1_1_JULIAN_DAY = 1721426; // January 1, year 1 (Gregorian)5253// Useful millisecond constants54static final int ONE_SECOND = 1000;55static final int ONE_MINUTE = 60*ONE_SECOND;56static final int ONE_HOUR = 60*ONE_MINUTE;57static final int ONE_DAY = 24*ONE_HOUR;58static final int ONE_WEEK = 7*ONE_DAY;59static final long ONE_YEAR = (long)(365.2425 * ONE_DAY);6061static long ORIGIN; // This is the *approximate* point at which BC switches to AD6263public static void main(String argv[]) throws Exception {64new CalendarLimitTest().run(argv);65}6667/**68* Converts Julian day to time as milliseconds.69* @param julian the given Julian day number.70* @return time as milliseconds.71*/72private static final long julianDayToMillis(long julian) {73return (julian - EPOCH_JULIAN_DAY) * ONE_DAY;74}7576/**77* Verify that the given time is processed without problem.78* @return the adjust year, with 0 = 1 BC, -1 = 2 BC, etc.79*/80int test(long millis, Calendar cal, DateFormat fmt)81{82Exception exception = null;83String theDate = "";84try {85Date d= new Date(millis);86cal.setTime(d);87theDate = fmt.format(d);88}89catch (IllegalArgumentException e) {90exception = e;91}92String s = "0x" + Long.toHexString(millis) + " " + theDate;9394int era=cal.get(Calendar.ERA), year=cal.get(Calendar.YEAR),95dom=cal.get(Calendar.DATE), mon=cal.get(Calendar.MONTH);9697cal.clear();98cal.set(year, mon, dom);99cal.set(Calendar.ERA, era);100Date rt = cal.getTime();101102boolean ok = true;103if (exception != null) {104errln("FAIL: Exception " + s);105ok = false;106}107if (((millis >= ORIGIN) && (era != GregorianCalendar.AD)) ||108((millis < ORIGIN) && (era != GregorianCalendar.BC)) ||109(year < 1)) {110errln("FAIL: Bad year/era " + s);111ok = false;112}113if (dom<1 || dom>31) {114errln("FAIL: Bad DOM " + s);115ok = false;116}117if (Math.abs(millis - rt.getTime()) > ONE_DAY) {118errln("FAIL: RT fail " + s + " -> 0x" +119Long.toHexString(rt.getTime()) + " " +120fmt.format(rt));121ok = false;122}123if (ok) logln(s);124if (era==GregorianCalendar.BC) year = 1-year;125return year;126}127128public void TestCalendarLimit()129{130ORIGIN = julianDayToMillis(JAN_1_1_JULIAN_DAY);131132Calendar cal = Calendar.getInstance();133// You must set the time zone to GMT+0 or the edge cases like134// Long.MIN_VALUE, Long.MAX_VALUE, and right around the threshold135// won't work, since before converting to fields the calendar code136// will add the offset for the zone.137cal.setTimeZone(TimeZone.getTimeZone("Africa/Casablanca"));138139DateFormat dateFormat = DateFormat.getDateInstance();140dateFormat.setCalendar(cal); // Make sure you do this -- same reason as above141((SimpleDateFormat)dateFormat).applyPattern("MMM d, yyyy G");142143// Don't expect any failure for positive longs144int lastYear=0;145boolean first=true;146for (long m = Long.MAX_VALUE; m > 0; m >>= 1)147{148int y = test(m, cal, dateFormat);149if (!first && y > lastYear)150errln("FAIL: Years should be decreasing " + lastYear + " " + y);151first = false;152lastYear = y;153}154155// Expect failures for negative millis below threshold156first = true;157for (long m = Long.MIN_VALUE; m < 0; m /= 2) // Don't use m >>= 1158{159int y = test(m, cal, dateFormat);160if (!first && y < lastYear)161errln("FAIL: Years should be increasing " + lastYear + " " + y);162first = false;163lastYear = y;164}165166// Test right around the threshold167test(EARLIEST_SUPPORTED_MILLIS, cal, dateFormat);168test(EARLIEST_SUPPORTED_MILLIS-1, cal, dateFormat);169170// Test a date that should work171test(Long.MIN_VALUE + ONE_DAY, cal, dateFormat);172173// Try hours in the earliest day or two174// JUST FOR DEBUGGING:175if (false) {176((SimpleDateFormat)dateFormat).applyPattern("H:mm MMM d, yyyy G");177for (int dom=2; dom<=3; ++dom) {178for (int h=0; h<24; ++h) {179cal.clear();180cal.set(Calendar.ERA, GregorianCalendar.BC);181cal.set(292269055, Calendar.DECEMBER, dom, h, 0);182Date d = cal.getTime();183cal.setTime(d);184logln("" + h + ":00 Dec "+dom+", 292269055 BC -> " +185Long.toHexString(d.getTime()) + " -> " +186dateFormat.format(cal.getTime()));187}188}189// Other way190long t = 0x80000000018c5c00L; // Dec 3, 292269055 BC191while (t<0) {192cal.setTime(new Date(t));193logln("0x" + Long.toHexString(t) + " -> " +194dateFormat.format(cal.getTime()));195t -= ONE_HOUR;196}197}198}199}200201//eof202203204