< prev index next >

test/jdk/java/util/Date/DateRegression.java

Print this page

  1 /*
  2  * Copyright (c) 1998, 2016, 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  * @library /java/text/testlib
 28  */
 29 
 30 import java.util.*;
 31 
 32 @SuppressWarnings("deprecation")
 33 public class DateRegression extends IntlTest {
 34 
 35     public static void main(String[] args) throws Exception {
 36         new DateRegression().run(args);
 37     }

 38 
 39     /**
 40      * @bug 4023247
 41      */

 42     public void Test4023247() {
 43         Date d1 = new Date(0);
 44         Date d2 = new Date(0);
 45 
 46         d1.setYear(96);
 47         d1.setMonth(11);
 48         d1.setDate(22);
 49         d1.setHours(0);
 50         d1.setMinutes(0);
 51         d1.setSeconds(0);
 52 
 53         d2.setYear(96);
 54         d2.setMonth(11);
 55         d2.setDate(22);
 56         d2.setHours(0);
 57         d2.setMinutes(0);
 58         d2.setSeconds(0);
 59 
 60         if (d1.hashCode() != d2.hashCode())
 61             errln("Fail: Date hashCode misbehaves");
 62     }
 63 
 64     /**
 65      * @bug 4027685
 66      */

 67     public void Test4027685() {
 68         // Should be 01/16/97 00:00:00
 69         Date nite = new Date("16-JAN-97 12:00 AM");
 70         // Should be 01/16/97 12:00:00
 71         Date noon = new Date("16-JAN-97 12:00 PM");
 72 
 73         logln("Midnight = " + nite + ", Noon = " + noon);
 74         if (!nite.equals(new Date(97, Calendar.JANUARY, 16, 0, 0)) ||
 75             !noon.equals(new Date(97, Calendar.JANUARY, 16, 12, 0)))
 76             errln("Fail: Nite/Noon confused");
 77     }
 78 
 79     /**
 80      * @bug 4032037
 81      */

 82     public void Test4032037() {
 83         Date ref = new Date(97, 1, 10);
 84         Date d = new Date(Date.parse("2/10/97"));
 85         logln("Date.parse(2/10/97) => " + d);
 86         if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);
 87         d = new Date(Date.parse("10 feb 1997"));
 88         logln("Date.parse(10 feb 1997) => " + d);
 89         if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);
 90         d = new Date("2/10/97");
 91         logln("Date(2/10/97) => " + d);
 92         if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);
 93         d = new Date("10 feb 1997");
 94         logln("Date(10 feb 1997) => " + d);
 95         if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);
 96     }
 97 
 98     /**
 99      * @bug 4072029
100      */

101     public void Test4072029() {
102         TimeZone saveZone = TimeZone.getDefault();
103 
104         try {
105             TimeZone.setDefault(TimeZone.getTimeZone("PST"));
106             Date now = new Date();
107             String s = now.toString();
108             Date now2 = new Date(now.toString());
109             String s2 = now2.toString(); // An hour's difference
110 
111             if (!s.equals(s2) ||
112                 Math.abs(now.getTime() - now2.getTime()) > 60000 /*one min*/) {
113                 errln("Fail: Roundtrip toString/parse");
114             }
115         }
116         finally {
117             TimeZone.setDefault(saveZone);
118         }
119     }
120 
121     /**
122      * @bug 4073003
123      */

124     public void Test4073003() {
125         Date d = new Date(Date.parse("01/02/1984"));
126         if (!d.equals(new Date(84, 0, 2)))
127             errln("Fail: Want 1/2/1984 Got " + d);
128         d = new Date(Date.parse("02/03/2012"));
129         if (!d.equals(new Date(112, 1, 3)))
130             errln("Fail: Want 2/3/2012 Got " + d);
131         d = new Date(Date.parse("03/04/15"));
132         if (!d.equals(new Date(115, 2, 4)))
133             errln("Fail: Want 3/4/2015 Got " + d);
134     }
135 
136     /**
137      * @bug 4118010
138      * Regress bug:
139      * Feb. 2000 has 29 days, but Date(2000, 1, 29) returns March 01, 2000
140      * NOTE: This turned out to be a user error (passing in 2000 instead
141      * of 2000-1900 to the Date constructor).
142      */

143     public void Test4118010() {
144         Date d=new java.util.Date(2000-1900, Calendar.FEBRUARY, 29);
145         int m=d.getMonth();
146         int date=d.getDate();
147         if (m != Calendar.FEBRUARY ||
148             date != 29)
149             errln("Fail: Want Feb 29, got " + d);
150     }
151 
152     /**
153      * @bug 4120606
154      * Date objects share state after cloning.
155      */

156     public void Test4120606() {
157         Date d = new Date(98, Calendar.JUNE, 24);
158         d.setMonth(Calendar.MAY);
159         Date e = (Date)d.clone();
160         d.setMonth(Calendar.FEBRUARY);
161         if (e.getMonth() != Calendar.MAY) {
162             errln("Cloned Date objects share state");
163         }
164     }
165 
166     /**
167      * @bug 4133833
168      * Date constructor crashes with parameters out of range, when it should
169      * normalize.
170      */

171     public void Test4133833() {
172         Date date = new java.util.Date(12,15,19);
173         Date exp  = new Date(1913-1900, Calendar.APRIL, 19);
174         if (!date.equals(exp))
175             errln("Fail: Want " + exp +
176                                 "; got " + date);
177     }
178 
179     /**
180      * @bug 4136916
181      * Date.toString() throws exception in 1.2b4-E
182      * CANNOT REPRODUCE this bug
183      */

184     public void Test4136916() {
185         Date time = new Date();
186         logln(time.toString());
187     }
188 
189     /**
190      * @bug 6274757
191      * Date getTime and toString interaction for some time values
192      */

193     public void Test6274757() {
194         TimeZone savedTz = TimeZone.getDefault();
195         try {
196             // Use a time zone west of GMT.
197             TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
198             TimeZone jdkGMT = TimeZone.getTimeZone("GMT");
199             Calendar jdkCal = Calendar.getInstance(jdkGMT);
200             jdkCal.clear();
201             jdkCal.set(1582, Calendar.OCTOBER, 15);
202             logln("JDK time: " + jdkCal.getTime().getTime() );
203             logln("JDK time (str): " + jdkCal.getTime() );
204             logln("Day of month: " + jdkCal.get(Calendar.DAY_OF_MONTH));
205             Date co = jdkCal.getTime();
206             logln("Change over (Oct 15 1582) = " + co + " (" +
207                   co.getTime() + ")");
208             long a = jdkCal.getTime().getTime();
209             Date c = jdkCal.getTime();
210             c.toString();
211             long b = c.getTime();
212 
213             if (a != b) {
214                 errln("ERROR: " + a + " != " + b);
215             } else {
216                 logln(a + " = " + b);
217             }
218         } finally {
219             TimeZone.setDefault(savedTz);
220         }
221     }
222 
223     /**
224      * @bug 6314387
225      * JCK6.0: api/java_util/Date/index.html#misc fails, mustang
226      */

227     public void Test6314387() {
228         Date d = new Date(Long.MAX_VALUE);
229         int y = d.getYear();
230         if (y != 292277094) {
231             errln("yesr: got " + y + ", expected 292277094");
232         }
233         d = new Date(Long.MIN_VALUE);
234         y = d.getYear();
235         if (y != 292267155) {
236             errln("yesr: got " + y + ", expected 292267155");
237         }
238     }
239 }
240 
241 //eof

  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
< prev index next >