Path: blob/master/test/jdk/java/sql/JavatimeTest.java
41145 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*@test25*@bug 800752026*@summary Test those bridge methods to/from java.time date/time classes27* @key randomness28*/2930import java.util.Random;31import java.sql.Date;32import java.sql.Time;33import java.sql.Timestamp;34import java.time.Instant;35import java.time.LocalDateTime;36import java.time.LocalDate;37import java.time.LocalTime;38import java.time.ZoneId;39import java.time.ZoneOffset;40import java.time.ZonedDateTime;4142public class JavatimeTest {4344static final int NANOS_PER_SECOND = 1000000000;4546public static void main(String[] args) throws Throwable {47int N = 10000;48long t1970 = new java.util.Date(70, 0, 01).getTime();49Random r = new Random();50for (int i = 0; i < N; i++) {51int days = r.nextInt(50) * 365 + r.nextInt(365);52long secs = t1970 + days * 86400 + r.nextInt(86400);53int nanos = r.nextInt(NANOS_PER_SECOND);54int nanos_ms = nanos / 1000000 * 1000000; // millis precision55long millis = secs * 1000 + r.nextInt(1000);5657LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);58LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);59Instant inst = Instant.ofEpochSecond(secs, nanos);60Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);61//System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);6263/////////// Timestamp ////////////////////////////////64Timestamp ta = new Timestamp(millis);65ta.setNanos(nanos);66if (!isEqual(ta.toLocalDateTime(), ta)) {67System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);68print(ta.toLocalDateTime(), ta);69throw new RuntimeException("FAILED: j.s.ts -> ldt");70}71if (!isEqual(ldt, Timestamp.valueOf(ldt))) {72System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);73print(ldt, Timestamp.valueOf(ldt));74throw new RuntimeException("FAILED: ldt -> j.s.ts");75}76Instant inst0 = ta.toInstant();77if (ta.getTime() != inst0.toEpochMilli() ||78ta.getNanos() != inst0.getNano() ||79!ta.equals(Timestamp.from(inst0))) {80System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);81throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts");82}83inst = Instant.ofEpochSecond(secs, nanos);84Timestamp ta0 = Timestamp.from(inst);85if (ta0.getTime() != inst.toEpochMilli() ||86ta0.getNanos() != inst.getNano() ||87!inst.equals(ta0.toInstant())) {88System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);89throw new RuntimeException("FAILED: instant -> timestamp -> instant");90}9192////////// java.sql.Date /////////////////////////////93// j.s.d/t uses j.u.d.equals() !!!!!!!!94java.sql.Date jsd = new java.sql.Date(millis);95if (!isEqual(jsd.toLocalDate(), jsd)) {96System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);97print(jsd.toLocalDate(), jsd);98throw new RuntimeException("FAILED: j.s.d -> ld");99}100LocalDate ld = ldt.toLocalDate();101if (!isEqual(ld, java.sql.Date.valueOf(ld))) {102System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);103print(ld, java.sql.Date.valueOf(ld));104throw new RuntimeException("FAILED: ld -> j.s.d");105}106////////// java.sql.Time /////////////////////////////107java.sql.Time jst = new java.sql.Time(millis);108if (!isEqual(jst.toLocalTime(), jst)) {109System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);110print(jst.toLocalTime(), jst);111throw new RuntimeException("FAILED: j.s.t -> lt");112}113// millis precision114LocalTime lt = ldt_ms.toLocalTime();115if (!isEqual(lt, java.sql.Time.valueOf(lt))) {116System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);117print(lt, java.sql.Time.valueOf(lt));118throw new RuntimeException("FAILED: lt -> j.s.t");119}120}121System.out.println("Passed!");122}123124private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {125ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());126return zdt.getYear() == ts.getYear() + 1900 &&127zdt.getMonthValue() == ts.getMonth() + 1 &&128zdt.getDayOfMonth() == ts.getDate() &&129zdt.getHour() == ts.getHours() &&130zdt.getMinute() == ts.getMinutes() &&131zdt.getSecond() == ts.getSeconds() &&132zdt.getNano() == ts.getNanos();133}134135private static void print(LocalDateTime ldt, Timestamp ts) {136ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());137System.out.printf("ldt:ts %d/%d, %d/%d, %d/%d, %d/%d, %d/%d, %d/%d, nano:[%d/%d]%n",138zdt.getYear(), ts.getYear() + 1900,139zdt.getMonthValue(), ts.getMonth() + 1,140zdt.getDayOfMonth(), ts.getDate(),141zdt.getHour(), ts.getHours(),142zdt.getMinute(), ts.getMinutes(),143zdt.getSecond(), ts.getSeconds(),144zdt.getNano(), ts.getNanos());145}146147private static boolean isEqual(LocalDate ld, java.sql.Date d) {148return ld.getYear() == d.getYear() + 1900 &&149ld.getMonthValue() == d.getMonth() + 1 &&150ld.getDayOfMonth() == d.getDate();151}152153private static void print(LocalDate ld, java.sql.Date d) {154System.out.printf("%d/%d, %d/%d, %d/%d%n",155ld.getYear(), d.getYear() + 1900,156ld.getMonthValue(), d.getMonth() + 1,157ld.getDayOfMonth(), d.getDate());158}159160private static boolean isEqual(LocalTime lt, java.sql.Time t) {161return lt.getHour() == t.getHours() &&162lt.getMinute() == t.getMinutes() &&163lt.getSecond() == t.getSeconds();164}165166private static void print(LocalTime lt, java.sql.Time t) {167System.out.printf("%d/%d, %d/%d, %d/%d%n",168lt.getHour(), t.getHours(),169lt.getMinute(), t.getMinutes(),170lt.getSecond(), t.getSeconds());171}172}173174175