8211990: DateTimeException thrown when calculating duration between certain dates
authornaoto
Tue, 13 Aug 2019 10:10:42 -0700
changeset 57733 be8c11fc16bb
parent 57732 23e13076e102
child 57734 18f4d3d46d54
8211990: DateTimeException thrown when calculating duration between certain dates Reviewed-by: lancea, scolebourne, rriggs
src/java.base/share/classes/java/time/OffsetDateTime.java
src/java.base/share/classes/java/time/ZonedDateTime.java
test/jdk/java/time/test/java/time/TestOffsetDateTime.java
test/jdk/java/time/test/java/time/TestZonedDateTime.java
--- a/src/java.base/share/classes/java/time/OffsetDateTime.java	Tue Aug 13 13:01:41 2019 -0400
+++ b/src/java.base/share/classes/java/time/OffsetDateTime.java	Tue Aug 13 10:10:42 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1654,8 +1654,14 @@
     public long until(Temporal endExclusive, TemporalUnit unit) {
         OffsetDateTime end = OffsetDateTime.from(endExclusive);
         if (unit instanceof ChronoUnit) {
-            end = end.withOffsetSameInstant(offset);
-            return dateTime.until(end.dateTime, unit);
+            OffsetDateTime start = this;
+            try {
+                end = end.withOffsetSameInstant(offset);
+            } catch (DateTimeException ex) {
+                // end may be out of valid range. Adjust to end's offset.
+                start = withOffsetSameInstant(end.offset);
+            }
+            return start.dateTime.until(end.dateTime, unit);
         }
         return unit.between(this, end);
     }
--- a/src/java.base/share/classes/java/time/ZonedDateTime.java	Tue Aug 13 13:01:41 2019 -0400
+++ b/src/java.base/share/classes/java/time/ZonedDateTime.java	Tue Aug 13 10:10:42 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -2129,11 +2129,17 @@
     public long until(Temporal endExclusive, TemporalUnit unit) {
         ZonedDateTime end = ZonedDateTime.from(endExclusive);
         if (unit instanceof ChronoUnit) {
-            end = end.withZoneSameInstant(zone);
+            ZonedDateTime start = this;
+            try {
+                end = end.withZoneSameInstant(zone);
+            } catch (DateTimeException ex) {
+                // end may be out of valid range. Adjust to end's zone.
+                start = withZoneSameInstant(end.zone);
+            }
             if (unit.isDateBased()) {
-                return dateTime.until(end.dateTime, unit);
+                return start.dateTime.until(end.dateTime, unit);
             } else {
-                return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
+                return start.toOffsetDateTime().until(end.toOffsetDateTime(), unit);
             }
         }
         return unit.between(this, end);
--- a/test/jdk/java/time/test/java/time/TestOffsetDateTime.java	Tue Aug 13 13:01:41 2019 -0400
+++ b/test/jdk/java/time/test/java/time/TestOffsetDateTime.java	Tue Aug 13 10:10:42 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -60,7 +60,9 @@
 package test.java.time;
 
 import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertEquals;
 
+import java.time.Duration;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
@@ -73,6 +75,8 @@
 
 /**
  * Test OffsetDateTime.
+ *
+ * @bug 8211990
  */
 @Test
 public class TestOffsetDateTime extends AbstractTest {
@@ -320,4 +324,11 @@
         assertSame(test, base);
     }
 
+    @Test
+    public void test_duration() {
+        OffsetDateTime start = OffsetDateTime.MAX
+                                .withOffsetSameLocal(ZoneOffset.ofHours(-17));
+        OffsetDateTime end = OffsetDateTime.MAX;
+        assertEquals(Duration.between(start, end), Duration.ofHours(1));
+    }
 }
--- a/test/jdk/java/time/test/java/time/TestZonedDateTime.java	Tue Aug 13 13:01:41 2019 -0400
+++ b/test/jdk/java/time/test/java/time/TestZonedDateTime.java	Tue Aug 13 10:10:42 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -59,12 +59,19 @@
  */
 package test.java.time;
 
+import static org.testng.Assert.assertEquals;
+
+import java.time.Duration;
+import java.time.LocalDateTime;
 import java.time.ZonedDateTime;
+import java.time.ZoneId;
 
 import org.testng.annotations.Test;
 
 /**
  * Test ZonedDateTime.
+ *
+ * @bug 8211990
  */
 @Test
 public class TestZonedDateTime extends AbstractTest {
@@ -74,4 +81,14 @@
         assertImmutable(ZonedDateTime.class);
     }
 
+    @Test
+    public void test_duration() {
+        ZoneId tokyo = ZoneId.of("Asia/Tokyo");
+        ZoneId sanJose = ZoneId.of("America/Los_Angeles");
+
+        ZonedDateTime end = ZonedDateTime.of(LocalDateTime.MAX, sanJose);
+        ZonedDateTime start = end.withZoneSameLocal(tokyo);
+
+        assertEquals(Duration.between(start, end), Duration.ofHours(17));
+    }
 }