1 /* 2 * Copyright (c) 2013, 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 8007520 27 *@summary Test those bridge methods to/from java.time date/time classes 28 */ 29 30 import java.util.Random; 31 import java.sql.Date; 32 import java.sql.Time; 33 import java.sql.Timestamp; 34 import java.time.Instant; 35 import java.time.LocalDateTime; 36 import java.time.LocalDate; 37 import java.time.LocalTime; 38 import java.time.ZoneId; 39 import java.time.ZoneOffset; 40 import java.time.ZonedDateTime; 41 42 public class JavatimeTest { 43 44 static final int NANOS_PER_SECOND = 1000000000; 45 46 public static void main(String[] args) throws Throwable { 47 int N = 10000; 48 long t1970 = new java.util.Date(70, 0, 01).getTime(); 49 Random r = new Random(); 50 for (int i = 0; i < N; i++) { 51 int days = r.nextInt(50) * 365 + r.nextInt(365); 52 long secs = t1970 + days * 86400 + r.nextInt(86400); 53 int nanos = r.nextInt(NANOS_PER_SECOND); 54 int nanos_ms = nanos / 1000000 * 1000000; // millis precision 55 long millis = secs * 1000 + r.nextInt(1000); 56 57 LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); 58 LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); 59 Instant inst = Instant.ofEpochSecond(secs, nanos); 60 Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); 61 //System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); 62 63 /////////// Timestamp //////////////////////////////// 64 Timestamp ta = new Timestamp(millis); 65 ta.setNanos(nanos); 66 if (!isEqual(ta.toLocalDateTime(), ta)) { 67 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); 68 print(ta.toLocalDateTime(), ta); 69 throw new RuntimeException("FAILED: j.s.ts -> ldt"); 70 } 71 if (!isEqual(ldt, Timestamp.valueOf(ldt))) { 72 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); 73 print(ldt, Timestamp.valueOf(ldt)); 74 throw new RuntimeException("FAILED: ldt -> j.s.ts"); 75 } 76 Instant inst0 = ta.toInstant(); 77 if (ta.getTime() != inst0.toEpochMilli() || 78 ta.getNanos() != inst0.getNano() || 79 !ta.equals(Timestamp.from(inst0))) { 80 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); 81 throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts"); 82 } 83 inst = Instant.ofEpochSecond(secs, nanos); 84 Timestamp ta0 = Timestamp.from(inst); 85 if (ta0.getTime() != inst.toEpochMilli() || 86 ta0.getNanos() != inst.getNano() || 87 !inst.equals(ta0.toInstant())) { 88 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); 89 throw new RuntimeException("FAILED: instant -> timestamp -> instant"); 90 } 91 92 ////////// java.sql.Date ///////////////////////////// 93 // j.s.d/t uses j.u.d.equals() !!!!!!!! 94 java.sql.Date jsd = new java.sql.Date(millis); 95 if (!isEqual(jsd.toLocalDate(), jsd)) { 96 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); 97 print(jsd.toLocalDate(), jsd); 98 throw new RuntimeException("FAILED: j.s.d -> ld"); 99 } 100 LocalDate ld = ldt.toLocalDate(); 101 if (!isEqual(ld, java.sql.Date.valueOf(ld))) { 102 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); 103 print(ld, java.sql.Date.valueOf(ld)); 104 throw new RuntimeException("FAILED: ld -> j.s.d"); 105 } 106 ////////// java.sql.Time ///////////////////////////// 107 java.sql.Time jst = new java.sql.Time(millis); 108 if (!isEqual(jst.toLocalTime(), jst)) { 109 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); 110 print(jst.toLocalTime(), jst); 111 throw new RuntimeException("FAILED: j.s.t -> lt"); 112 } 113 // millis precision 114 LocalTime lt = ldt_ms.toLocalTime(); 115 if (!isEqual(lt, java.sql.Time.valueOf(lt))) { 116 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); 117 print(lt, java.sql.Time.valueOf(lt)); 118 throw new RuntimeException("FAILED: lt -> j.s.t"); 119 } 120 } 121 System.out.println("Passed!"); 122 } 123 124 private static boolean isEqual(LocalDateTime ldt, Timestamp ts) { 125 ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault()); 126 return zdt.getYear() == ts.getYear() + 1900 && 127 zdt.getMonthValue() == ts.getMonth() + 1 && 128 zdt.getDayOfMonth() == ts.getDate() && 129 zdt.getHour() == ts.getHours() && 130 zdt.getMinute() == ts.getMinutes() && 131 zdt.getSecond() == ts.getSeconds() && 132 zdt.getNano() == ts.getNanos(); 133 } 134 135 private static void print(LocalDateTime ldt, Timestamp ts) { 136 ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault()); 137 System.out.printf("ldt:ts %d/%d, %d/%d, %d/%d, %d/%d, %d/%d, %d/%d, nano:[%d/%d]%n", 138 zdt.getYear(), ts.getYear() + 1900, 139 zdt.getMonthValue(), ts.getMonth() + 1, 140 zdt.getDayOfMonth(), ts.getDate(), 141 zdt.getHour(), ts.getHours(), 142 zdt.getMinute(), ts.getMinutes(), 143 zdt.getSecond(), ts.getSeconds(), 144 zdt.getNano(), ts.getNanos()); 145 } 146 147 private static boolean isEqual(LocalDate ld, java.sql.Date d) { 148 return ld.getYear() == d.getYear() + 1900 && 149 ld.getMonthValue() == d.getMonth() + 1 && 150 ld.getDayOfMonth() == d.getDate(); 151 } 152 153 private static void print(LocalDate ld, java.sql.Date d) { 154 System.out.printf("%d/%d, %d/%d, %d/%d%n", 155 ld.getYear(), d.getYear() + 1900, 156 ld.getMonthValue(), d.getMonth() + 1, 157 ld.getDayOfMonth(), d.getDate()); 158 } 159 160 private static boolean isEqual(LocalTime lt, java.sql.Time t) { 161 return lt.getHour() == t.getHours() && 162 lt.getMinute() == t.getMinutes() && 163 lt.getSecond() == t.getSeconds(); 164 } 165 166 private static void print(LocalTime lt, java.sql.Time t) { 167 System.out.printf("%d/%d, %d/%d, %d/%d%n", 168 lt.getHour(), t.getHours(), 169 lt.getMinute(), t.getMinutes(), 170 lt.getSecond(), t.getSeconds()); 171 } 172 }