1 /*
2 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
2112 * <p>
2113 * If the unit is not a {@code ChronoUnit}, then the result of this method
2114 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
2115 * passing {@code this} as the first argument and the converted input temporal
2116 * as the second argument.
2117 * <p>
2118 * This instance is immutable and unaffected by this method call.
2119 *
2120 * @param endExclusive the end date, exclusive, which is converted to a {@code ZonedDateTime}, not null
2121 * @param unit the unit to measure the amount in, not null
2122 * @return the amount of time between this date-time and the end date-time
2123 * @throws DateTimeException if the amount cannot be calculated, or the end
2124 * temporal cannot be converted to a {@code ZonedDateTime}
2125 * @throws UnsupportedTemporalTypeException if the unit is not supported
2126 * @throws ArithmeticException if numeric overflow occurs
2127 */
2128 @Override
2129 public long until(Temporal endExclusive, TemporalUnit unit) {
2130 ZonedDateTime end = ZonedDateTime.from(endExclusive);
2131 if (unit instanceof ChronoUnit) {
2132 end = end.withZoneSameInstant(zone);
2133 if (unit.isDateBased()) {
2134 return dateTime.until(end.dateTime, unit);
2135 } else {
2136 return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
2137 }
2138 }
2139 return unit.between(this, end);
2140 }
2141
2142 /**
2143 * Formats this date-time using the specified formatter.
2144 * <p>
2145 * This date-time will be passed to the formatter to produce a string.
2146 *
2147 * @param formatter the formatter to use, not null
2148 * @return the formatted date-time string, not null
2149 * @throws DateTimeException if an error occurs during printing
2150 */
2151 @Override // override for Javadoc and performance
2152 public String format(DateTimeFormatter formatter) {
2153 Objects.requireNonNull(formatter, "formatter");
2154 return formatter.format(this);
2155 }
2156
|
1 /*
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.
4 *
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
2112 * <p>
2113 * If the unit is not a {@code ChronoUnit}, then the result of this method
2114 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
2115 * passing {@code this} as the first argument and the converted input temporal
2116 * as the second argument.
2117 * <p>
2118 * This instance is immutable and unaffected by this method call.
2119 *
2120 * @param endExclusive the end date, exclusive, which is converted to a {@code ZonedDateTime}, not null
2121 * @param unit the unit to measure the amount in, not null
2122 * @return the amount of time between this date-time and the end date-time
2123 * @throws DateTimeException if the amount cannot be calculated, or the end
2124 * temporal cannot be converted to a {@code ZonedDateTime}
2125 * @throws UnsupportedTemporalTypeException if the unit is not supported
2126 * @throws ArithmeticException if numeric overflow occurs
2127 */
2128 @Override
2129 public long until(Temporal endExclusive, TemporalUnit unit) {
2130 ZonedDateTime end = ZonedDateTime.from(endExclusive);
2131 if (unit instanceof ChronoUnit) {
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 }
2139 if (unit.isDateBased()) {
2140 return start.dateTime.until(end.dateTime, unit);
2141 } else {
2142 return start.toOffsetDateTime().until(end.toOffsetDateTime(), unit);
2143 }
2144 }
2145 return unit.between(this, end);
2146 }
2147
2148 /**
2149 * Formats this date-time using the specified formatter.
2150 * <p>
2151 * This date-time will be passed to the formatter to produce a string.
2152 *
2153 * @param formatter the formatter to use, not null
2154 * @return the formatted date-time string, not null
2155 * @throws DateTimeException if an error occurs during printing
2156 */
2157 @Override // override for Javadoc and performance
2158 public String format(DateTimeFormatter formatter) {
2159 Objects.requireNonNull(formatter, "formatter");
2160 return formatter.format(this);
2161 }
2162
|