1 /*
  2  * Copyright (c) 1998, 2023, 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 
 24 /*
 25  * @test
 26  * @bug 4023247 4027685 4032037 4072029 4073003 4118010 4120606 4133833 4136916 6274757 6314387
 27  * @run junit DateRegression
 28  */
 29 
 30 import java.util.*;
 31 
 32 import org.junit.jupiter.api.Test;
 33 
 34 import static org.junit.jupiter.api.Assertions.fail;
 35 
 36 @SuppressWarnings("deprecation")
 37 public class DateRegression {
 38 
 39     /**
 40      * @bug 4023247
 41      */
 42     @Test
 43     public void Test4023247() {
 44         Date d1 = new Date(0);
 45         Date d2 = new Date(0);
 46 
 47         d1.setYear(96);
 48         d1.setMonth(11);
 49         d1.setDate(22);
 50         d1.setHours(0);
 51         d1.setMinutes(0);
 52         d1.setSeconds(0);
 53 
 54         d2.setYear(96);
 55         d2.setMonth(11);
 56         d2.setDate(22);
 57         d2.setHours(0);
 58         d2.setMinutes(0);
 59         d2.setSeconds(0);
 60 
 61         if (d1.hashCode() != d2.hashCode())
 62             fail("Fail: Date hashCode misbehaves");
 63     }
 64 
 65     /**
 66      * @bug 4027685
 67      */
 68     @Test
 69     public void Test4027685() {
 70         // Should be 01/16/97 00:00:00
 71         Date nite = new Date("16-JAN-97 12:00 AM");
 72         // Should be 01/16/97 12:00:00
 73         Date noon = new Date("16-JAN-97 12:00 PM");
 74 
 75         System.out.println("Midnight = " + nite + ", Noon = " + noon);
 76         if (!nite.equals(new Date(97, Calendar.JANUARY, 16, 0, 0)) ||
 77             !noon.equals(new Date(97, Calendar.JANUARY, 16, 12, 0)))
 78             fail("Fail: Nite/Noon confused");
 79     }
 80 
 81     /**
 82      * @bug 4032037
 83      */
 84     @Test
 85     public void Test4032037() {
 86         Date ref = new Date(97, 1, 10);
 87         Date d = new Date(Date.parse("2/10/97"));
 88         System.out.println("Date.parse(2/10/97) => " + d);
 89         if (!d.equals(ref)) fail("Fail: Want " + ref + " Got " + d);
 90         d = new Date(Date.parse("10 feb 1997"));
 91         System.out.println("Date.parse(10 feb 1997) => " + d);
 92         if (!d.equals(ref)) fail("Fail: Want " + ref + " Got " + d);
 93         d = new Date("2/10/97");
 94         System.out.println("Date(2/10/97) => " + d);
 95         if (!d.equals(ref)) fail("Fail: Want " + ref + " Got " + d);
 96         d = new Date("10 feb 1997");
 97         System.out.println("Date(10 feb 1997) => " + d);
 98         if (!d.equals(ref)) fail("Fail: Want " + ref + " Got " + d);
 99     }
100 
101     /**
102      * @bug 4072029
103      */
104     @Test
105     public void Test4072029() {
106         TimeZone saveZone = TimeZone.getDefault();
107 
108         try {
109             TimeZone.setDefault(TimeZone.getTimeZone("PST"));
110             Date now = new Date();
111             String s = now.toString();
112             Date now2 = new Date(now.toString());
113             String s2 = now2.toString(); // An hour's difference
114 
115             if (!s.equals(s2) ||
116                 Math.abs(now.getTime() - now2.getTime()) > 60000 /*one min*/) {
117                 fail("Fail: Roundtrip toString/parse");
118             }
119         }
120         finally {
121             TimeZone.setDefault(saveZone);
122         }
123     }
124 
125     /**
126      * @bug 4073003
127      */
128     @Test
129     public void Test4073003() {
130         Date d = new Date(Date.parse("01/02/1984"));
131         if (!d.equals(new Date(84, 0, 2)))
132             fail("Fail: Want 1/2/1984 Got " + d);
133         d = new Date(Date.parse("02/03/2012"));
134         if (!d.equals(new Date(112, 1, 3)))
135             fail("Fail: Want 2/3/2012 Got " + d);
136         d = new Date(Date.parse("03/04/15"));
137         if (!d.equals(new Date(115, 2, 4)))
138             fail("Fail: Want 3/4/2015 Got " + d);
139     }
140 
141     /**
142      * @bug 4118010
143      * Regress bug:
144      * Feb. 2000 has 29 days, but Date(2000, 1, 29) returns March 01, 2000
145      * NOTE: This turned out to be a user error (passing in 2000 instead
146      * of 2000-1900 to the Date constructor).
147      */
148     @Test
149     public void Test4118010() {
150         Date d=new java.util.Date(2000-1900, Calendar.FEBRUARY, 29);
151         int m=d.getMonth();
152         int date=d.getDate();
153         if (m != Calendar.FEBRUARY ||
154             date != 29)
155             fail("Fail: Want Feb 29, got " + d);
156     }
157 
158     /**
159      * @bug 4120606
160      * Date objects share state after cloning.
161      */
162     @Test
163     public void Test4120606() {
164         Date d = new Date(98, Calendar.JUNE, 24);
165         d.setMonth(Calendar.MAY);
166         Date e = (Date)d.clone();
167         d.setMonth(Calendar.FEBRUARY);
168         if (e.getMonth() != Calendar.MAY) {
169             fail("Cloned Date objects share state");
170         }
171     }
172 
173     /**
174      * @bug 4133833
175      * Date constructor crashes with parameters out of range, when it should
176      * normalize.
177      */
178     @Test
179     public void Test4133833() {
180         Date date = new java.util.Date(12,15,19);
181         Date exp  = new Date(1913-1900, Calendar.APRIL, 19);
182         if (!date.equals(exp))
183             fail("Fail: Want " + exp +
184                                 "; got " + date);
185     }
186 
187     /**
188      * @bug 4136916
189      * Date.toString() throws exception in 1.2b4-E
190      * CANNOT REPRODUCE this bug
191      */
192     @Test
193     public void Test4136916() {
194         Date time = new Date();
195         System.out.println(time.toString());
196     }
197 
198     /**
199      * @bug 6274757
200      * Date getTime and toString interaction for some time values
201      */
202     @Test
203     public void Test6274757() {
204         TimeZone savedTz = TimeZone.getDefault();
205         try {
206             // Use a time zone west of GMT.
207             TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
208             TimeZone jdkGMT = TimeZone.getTimeZone("GMT");
209             Calendar jdkCal = Calendar.getInstance(jdkGMT);
210             jdkCal.clear();
211             jdkCal.set(1582, Calendar.OCTOBER, 15);
212             System.out.println("JDK time: " + jdkCal.getTime().getTime() );
213             System.out.println("JDK time (str): " + jdkCal.getTime() );
214             System.out.println("Day of month: " + jdkCal.get(Calendar.DAY_OF_MONTH));
215             Date co = jdkCal.getTime();
216             System.out.println("Change over (Oct 15 1582) = " + co + " (" +
217                   co.getTime() + ")");
218             long a = jdkCal.getTime().getTime();
219             Date c = jdkCal.getTime();
220             c.toString();
221             long b = c.getTime();
222 
223             if (a != b) {
224                 fail("ERROR: " + a + " != " + b);
225             } else {
226                 System.out.println(a + " = " + b);
227             }
228         } finally {
229             TimeZone.setDefault(savedTz);
230         }
231     }
232 
233     /**
234      * @bug 6314387
235      * JCK6.0: api/java_util/Date/index.html#misc fails, mustang
236      */
237     @Test
238     public void Test6314387() {
239         Date d = new Date(Long.MAX_VALUE);
240         int y = d.getYear();
241         if (y != 292277094) {
242             fail("yesr: got " + y + ", expected 292277094");
243         }
244         d = new Date(Long.MIN_VALUE);
245         y = d.getYear();
246         if (y != 292267155) {
247             fail("yesr: got " + y + ", expected 292267155");
248         }
249     }
250 }
251 
252 //eof