Path: blob/master/test/jdk/java/util/Date/DateRegression.java
41149 views
/*1* Copyright (c) 1998, 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 4023247 4027685 4032037 4072029 4073003 4118010 4120606 4133833 4136916 6274757 631438726* @library /java/text/testlib27*/2829import java.util.*;3031@SuppressWarnings("deprecation")32public class DateRegression extends IntlTest {3334public static void main(String[] args) throws Exception {35new DateRegression().run(args);36}3738/**39* @bug 402324740*/41public void Test4023247() {42Date d1 = new Date(0);43Date d2 = new Date(0);4445d1.setYear(96);46d1.setMonth(11);47d1.setDate(22);48d1.setHours(0);49d1.setMinutes(0);50d1.setSeconds(0);5152d2.setYear(96);53d2.setMonth(11);54d2.setDate(22);55d2.setHours(0);56d2.setMinutes(0);57d2.setSeconds(0);5859if (d1.hashCode() != d2.hashCode())60errln("Fail: Date hashCode misbehaves");61}6263/**64* @bug 402768565*/66public void Test4027685() {67// Should be 01/16/97 00:00:0068Date nite = new Date("16-JAN-97 12:00 AM");69// Should be 01/16/97 12:00:0070Date noon = new Date("16-JAN-97 12:00 PM");7172logln("Midnight = " + nite + ", Noon = " + noon);73if (!nite.equals(new Date(97, Calendar.JANUARY, 16, 0, 0)) ||74!noon.equals(new Date(97, Calendar.JANUARY, 16, 12, 0)))75errln("Fail: Nite/Noon confused");76}7778/**79* @bug 403203780*/81public void Test4032037() {82Date ref = new Date(97, 1, 10);83Date d = new Date(Date.parse("2/10/97"));84logln("Date.parse(2/10/97) => " + d);85if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);86d = new Date(Date.parse("10 feb 1997"));87logln("Date.parse(10 feb 1997) => " + d);88if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);89d = new Date("2/10/97");90logln("Date(2/10/97) => " + d);91if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);92d = new Date("10 feb 1997");93logln("Date(10 feb 1997) => " + d);94if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);95}9697/**98* @bug 407202999*/100public void Test4072029() {101TimeZone saveZone = TimeZone.getDefault();102103try {104TimeZone.setDefault(TimeZone.getTimeZone("PST"));105Date now = new Date();106String s = now.toString();107Date now2 = new Date(now.toString());108String s2 = now2.toString(); // An hour's difference109110if (!s.equals(s2) ||111Math.abs(now.getTime() - now2.getTime()) > 60000 /*one min*/) {112errln("Fail: Roundtrip toString/parse");113}114}115finally {116TimeZone.setDefault(saveZone);117}118}119120/**121* @bug 4073003122*/123public void Test4073003() {124Date d = new Date(Date.parse("01/02/1984"));125if (!d.equals(new Date(84, 0, 2)))126errln("Fail: Want 1/2/1984 Got " + d);127d = new Date(Date.parse("02/03/2012"));128if (!d.equals(new Date(112, 1, 3)))129errln("Fail: Want 2/3/2012 Got " + d);130d = new Date(Date.parse("03/04/15"));131if (!d.equals(new Date(115, 2, 4)))132errln("Fail: Want 3/4/2015 Got " + d);133}134135/**136* @bug 4118010137* Regress bug:138* Feb. 2000 has 29 days, but Date(2000, 1, 29) returns March 01, 2000139* NOTE: This turned out to be a user error (passing in 2000 instead140* of 2000-1900 to the Date constructor).141*/142public void Test4118010() {143Date d=new java.util.Date(2000-1900, Calendar.FEBRUARY, 29);144int m=d.getMonth();145int date=d.getDate();146if (m != Calendar.FEBRUARY ||147date != 29)148errln("Fail: Want Feb 29, got " + d);149}150151/**152* @bug 4120606153* Date objects share state after cloning.154*/155public void Test4120606() {156Date d = new Date(98, Calendar.JUNE, 24);157d.setMonth(Calendar.MAY);158Date e = (Date)d.clone();159d.setMonth(Calendar.FEBRUARY);160if (e.getMonth() != Calendar.MAY) {161errln("Cloned Date objects share state");162}163}164165/**166* @bug 4133833167* Date constructor crashes with parameters out of range, when it should168* normalize.169*/170public void Test4133833() {171Date date = new java.util.Date(12,15,19);172Date exp = new Date(1913-1900, Calendar.APRIL, 19);173if (!date.equals(exp))174errln("Fail: Want " + exp +175"; got " + date);176}177178/**179* @bug 4136916180* Date.toString() throws exception in 1.2b4-E181* CANNOT REPRODUCE this bug182*/183public void Test4136916() {184Date time = new Date();185logln(time.toString());186}187188/**189* @bug 6274757190* Date getTime and toString interaction for some time values191*/192public void Test6274757() {193TimeZone savedTz = TimeZone.getDefault();194try {195// Use a time zone west of GMT.196TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));197TimeZone jdkGMT = TimeZone.getTimeZone("GMT");198Calendar jdkCal = Calendar.getInstance(jdkGMT);199jdkCal.clear();200jdkCal.set(1582, Calendar.OCTOBER, 15);201logln("JDK time: " + jdkCal.getTime().getTime() );202logln("JDK time (str): " + jdkCal.getTime() );203logln("Day of month: " + jdkCal.get(Calendar.DAY_OF_MONTH));204Date co = jdkCal.getTime();205logln("Change over (Oct 15 1582) = " + co + " (" +206co.getTime() + ")");207long a = jdkCal.getTime().getTime();208Date c = jdkCal.getTime();209c.toString();210long b = c.getTime();211212if (a != b) {213errln("ERROR: " + a + " != " + b);214} else {215logln(a + " = " + b);216}217} finally {218TimeZone.setDefault(savedTz);219}220}221222/**223* @bug 6314387224* JCK6.0: api/java_util/Date/index.html#misc fails, mustang225*/226public void Test6314387() {227Date d = new Date(Long.MAX_VALUE);228int y = d.getYear();229if (y != 292277094) {230errln("yesr: got " + y + ", expected 292277094");231}232d = new Date(Long.MIN_VALUE);233y = d.getYear();234if (y != 292267155) {235errln("yesr: got " + y + ", expected 292267155");236}237}238}239240//eof241242243