src/java.base/share/classes/java/time/ZonedDateTime.java
changeset 57733 be8c11fc16bb
parent 52078 4a63197816ce
child 57956 e0b8b019d2f5
equal deleted inserted replaced
57732:23e13076e102 57733:be8c11fc16bb
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
  2127      */
  2127      */
  2128     @Override
  2128     @Override
  2129     public long until(Temporal endExclusive, TemporalUnit unit) {
  2129     public long until(Temporal endExclusive, TemporalUnit unit) {
  2130         ZonedDateTime end = ZonedDateTime.from(endExclusive);
  2130         ZonedDateTime end = ZonedDateTime.from(endExclusive);
  2131         if (unit instanceof ChronoUnit) {
  2131         if (unit instanceof ChronoUnit) {
  2132             end = end.withZoneSameInstant(zone);
  2132             ZonedDateTime start = this;
       
  2133             try {
       
  2134                 end = end.withZoneSameInstant(zone);
       
  2135             } catch (DateTimeException ex) {
       
  2136                 // end may be out of valid range. Adjust to end's zone.
       
  2137                 start = withZoneSameInstant(end.zone);
       
  2138             }
  2133             if (unit.isDateBased()) {
  2139             if (unit.isDateBased()) {
  2134                 return dateTime.until(end.dateTime, unit);
  2140                 return start.dateTime.until(end.dateTime, unit);
  2135             } else {
  2141             } else {
  2136                 return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
  2142                 return start.toOffsetDateTime().until(end.toOffsetDateTime(), unit);
  2137             }
  2143             }
  2138         }
  2144         }
  2139         return unit.between(this, end);
  2145         return unit.between(this, end);
  2140     }
  2146     }
  2141 
  2147