|
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.Calendar; |
|
31 import java.util.Date; |
|
32 import java.util.GregorianCalendar; |
|
33 import java.util.Random; |
|
34 import java.util.TimeZone; |
|
35 import java.time.Instant; |
|
36 import java.time.LocalDateTime; |
|
37 import java.time.ZonedDateTime; |
|
38 import java.time.ZoneId; |
|
39 import java.time.ZoneOffset; |
|
40 |
|
41 public class JavatimeTest { |
|
42 |
|
43 static final int NANOS_PER_SECOND = 1000_000_000; |
|
44 |
|
45 public static void main(String[] args) throws Throwable { |
|
46 |
|
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 ///////////// java.util.Date ///////////////////////// |
|
64 Date jud = new java.util.Date(millis); |
|
65 Instant inst0 = jud.toInstant(); |
|
66 if (jud.getTime() != inst0.toEpochMilli() || |
|
67 !jud.equals(Date.from(inst0))) { |
|
68 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
69 throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); |
|
70 } |
|
71 // roundtrip only with millis precision |
|
72 Date jud0 = Date.from(inst_ms); |
|
73 if (jud0.getTime() != inst_ms.toEpochMilli() || |
|
74 !inst_ms.equals(jud0.toInstant())) { |
|
75 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
76 throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); |
|
77 } |
|
78 //////////// java.util.GregorianCalendar ///////////// |
|
79 GregorianCalendar cal = new GregorianCalendar(); |
|
80 cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); |
|
81 cal.setFirstDayOfWeek(Calendar.MONDAY); |
|
82 cal.setMinimalDaysInFirstWeek(4); |
|
83 cal.setTimeInMillis(millis); |
|
84 ZonedDateTime zdt0 = cal.toZonedDateTime(); |
|
85 if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || |
|
86 !cal.equals(GregorianCalendar.from(zdt0))) { |
|
87 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
88 throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); |
|
89 } |
|
90 inst0 = cal.toInstant(); |
|
91 if (cal.getTimeInMillis() != inst0.toEpochMilli()) { |
|
92 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
93 throw new RuntimeException("FAILED: gcal -> zdt"); |
|
94 } |
|
95 ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); |
|
96 GregorianCalendar cal0 = GregorianCalendar.from(zdt); |
|
97 if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || |
|
98 !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { |
|
99 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
100 throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); |
|
101 } |
|
102 } |
|
103 |
|
104 ///////////// java.util.TimeZone ///////////////////////// |
|
105 for (String zidStr : TimeZone.getAvailableIDs()) { |
|
106 // TBD: tzdt intergration |
|
107 if (zidStr.startsWith("SystemV") || |
|
108 zidStr.contains("Riyadh8") || |
|
109 zidStr.equals("US/Pacific-New")) { |
|
110 continue; |
|
111 } |
|
112 ZoneId zid = ZoneId.of(zidStr, ZoneId.OLD_IDS_POST_2005); |
|
113 if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { |
|
114 throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); |
|
115 } |
|
116 TimeZone tz = TimeZone.getTimeZone(zidStr); |
|
117 // no round-trip for alias and "GMT" |
|
118 if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && |
|
119 !ZoneId.OLD_IDS_POST_2005.containsKey(zidStr) && |
|
120 !zidStr.startsWith("GMT")) { |
|
121 throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); |
|
122 } |
|
123 } |
|
124 System.out.println("Passed!"); |
|
125 } |
|
126 } |