Path: blob/master/test/jdk/java/util/Date/Bug4955000.java
41152 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.Date;31import java.util.GregorianCalendar;32import java.util.TimeZone;3334import static java.util.GregorianCalendar.*;3536@SuppressWarnings("deprecation")37public class Bug4955000 {3839// Tests for Date.UTC(), derived from JCK40// Date.miscTests.Date1025 and Date201541public static void main(String[] args) {42TimeZone defaultTZ = TimeZone.getDefault();43try {44TimeZone.setDefault(TimeZone.getTimeZone("NST"));45GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("UTC"));46// Date102547int[] years1 = {48Integer.MIN_VALUE,49Integer.MIN_VALUE + 1,50gc.getMinimum(YEAR) - 1,51gc.getMaximum(YEAR) + 1,52Integer.MAX_VALUE - 1,53Integer.MAX_VALUE54};55for (int i = 0; i < years1.length; i++) {56gc.clear();57gc.set(years1[i], JANUARY, 1);58long t = gc.getTimeInMillis();59long utc = Date.UTC(years1[i] - 1900, 1 - 1, 1,600, 0, 0); // Jan 1 00:00:0061if (t != utc) {62throw new RuntimeException("t (" + t + ") != utc (" + utc + ")");63}64}6566// Date201567int[] years = {68gc.getGreatestMinimum(YEAR),69gc.getGreatestMinimum(YEAR) + 1,70-1,710,721,73gc.getLeastMaximum(YEAR) - 1,74gc.getLeastMaximum(YEAR)75};7677int[] months = {78gc.getMinimum(MONTH),79gc.getMinimum(MONTH) + 1,80gc.getMaximum(MONTH) - 1,81gc.getMaximum(MONTH)82};8384int[] dates = {85gc.getMinimum(DAY_OF_MONTH),86gc.getMinimum(DAY_OF_MONTH) + 1,87gc.getMaximum(DAY_OF_MONTH) - 1,88gc.getMaximum(DAY_OF_MONTH)89};9091int[] hs = {92gc.getMinimum(HOUR),93gc.getMinimum(HOUR) + 1,94gc.getMaximum(HOUR) - 1,95gc.getMaximum(HOUR)96};9798int[] ms = {99gc.getMinimum(MINUTE),100gc.getMinimum(MINUTE) + 1,101gc.getMaximum(MINUTE) - 1,102gc.getMaximum(MINUTE)103};104105int[] ss = {106gc.getMinimum(SECOND),107gc.getMinimum(SECOND) + 1,108gc.getMaximum(SECOND) - 1,109gc.getMaximum(SECOND)110};111112for (int i = 0; i < years.length; i++) {113for (int j = 0; j < months.length; j++) {114for (int k = 0; k < dates.length; k++) {115for (int m = 0; m < hs.length; m++) {116for (int n = 0; n < ms.length; n++) {117for (int p = 0; p < ss.length; p++) {118int year = years[i] - 1900;119int month = months[j];120int date = dates[k];121int hours = hs[m];122int minutes = ms[n];123int seconds = ss[p];124125long result = Date.UTC(year, month, date,126hours, minutes, seconds);127128gc.clear();129gc.set(year + 1900, month, date, hours, minutes, seconds);130131long expected = gc.getTime().getTime();132133if (expected != result) {134throw new RuntimeException("expected (" + expected135+ ") != result (" + result + ")");136}137}138}139}140}141}142}143} finally {144TimeZone.setDefault(defaultTZ);145}146}147}148149150