8211990: DateTimeException thrown when calculating duration between certain dates

Reviewed-by: lancea, scolebourne, rriggs
This commit is contained in:
Naoto Sato 2019-08-13 10:10:42 -07:00
parent 320eaaccc4
commit cf9351a41f
4 changed files with 49 additions and 9 deletions

View file

@ -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 final class OffsetDateTime
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);
}