1 /* 2 * Copyright (c) 1997, 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 4143459 27 * @summary test Date 28 * @library /java/text/testlib 29 */ 30 package test.java.util.Date; 31 32 import java.util.*; 33 34 import org.testng.Assert; 35 import org.testng.annotations.Test; 36 37 public class DateTest { 38 39 /** 40 * Verify that the Date(String) constructor works. 41 */ 42 @Test testParseOfGMT()43 public void testParseOfGMT() 44 { 45 Date OUT; 46 47 /* Input values */ 48 String stringVal = "Jan 01 00:00:00 GMT 1900"; 49 long expectedVal = -2208988800000L; 50 51 OUT = new Date( stringVal ); 52 53 Assert.assertEquals(OUT.getTime( ), expectedVal ); 54 } 55 56 // Check out Date's behavior with large negative year values; bug 664 57 // As of the fix to bug 4056585, Date should work correctly with 58 // large negative years. 59 @Test testDateNegativeYears()60 public void testDateNegativeYears() 61 { 62 Date d1= new Date(80,-1,2); 63 d1= new Date(-80,-1,2); 64 try { 65 d1= new Date(-800000,-1,2); 66 } 67 catch (IllegalArgumentException ex) { 68 Assert.fail(); 69 } 70 } 71 72 // Verify the behavior of Date 73 @Test testDate480()74 public void testDate480() 75 { 76 TimeZone save = TimeZone.getDefault(); 77 try { 78 TimeZone.setDefault(TimeZone.getTimeZone("PST")); 79 Date d1=new java.util.Date(97,8,13,10,8,13); 80 Date d2=new java.util.Date(97,8,13,30,8,13); // 20 hours later 81 82 double delta = (d2.getTime() - d1.getTime()) / 3600000; 83 84 85 Assert.assertEquals(delta, 20.0); 86 87 Calendar cal = Calendar.getInstance(); 88 cal.clear(); 89 cal.set(1997,8,13,10,8,13); 90 Date t1 = cal.getTime(); 91 cal.clear(); 92 cal.set(1997,8,13,30,8,13); // 20 hours later 93 Date t2 = cal.getTime(); 94 95 double delta2 = (t2.getTime() - t1.getTime()) / 3600000; 96 97 Assert.assertEquals(delta2, 20.0); 98 } 99 finally { 100 TimeZone.setDefault(save); 101 } 102 } 103 }