• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * @test
19  * @bug 4143459
20  * @summary test Date
21  * @library /java/text/testlib
22  */
23 package test.java.util.Date;
24 
25 import java.util.*;
26 
27 import org.testng.Assert;
28 import org.testng.annotations.Test;
29 
30 public class DateTest {
31 
32     /**
33      * Verify that the Date(String) constructor works.
34      */
35     @Test
testParseOfGMT()36     public void testParseOfGMT()
37     {
38         Date OUT;
39 
40         /* Input values */
41         String stringVal = "Jan 01 00:00:00 GMT 1900";
42         long expectedVal = -2208988800000L;
43 
44         OUT = new Date( stringVal );
45 
46         Assert.assertEquals(OUT.getTime( ), expectedVal );
47     }
48 
49     // Check out Date's behavior with large negative year values; bug 664
50     // As of the fix to bug 4056585, Date should work correctly with
51     // large negative years.
52     @Test
testDateNegativeYears()53     public void testDateNegativeYears()
54     {
55         Date d1= new Date(80,-1,2);
56         d1= new Date(-80,-1,2);
57         try {
58             d1= new Date(-800000,-1,2);
59         }
60         catch (IllegalArgumentException ex) {
61             Assert.fail();
62         }
63     }
64 
65     // Verify the behavior of Date
66     @Test
testDate480()67     public void testDate480()
68     {
69         TimeZone save = TimeZone.getDefault();
70         try {
71             TimeZone.setDefault(TimeZone.getTimeZone("PST"));
72             Date d1=new java.util.Date(97,8,13,10,8,13);
73             Date d2=new java.util.Date(97,8,13,30,8,13); // 20 hours later
74 
75             double delta = (d2.getTime() - d1.getTime()) / 3600000;
76 
77 
78             Assert.assertEquals(delta, 20.0);
79 
80             Calendar cal = Calendar.getInstance();
81             cal.clear();
82             cal.set(1997,8,13,10,8,13);
83             Date t1 = cal.getTime();
84             cal.clear();
85             cal.set(1997,8,13,30,8,13); // 20 hours later
86             Date t2 = cal.getTime();
87 
88             double delta2 = (t2.getTime() - t1.getTime()) / 3600000;
89 
90             Assert.assertEquals(delta2, 20.0);
91         }
92         finally {
93             TimeZone.setDefault(save);
94         }
95     }
96 }