1/*
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.util.*;
25import java.text.*;
26
27/**
28 * Test GregorianCalendar limits, which should not exist.
29 * @test
30 * @bug 4056585
31 * @summary Make sure that GregorianCalendar works far in the past and future.
32 * @author Alan Liu
33 */
34public class Limit {
35    static final long ONE_DAY = 24*60*60*1000L;
36
37    public static void main(String args[]) throws Exception {
38        GregorianCalendar c = new GregorianCalendar();
39        DateFormat fmt = new SimpleDateFormat("EEEE, MMMM dd, yyyy G", Locale.US);
40        long bigMillis = 300000000000000L;
41
42        try {
43            // We check two things:
44            // 1. That handling millis in the range of +/- bigMillis works.
45            //    bigMillis is a value that used to blow up.
46            // 2. The round-trip format/parse works in these extreme areas.
47            c.setTime(new Date(-bigMillis));
48            String s = fmt.format(c.getTime());
49            Date d = fmt.parse(s);
50            if (Math.abs(d.getTime() + bigMillis) >= ONE_DAY) {
51                throw new Exception(s + " != " + fmt.format(d));
52            }
53
54            c.setTime(new Date(+bigMillis));
55            s = fmt.format(c.getTime());
56            d = fmt.parse(s);
57            if (Math.abs(d.getTime() - bigMillis) >= ONE_DAY) {
58                throw new Exception(s + " != " + fmt.format(d));
59            }
60        } catch (IllegalArgumentException | ParseException e) {
61            throw e;
62        }
63    }
64}
65