--- old/src/java.base/share/classes/java/time/OffsetDateTime.java 2019-08-09 16:18:45.000000000 -0700 +++ new/src/java.base/share/classes/java/time/OffsetDateTime.java 2019-08-09 16:18:45.000000000 -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); } --- old/src/java.base/share/classes/java/time/ZonedDateTime.java 2019-08-09 16:18:46.000000000 -0700 +++ new/src/java.base/share/classes/java/time/ZonedDateTime.java 2019-08-09 16:18:46.000000000 -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); --- old/test/jdk/java/time/test/java/time/TestOffsetDateTime.java 2019-08-09 16:18:47.000000000 -0700 +++ new/test/jdk/java/time/test/java/time/TestOffsetDateTime.java 2019-08-09 16:18:47.000000000 -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)); + } } --- old/test/jdk/java/time/test/java/time/TestZonedDateTime.java 2019-08-09 16:18:49.000000000 -0700 +++ new/test/jdk/java/time/test/java/time/TestZonedDateTime.java 2019-08-09 16:18:48.000000000 -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)); + } }