Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Date/Bug4955000.java
38812 views
/*1* Copyright (c) 2005, 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 495500026* @summary Make sure that a Date and a GregorianCalendar produce the27* same date/time. Both are new implementations in 1.5.28*/2930import java.util.*;31import static java.util.GregorianCalendar.*;3233@SuppressWarnings("deprecation")34public class Bug4955000 {35// Tests for Date.UTC(), derived from JCK36// Date.miscTests.Date1025 and Date201537public static void main(String[] args) {38TimeZone defaultTZ = TimeZone.getDefault();39try {40TimeZone.setDefault(TimeZone.getTimeZone("NST"));41GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("UTC"));42// Date102543int[] years1 = {44Integer.MIN_VALUE,45Integer.MIN_VALUE + 1,46gc.getMinimum(YEAR) - 1,47gc.getMaximum(YEAR) + 1,48Integer.MAX_VALUE - 1,49Integer.MAX_VALUE50};51for (int i = 0; i < years1.length; i++) {52gc.clear();53gc.set(years1[i], gc.JANUARY, 1);54long t = gc.getTimeInMillis();55long utc = Date.UTC(years1[i] - 1900, 1-1, 1,560, 0, 0); // Jan 1 00:00:0057if (t != utc) {58throw new RuntimeException("t (" + t + ") != utc (" + utc +")");59}60}6162// Date201563int years[] = {64gc.getGreatestMinimum(YEAR),65gc.getGreatestMinimum(YEAR) + 1,66-1,670,681,69gc.getLeastMaximum(YEAR) - 1,70gc.getLeastMaximum(YEAR)71};7273int months[] = {74gc.getMinimum(MONTH),75gc.getMinimum(MONTH) + 1,76gc.getMaximum(MONTH) - 1,77gc.getMaximum(MONTH)78};7980int dates[] = {81gc.getMinimum(DAY_OF_MONTH),82gc.getMinimum(DAY_OF_MONTH) + 1,83gc.getMaximum(DAY_OF_MONTH) - 1,84gc.getMaximum(DAY_OF_MONTH)85};8687int hs[] = {88gc.getMinimum(HOUR),89gc.getMinimum(HOUR) + 1,90gc.getMaximum(HOUR) - 1,91gc.getMaximum(HOUR)92};9394int ms[] = {95gc.getMinimum(MINUTE),96gc.getMinimum(MINUTE) + 1,97gc.getMaximum(MINUTE) - 1,98gc.getMaximum(MINUTE)99};100101int ss[] = {102gc.getMinimum(SECOND),103gc.getMinimum(SECOND) + 1,104gc.getMaximum(SECOND) - 1,105gc.getMaximum(SECOND)106};107108for(int i = 0; i < years.length; i++) {109for(int j = 0; j < months.length; j++) {110for(int k = 0; k < dates.length; k++) {111for(int m = 0; m < hs.length; m++) {112for(int n = 0; n < ms.length; n++) {113for(int p = 0; p < ss.length; p++) {114int year = years[i] - 1900;115int month = months[j];116int date = dates[k];117int hours = hs[m];118int minutes = ms[n];119int seconds = ss[p];120121long result = Date.UTC(year, month, date,122hours, minutes, seconds);123124gc.clear();125gc.set(year + 1900, month, date, hours, minutes, seconds);126127long expected = gc.getTime().getTime();128129if (expected != result) {130throw new RuntimeException("expected (" + expected131+ ") != result (" + result +")");132}133}134}135}136}137}138}139} finally {140TimeZone.setDefault(defaultTZ);141}142}143}144145146