Path: blob/master/test/jdk/java/util/Date/DateTest.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*/2223/*24* @test25* @bug 414345926* @summary test Date27* @library /java/text/testlib28*/2930import java.text.*;31import java.util.*;3233@SuppressWarnings("deprecation")34public class DateTest extends IntlTest35{36public static void main(String[] args) throws Exception {37new DateTest().run(args);38}3940/**41* @bug 414345942* Warning: Use TestDefaultZone() for complete testing of this bug.43*/44public void TestDefaultZoneLite() {45// Note: This test is redundant with TestDefaultZone(). It was added by46// request to provide a short&sweet test for this bug. It does not test47// all cases though, so IF THIS TEST PASSES, THE BUG MAY STILL BE48// PRESENT. Use TestDefaultZone() to be sure.49TimeZone save = TimeZone.getDefault();50try {51TimeZone.setDefault(TimeZone.getTimeZone("GMT"));52Date d = new Date();53d.setYear(98);54d.setMonth(Calendar.JANUARY);55d.setDate(1);56d.setHours(6);57TimeZone.setDefault(TimeZone.getTimeZone("PST"));58if (d.getHours() != 22) {59errln("Fail: Date.setHours()/getHours() ignoring default zone");60}61}62finally { TimeZone.setDefault(save); }63}6465/**66* @bug 414345967*/68public void TestDefaultZone() {69// Various problems can creep up, with the current implementation of Date,70// when the default zone is changed.7172TimeZone saveZone = TimeZone.getDefault();73try {7475Date d = new Date(); // Trigger static init76Date ref = new Date(883634400000L); // This is Thu Jan 1 1998 6:00 am GMT77String refstr = "Jan 1 1998 6:00";78TimeZone GMT = TimeZone.getTimeZone("GMT");79TimeZone PST = TimeZone.getTimeZone("PST");8081String[] names = { "year", "month", "date", "day of week", "hour", "offset" };82int[] GMT_EXP = { 98, Calendar.JANUARY, 1, Calendar.THURSDAY - Calendar.SUNDAY, 6, 0 };83int[] PST_EXP = { 97, Calendar.DECEMBER, 31, Calendar.WEDNESDAY - Calendar.SUNDAY, 22, 480 };8485// There are two cases to consider: a Date object with no Calendar86// sub-object (most Date objects), and a Date object with a Calendar87// sub-object. We make two passes to cover the two cases.88for (int pass=0; pass<2; ++pass) {89logln(pass == 0 ? "Normal Date object" : "Date with Calendar sub-object");9091TimeZone.setDefault(GMT);92d = new Date(refstr);93if (pass == 1) {94// Force creation of Calendar sub-object95d.setYear(d.getYear());96}97if (d.getTime() != ref.getTime()) {98errln("FAIL: new Date(\"" + refstr + "\") x GMT -> " + d +99" " + d.getTime() + " ms");100}101102int[] fields = { d.getYear(), d.getMonth(), d.getDate(),103d.getDay(), d.getHours(), d.getTimezoneOffset() };104for (int i=0; i<fields.length; ++i) {105if (fields[i] != GMT_EXP[i]) {106errln("FAIL: GMT Expected " + names[i] + " of " + GMT_EXP[i] +107", got " + fields[i]);108}109}110111TimeZone.setDefault(PST);112int[] fields2 = { d.getYear(), d.getMonth(), d.getDate(),113d.getDay(), d.getHours(), d.getTimezoneOffset() };114for (int i=0; i<fields2.length; ++i) {115if (fields2[i] != PST_EXP[i]) {116errln("FAIL: PST Expected " + names[i] + " of " + PST_EXP[i] +117", got " + fields2[i]);118}119}120}121}122finally {123TimeZone.setDefault(saveZone);124}125}126127// Test the performance of Date128public void TestPerformance592()129{130int REPS = 500;131132// Do timing test with Date133long start = new Date().getTime();134for (int i=0; i<REPS; ++i)135{136Date d = new Date();137int y = d.getYear();138}139long ms = new Date().getTime() - start;140141double perLoop = ((double)ms) / REPS;142logln(REPS + " iterations at " + perLoop + " ms/loop");143if (perLoop > PER_LOOP_LIMIT)144logln("WARNING: Date constructor/getYear slower than " +145PER_LOOP_LIMIT + " ms");146}147static double PER_LOOP_LIMIT = 3.0;148149/**150* Verify that the Date(String) constructor works.151*/152public void TestParseOfGMT()153{154Date OUT = null;155156/* Input values */157String stringVal = "Jan 01 00:00:00 GMT 1900";158long expectedVal = -2208988800000L;159160OUT = new Date( stringVal );161162if( OUT.getTime( ) == expectedVal ) {163// logln("PASS");164}165else {166errln( "Expected: " +167new Date( expectedVal ) +168": " +169expectedVal +170" Received: " +171OUT.toString() +172": " +173OUT.getTime() );174}175}176177// Check out Date's behavior with large negative year values; bug 664178// As of the fix to bug 4056585, Date should work correctly with179// large negative years.180public void TestDateNegativeYears()181{182Date d1= new Date(80,-1,2);183logln(d1.toString());184d1= new Date(-80,-1,2);185logln(d1.toString());186boolean e = false;187try {188d1= new Date(-800000,-1,2);189logln(d1.toString());190}191catch (IllegalArgumentException ex) {192e = true;193}194if (e) errln("FAIL: Saw exception for year -800000");195else logln("Pass: No exception for year -800000");196}197198// Verify the behavior of Date199public void TestDate480()200{201TimeZone save = TimeZone.getDefault();202try {203TimeZone.setDefault(TimeZone.getTimeZone("PST"));204Date d1=new java.util.Date(97,8,13,10,8,13);205logln("d = "+d1);206Date d2=new java.util.Date(97,8,13,30,8,13); // 20 hours later207logln("d+20h = "+d2);208209double delta = (d2.getTime() - d1.getTime()) / 3600000;210211logln("delta = " + delta + "h");212213if (delta != 20.0) errln("Expected delta of 20; got " + delta);214215Calendar cal = Calendar.getInstance();216cal.clear();217cal.set(1997,8,13,10,8,13);218Date t1 = cal.getTime();219logln("d = "+t1);220cal.clear();221cal.set(1997,8,13,30,8,13); // 20 hours later222Date t2 = cal.getTime();223logln("d+20h = "+t2);224225double delta2 = (t2.getTime() - t1.getTime()) / 3600000;226227logln("delta = " + delta2 + "h");228229if (delta != 20.0) errln("Expected delta of 20; got " + delta2);230}231finally {232TimeZone.setDefault(save);233}234}235}236237238