Path: blob/master/test/jdk/java/util/Calendar/Limit.java
41149 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*/2223import java.util.*;24import java.text.*;2526/**27* Test GregorianCalendar limits, which should not exist.28* @test29* @bug 405658530* @summary Make sure that GregorianCalendar works far in the past and future.31* @author Alan Liu32*/33public class Limit {34static final long ONE_DAY = 24*60*60*1000L;3536public static void main(String args[]) throws Exception {37GregorianCalendar c = new GregorianCalendar();38DateFormat fmt = new SimpleDateFormat("EEEE, MMMM dd, yyyy G", Locale.US);39long bigMillis = 300000000000000L;4041try {42// We check two things:43// 1. That handling millis in the range of +/- bigMillis works.44// bigMillis is a value that used to blow up.45// 2. The round-trip format/parse works in these extreme areas.46c.setTime(new Date(-bigMillis));47String s = fmt.format(c.getTime());48Date d = fmt.parse(s);49if (Math.abs(d.getTime() + bigMillis) >= ONE_DAY) {50throw new Exception(s + " != " + fmt.format(d));51}5253c.setTime(new Date(+bigMillis));54s = fmt.format(c.getTime());55d = fmt.parse(s);56if (Math.abs(d.getTime() - bigMillis) >= ONE_DAY) {57throw new Exception(s + " != " + fmt.format(d));58}59} catch (IllegalArgumentException | ParseException e) {60throw e;61}62}63}646566