Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Calendar/JavatimeTest.java
46977 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 8007520 800825426*@summary Test those bridge methods to/from java.time date/time classes27* @key randomness28*/2930import java.util.Calendar;31import java.util.Date;32import java.util.GregorianCalendar;33import java.util.Random;34import java.util.TimeZone;35import java.time.Instant;36import java.time.LocalDateTime;37import java.time.ZonedDateTime;38import java.time.ZoneId;39import java.time.ZoneOffset;4041public class JavatimeTest {4243static final int NANOS_PER_SECOND = 1000_000_000;4445public static void main(String[] args) throws Throwable {4647int 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);56LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);57LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);58Instant inst = Instant.ofEpochSecond(secs, nanos);59Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);60///////////// java.util.Date /////////////////////////61Date jud = new java.util.Date(millis);62Instant inst0 = jud.toInstant();63if (jud.getTime() != inst0.toEpochMilli() ||64!jud.equals(Date.from(inst0))) {65System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);66throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");67}68// roundtrip only with millis precision69Date jud0 = Date.from(inst_ms);70if (jud0.getTime() != inst_ms.toEpochMilli() ||71!inst_ms.equals(jud0.toInstant())) {72System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);73throw new RuntimeException("FAILED: instant -> j.u.d -> instant");74}75//////////// java.util.GregorianCalendar /////////////76GregorianCalendar cal = new GregorianCalendar();77// non-roundtrip of tz name between j.u.tz and j.t.zid78cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));79cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));80cal.setFirstDayOfWeek(Calendar.MONDAY);81cal.setMinimalDaysInFirstWeek(4);82cal.setTimeInMillis(millis);83ZonedDateTime zdt0 = cal.toZonedDateTime();84if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||85!cal.equals(GregorianCalendar.from(zdt0))) {86System.out.println("cal:" + cal);87System.out.println("zdt:" + zdt0);88System.out.println("calNew:" + GregorianCalendar.from(zdt0));89System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);90throw new RuntimeException("FAILED: gcal -> zdt -> gcal");91}92inst0 = cal.toInstant();93if (cal.getTimeInMillis() != inst0.toEpochMilli()) {94System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);95throw new RuntimeException("FAILED: gcal -> zdt");96}97ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());98GregorianCalendar cal0 = GregorianCalendar.from(zdt);99if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||100!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {101System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);102throw new RuntimeException("FAILED: zdt -> gcal -> zdt");103}104}105106///////////// java.util.TimeZone /////////////////////////107for (String zidStr : TimeZone.getAvailableIDs()) {108// TBD: tzdt intergration109if (zidStr.startsWith("SystemV") ||110zidStr.contains("Riyadh8") ||111zidStr.equals("US/Pacific-New") ||112zidStr.equals("EST") ||113zidStr.equals("HST") ||114zidStr.equals("MST")) {115continue;116}117ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);118if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {119throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);120}121TimeZone tz = TimeZone.getTimeZone(zidStr);122// no round-trip for alias and "GMT"123if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&124!ZoneId.SHORT_IDS.containsKey(zidStr) &&125!zidStr.startsWith("GMT")) {126throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);127}128}129System.out.println("Passed!");130}131}132133134