Path: blob/master/test/jdk/java/util/Calendar/JavatimeTest.java
41149 views
/*1* Copyright (c) 2013, 2016, 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 8007520 800825426*@summary Test those bridge methods to/from java.time date/time classes27* @key randomness28*/2930import java.time.Instant;31import java.time.LocalDateTime;32import java.time.ZonedDateTime;33import java.time.ZoneId;34import java.time.ZoneOffset;35import java.util.Calendar;36import java.util.Date;37import java.util.GregorianCalendar;38import java.util.Random;39import java.util.TimeZone;4041public class JavatimeTest {4243static final int NANOS_PER_SECOND = 1000_000_000;4445public static void main(String[] args) throws Throwable {4647int N = 10000;48@SuppressWarnings("deprecation")49long t1970 = new java.util.Date(70, 0, 01).getTime();50Random r = new Random();51for (int i = 0; i < N; i++) {52int days = r.nextInt(50) * 365 + r.nextInt(365);53long secs = t1970 + days * 86400 + r.nextInt(86400);54int nanos = r.nextInt(NANOS_PER_SECOND);55int nanos_ms = nanos / 1000000 * 1000000; // millis precision56long millis = secs * 1000 + r.nextInt(1000);57LocalDateTime 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///////////// java.util.Date /////////////////////////62Date jud = new java.util.Date(millis);63Instant inst0 = jud.toInstant();64if (jud.getTime() != inst0.toEpochMilli()65|| !jud.equals(Date.from(inst0))) {66System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);67throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");68}69// roundtrip only with millis precision70Date jud0 = Date.from(inst_ms);71if (jud0.getTime() != inst_ms.toEpochMilli()72|| !inst_ms.equals(jud0.toInstant())) {73System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);74throw new RuntimeException("FAILED: instant -> j.u.d -> instant");75}76//////////// java.util.GregorianCalendar /////////////77GregorianCalendar cal = new GregorianCalendar();78// non-roundtrip of tz name between j.u.tz and j.t.zid79cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));80cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));81cal.setFirstDayOfWeek(Calendar.MONDAY);82cal.setMinimalDaysInFirstWeek(4);83cal.setTimeInMillis(millis);84ZonedDateTime zdt0 = cal.toZonedDateTime();85if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli()86|| !cal.equals(GregorianCalendar.from(zdt0))) {87System.out.println("cal:" + cal);88System.out.println("zdt:" + zdt0);89System.out.println("calNew:" + GregorianCalendar.from(zdt0));90System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);91throw new RuntimeException("FAILED: gcal -> zdt -> gcal");92}93inst0 = cal.toInstant();94if (cal.getTimeInMillis() != inst0.toEpochMilli()) {95System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);96throw new RuntimeException("FAILED: gcal -> zdt");97}98ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());99GregorianCalendar cal0 = GregorianCalendar.from(zdt);100if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis()101|| !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {102System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);103throw new RuntimeException("FAILED: zdt -> gcal -> zdt");104}105}106107///////////// java.util.TimeZone /////////////////////////108for (String zidStr : TimeZone.getAvailableIDs()) {109// TBD: tzdt intergration110if (zidStr.startsWith("SystemV")111|| zidStr.contains("Riyadh8")112|| zidStr.equals("US/Pacific-New")113|| zidStr.equals("EST")114|| zidStr.equals("HST")115|| zidStr.equals("MST")) {116continue;117}118ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);119if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {120throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);121}122TimeZone tz = TimeZone.getTimeZone(zidStr);123// no round-trip for alias and "GMT"124if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId()))125&& !ZoneId.SHORT_IDS.containsKey(zidStr)126&& !zidStr.startsWith("GMT")) {127throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);128}129}130System.out.println("Passed!");131}132}133134135