changeset 14431:d7caf74c48ab

8066644: Fix deprecation warnings in jdk.zipfs module Reviewed-by: sherman, shade Contributed-by: Peter Levart <peter.levart@gmail.com>, Claes Redestad <claes.redestad@gmail.com>
author redestad
date Wed, 28 Oct 2015 23:31:01 +0100
parents 71e43dd2e0b9
children a0eb148fa9d5
files src/java.base/share/classes/java/util/zip/ZipUtils.java src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipUtils.java
diffstat 2 files changed, 48 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/zip/ZipUtils.java	Mon Oct 26 19:49:22 2015 +0530
+++ b/src/java.base/share/classes/java/util/zip/ZipUtils.java	Wed Oct 28 23:31:01 2015 +0100
@@ -26,7 +26,9 @@
 package java.util.zip;
 
 import java.nio.file.attribute.FileTime;
-import java.util.Date;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
 import java.util.concurrent.TimeUnit;
 
 class ZipUtils {
@@ -66,15 +68,16 @@
     /**
      * Converts DOS time to Java time (number of milliseconds since epoch).
      */
-    private static long dosToJavaTime(long dtime) {
-        @SuppressWarnings("deprecation") // Use of date constructor.
-        Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
-                          (int)(((dtime >> 21) & 0x0f) - 1),
-                          (int)((dtime >> 16) & 0x1f),
-                          (int)((dtime >> 11) & 0x1f),
-                          (int)((dtime >> 5) & 0x3f),
-                          (int)((dtime << 1) & 0x3e));
-        return d.getTime();
+    public static long dosToJavaTime(long dtime) {
+        LocalDateTime ldt = LocalDateTime.of(
+                (int) (((dtime >> 25) & 0x7f) + 1980),
+                (int) ((dtime >> 21) & 0x0f),
+                (int) ((dtime >> 16) & 0x1f),
+                (int) ((dtime >> 11) & 0x1f),
+                (int) ((dtime >> 5) & 0x3f),
+                (int) ((dtime << 1) & 0x3e));
+        return TimeUnit.MILLISECONDS.convert(ldt.toEpochSecond(
+                ZoneId.systemDefault().getRules().getOffset(ldt)), TimeUnit.SECONDS);
     }
 
     /**
@@ -92,16 +95,20 @@
     /**
      * Converts Java time to DOS time.
      */
-    @SuppressWarnings("deprecation") // Use of date methods
     private static long javaToDosTime(long time) {
-        Date d = new Date(time);
-        int year = d.getYear() + 1900;
-        if (year < 1980) {
-            return ZipEntry.DOSTIME_BEFORE_1980;
+        Instant instant = Instant.ofEpochMilli(time);
+        LocalDateTime ldt = LocalDateTime.ofInstant(
+                instant, ZoneId.systemDefault());
+        int year = ldt.getYear() - 1980;
+        if (year < 0) {
+            return (1 << 21) | (1 << 16);
         }
-        return ((year - 1980) << 25 | (d.getMonth() + 1) << 21 |
-                d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
-                d.getSeconds() >> 1) & 0xffffffffL;
+        return (year << 25 |
+            ldt.getMonthValue() << 21 |
+            ldt.getDayOfMonth() << 16 |
+            ldt.getHour() << 11 |
+            ldt.getMinute() << 5 |
+            ldt.getSecond() >> 1) & 0xffffffffL;
     }
 
     /**
--- a/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipUtils.java	Mon Oct 26 19:49:22 2015 +0530
+++ b/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipUtils.java	Wed Oct 28 23:31:01 2015 +0100
@@ -27,8 +27,10 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
 import java.util.Arrays;
-import java.util.Date;
 import java.util.regex.PatternSyntaxException;
 import java.util.concurrent.TimeUnit;
 
@@ -103,30 +105,35 @@
     /*
      * Converts DOS time to Java time (number of milliseconds since epoch).
      */
-    @SuppressWarnings("deprecation")
     public static long dosToJavaTime(long dtime) {
-        Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
-                          (int)(((dtime >> 21) & 0x0f) - 1),
-                          (int)((dtime >> 16) & 0x1f),
-                          (int)((dtime >> 11) & 0x1f),
-                          (int)((dtime >> 5) & 0x3f),
-                          (int)((dtime << 1) & 0x3e));
-        return d.getTime();
+        LocalDateTime ldt = LocalDateTime.of(
+                (int) (((dtime >> 25) & 0x7f) + 1980),
+                (int) ((dtime >> 21) & 0x0f),
+                (int) ((dtime >> 16) & 0x1f),
+                (int) ((dtime >> 11) & 0x1f),
+                (int) ((dtime >> 5) & 0x3f),
+                (int) ((dtime << 1) & 0x3e));
+        return TimeUnit.MILLISECONDS.convert(ldt.toEpochSecond(
+                ZoneId.systemDefault().getRules().getOffset(ldt)), TimeUnit.SECONDS);
     }
 
     /*
      * Converts Java time to DOS time.
      */
-    @SuppressWarnings("deprecation")
     public static long javaToDosTime(long time) {
-        Date d = new Date(time);
-        int year = d.getYear() + 1900;
-        if (year < 1980) {
+        Instant instant = Instant.ofEpochMilli(time);
+        LocalDateTime ldt = LocalDateTime.ofInstant(
+                instant, ZoneId.systemDefault());
+        int year = ldt.getYear() - 1980;
+        if (year < 0) {
             return (1 << 21) | (1 << 16);
         }
-        return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
-               d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
-               d.getSeconds() >> 1;
+        return (year << 25 |
+            ldt.getMonthValue() << 21 |
+            ldt.getDayOfMonth() << 16 |
+            ldt.getHour() << 11 |
+            ldt.getMinute() << 5 |
+            ldt.getSecond() >> 1) & 0xffffffffL;
     }