author | mduigou |
Tue, 05 Nov 2013 19:44:41 -0800 | |
changeset 21615 | 0231a565a5b7 |
parent 20873 | e91d5b1cb8e6 |
child 24256 | da9a41004459 |
permissions | -rw-r--r-- |
15289 | 1 |
/* |
2 |
* Copyright (c) 2012, 2013, 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 |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
* This file is available under and governed by the GNU General Public |
|
28 |
* License version 2 only, as published by the Free Software Foundation. |
|
29 |
* However, the following notice accompanied the original version of this |
|
30 |
* file: |
|
31 |
* |
|
32 |
* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos |
|
33 |
* |
|
34 |
* All rights reserved. |
|
35 |
* |
|
36 |
* Redistribution and use in source and binary forms, with or without |
|
37 |
* modification, are permitted provided that the following conditions are met: |
|
38 |
* |
|
39 |
* * Redistributions of source code must retain the above copyright notice, |
|
40 |
* this list of conditions and the following disclaimer. |
|
41 |
* |
|
42 |
* * Redistributions in binary form must reproduce the above copyright notice, |
|
43 |
* this list of conditions and the following disclaimer in the documentation |
|
44 |
* and/or other materials provided with the distribution. |
|
45 |
* |
|
46 |
* * Neither the name of JSR-310 nor the names of its contributors |
|
47 |
* may be used to endorse or promote products derived from this software |
|
48 |
* without specific prior written permission. |
|
49 |
* |
|
50 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
51 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
52 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
53 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
54 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
55 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
56 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
57 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
58 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
59 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
60 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
61 |
*/ |
|
62 |
package java.time.temporal; |
|
63 |
||
64 |
import java.time.DateTimeException; |
|
65 |
||
66 |
/** |
|
67 |
* Framework-level interface defining read-write access to a temporal object, |
|
68 |
* such as a date, time, offset or some combination of these. |
|
69 |
* <p> |
|
70 |
* This is the base interface type for date, time and offset objects that |
|
71 |
* are complete enough to be manipulated using plus and minus. |
|
72 |
* It is implemented by those classes that can provide and manipulate information |
|
73 |
* as {@linkplain TemporalField fields} or {@linkplain TemporalQuery queries}. |
|
74 |
* See {@link TemporalAccessor} for the read-only version of this interface. |
|
75 |
* <p> |
|
76 |
* Most date and time information can be represented as a number. |
|
77 |
* These are modeled using {@code TemporalField} with the number held using |
|
78 |
* a {@code long} to handle large values. Year, month and day-of-month are |
|
79 |
* simple examples of fields, but they also include instant and offsets. |
|
80 |
* See {@link ChronoField} for the standard set of fields. |
|
81 |
* <p> |
|
82 |
* Two pieces of date/time information cannot be represented by numbers, |
|
19030 | 83 |
* the {@linkplain java.time.chrono.Chronology chronology} and the |
84 |
* {@linkplain java.time.ZoneId time-zone}. |
|
15289 | 85 |
* These can be accessed via {@link #query(TemporalQuery) queries} using |
16852 | 86 |
* the static methods defined on {@link TemporalQuery}. |
15289 | 87 |
* <p> |
88 |
* This interface is a framework-level interface that should not be widely |
|
89 |
* used in application code. Instead, applications should create and pass |
|
90 |
* around instances of concrete types, such as {@code LocalDate}. |
|
91 |
* There are many reasons for this, part of which is that implementations |
|
92 |
* of this interface may be in calendar systems other than ISO. |
|
15658 | 93 |
* See {@link java.time.chrono.ChronoLocalDate} for a fuller discussion of the issues. |
15289 | 94 |
* |
95 |
* <h3>When to implement</h3> |
|
96 |
* <p> |
|
97 |
* A class should implement this interface if it meets three criteria: |
|
20873 | 98 |
* <ul> |
15289 | 99 |
* <li>it provides access to date/time/offset information, as per {@code TemporalAccessor} |
100 |
* <li>the set of fields are contiguous from the largest to the smallest |
|
101 |
* <li>the set of fields are complete, such that no other field is needed to define the |
|
102 |
* valid range of values for the fields that are represented |
|
20873 | 103 |
* </ul> |
15289 | 104 |
* <p> |
105 |
* Four examples make this clear: |
|
20873 | 106 |
* <ul> |
15289 | 107 |
* <li>{@code LocalDate} implements this interface as it represents a set of fields |
108 |
* that are contiguous from days to forever and require no external information to determine |
|
109 |
* the validity of each date. It is therefore able to implement plus/minus correctly. |
|
110 |
* <li>{@code LocalTime} implements this interface as it represents a set of fields |
|
111 |
* that are contiguous from nanos to within days and require no external information to determine |
|
112 |
* validity. It is able to implement plus/minus correctly, by wrapping around the day. |
|
113 |
* <li>{@code MonthDay}, the combination of month-of-year and day-of-month, does not implement |
|
114 |
* this interface. While the combination is contiguous, from days to months within years, |
|
115 |
* the combination does not have sufficient information to define the valid range of values |
|
116 |
* for day-of-month. As such, it is unable to implement plus/minus correctly. |
|
117 |
* <li>The combination day-of-week and day-of-month ("Friday the 13th") should not implement |
|
118 |
* this interface. It does not represent a contiguous set of fields, as days to weeks overlaps |
|
119 |
* days to months. |
|
20873 | 120 |
* </ul> |
15289 | 121 |
* |
17474 | 122 |
* @implSpec |
15289 | 123 |
* This interface places no restrictions on the mutability of implementations, |
124 |
* however immutability is strongly recommended. |
|
125 |
* All implementations must be {@link Comparable}. |
|
126 |
* |
|
127 |
* @since 1.8 |
|
128 |
*/ |
|
129 |
public interface Temporal extends TemporalAccessor { |
|
130 |
||
131 |
/** |
|
19030 | 132 |
* Checks if the specified unit is supported. |
133 |
* <p> |
|
134 |
* This checks if the specified unit can be added to, or subtracted from, this date-time. |
|
135 |
* If false, then calling the {@link #plus(long, TemporalUnit)} and |
|
136 |
* {@link #minus(long, TemporalUnit) minus} methods will throw an exception. |
|
137 |
* |
|
138 |
* @implSpec |
|
139 |
* Implementations must check and handle all units defined in {@link ChronoUnit}. |
|
140 |
* If the unit is supported, then true must be returned, otherwise false must be returned. |
|
141 |
* <p> |
|
142 |
* If the field is not a {@code ChronoUnit}, then the result of this method |
|
143 |
* is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)} |
|
144 |
* passing {@code this} as the argument. |
|
145 |
* <p> |
|
146 |
* Implementations must ensure that no observable state is altered when this |
|
147 |
* read-only method is invoked. |
|
148 |
* |
|
149 |
* @param unit the unit to check, null returns false |
|
150 |
* @return true if the unit can be added/subtracted, false if not |
|
151 |
*/ |
|
152 |
boolean isSupported(TemporalUnit unit); |
|
153 |
||
154 |
/** |
|
15289 | 155 |
* Returns an adjusted object of the same type as this object with the adjustment made. |
156 |
* <p> |
|
157 |
* This adjusts this date-time according to the rules of the specified adjuster. |
|
158 |
* A simple adjuster might simply set the one of the fields, such as the year field. |
|
159 |
* A more complex adjuster might set the date to the last day of the month. |
|
16852 | 160 |
* A selection of common adjustments is provided in {@link TemporalAdjuster}. |
15289 | 161 |
* These include finding the "last day of the month" and "next Wednesday". |
162 |
* The adjuster is responsible for handling special cases, such as the varying |
|
163 |
* lengths of month and leap years. |
|
164 |
* <p> |
|
165 |
* Some example code indicating how and why this method is used: |
|
166 |
* <pre> |
|
167 |
* date = date.with(Month.JULY); // most key classes implement TemporalAdjuster |
|
168 |
* date = date.with(lastDayOfMonth()); // static import from Adjusters |
|
169 |
* date = date.with(next(WEDNESDAY)); // static import from Adjusters and DayOfWeek |
|
170 |
* </pre> |
|
171 |
* |
|
17474 | 172 |
* @implSpec |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
173 |
* <p> |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
174 |
* Implementations must not alter either this object or the specified temporal object. |
15289 | 175 |
* Instead, an adjusted copy of the original must be returned. |
176 |
* This provides equivalent, safe behavior for immutable and mutable implementations. |
|
177 |
* <p> |
|
178 |
* The default implementation must behave equivalent to this code: |
|
179 |
* <pre> |
|
180 |
* return adjuster.adjustInto(this); |
|
181 |
* </pre> |
|
182 |
* |
|
183 |
* @param adjuster the adjuster to use, not null |
|
184 |
* @return an object of the same type with the specified adjustment made, not null |
|
185 |
* @throws DateTimeException if unable to make the adjustment |
|
186 |
* @throws ArithmeticException if numeric overflow occurs |
|
187 |
*/ |
|
16852 | 188 |
default Temporal with(TemporalAdjuster adjuster) { |
15289 | 189 |
return adjuster.adjustInto(this); |
190 |
} |
|
191 |
||
192 |
/** |
|
193 |
* Returns an object of the same type as this object with the specified field altered. |
|
194 |
* <p> |
|
195 |
* This returns a new object based on this one with the value for the specified field changed. |
|
196 |
* For example, on a {@code LocalDate}, this could be used to set the year, month or day-of-month. |
|
197 |
* The returned object will have the same observable type as this object. |
|
198 |
* <p> |
|
199 |
* In some cases, changing a field is not fully defined. For example, if the target object is |
|
200 |
* a date representing the 31st January, then changing the month to February would be unclear. |
|
201 |
* In cases like this, the field is responsible for resolving the result. Typically it will choose |
|
202 |
* the previous valid date, which would be the last valid day of February in this example. |
|
203 |
* |
|
17474 | 204 |
* @implSpec |
15289 | 205 |
* Implementations must check and handle all fields defined in {@link ChronoField}. |
206 |
* If the field is supported, then the adjustment must be performed. |
|
16852 | 207 |
* If unsupported, then an {@code UnsupportedTemporalTypeException} must be thrown. |
15289 | 208 |
* <p> |
209 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
15658 | 210 |
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} |
15289 | 211 |
* passing {@code this} as the first argument. |
212 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
213 |
* Implementations must not alter this object. |
15289 | 214 |
* Instead, an adjusted copy of the original must be returned. |
215 |
* This provides equivalent, safe behavior for immutable and mutable implementations. |
|
216 |
* |
|
217 |
* @param field the field to set in the result, not null |
|
218 |
* @param newValue the new value of the field in the result |
|
219 |
* @return an object of the same type with the specified field set, not null |
|
220 |
* @throws DateTimeException if the field cannot be set |
|
16852 | 221 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15289 | 222 |
* @throws ArithmeticException if numeric overflow occurs |
223 |
*/ |
|
224 |
Temporal with(TemporalField field, long newValue); |
|
225 |
||
226 |
//----------------------------------------------------------------------- |
|
227 |
/** |
|
228 |
* Returns an object of the same type as this object with an amount added. |
|
229 |
* <p> |
|
15658 | 230 |
* This adjusts this temporal, adding according to the rules of the specified amount. |
231 |
* The amount is typically a {@link java.time.Period} but may be any other type implementing |
|
232 |
* the {@link TemporalAmount} interface, such as {@link java.time.Duration}. |
|
15289 | 233 |
* <p> |
234 |
* Some example code indicating how and why this method is used: |
|
235 |
* <pre> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
236 |
* date = date.plus(period); // add a Period instance |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
237 |
* date = date.plus(duration); // add a Duration instance |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
238 |
* date = date.plus(workingDays(6)); // example user-written workingDays method |
15289 | 239 |
* </pre> |
240 |
* <p> |
|
241 |
* Note that calling {@code plus} followed by {@code minus} is not guaranteed to |
|
242 |
* return the same date-time. |
|
243 |
* |
|
17474 | 244 |
* @implSpec |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
245 |
* <p> |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
246 |
* Implementations must not alter either this object or the specified temporal object. |
15289 | 247 |
* Instead, an adjusted copy of the original must be returned. |
248 |
* This provides equivalent, safe behavior for immutable and mutable implementations. |
|
249 |
* <p> |
|
250 |
* The default implementation must behave equivalent to this code: |
|
251 |
* <pre> |
|
15658 | 252 |
* return amount.addTo(this); |
15289 | 253 |
* </pre> |
254 |
* |
|
15658 | 255 |
* @param amount the amount to add, not null |
15289 | 256 |
* @return an object of the same type with the specified adjustment made, not null |
257 |
* @throws DateTimeException if the addition cannot be made |
|
258 |
* @throws ArithmeticException if numeric overflow occurs |
|
259 |
*/ |
|
16852 | 260 |
default Temporal plus(TemporalAmount amount) { |
15658 | 261 |
return amount.addTo(this); |
15289 | 262 |
} |
263 |
||
264 |
/** |
|
265 |
* Returns an object of the same type as this object with the specified period added. |
|
266 |
* <p> |
|
267 |
* This method returns a new object based on this one with the specified period added. |
|
268 |
* For example, on a {@code LocalDate}, this could be used to add a number of years, months or days. |
|
269 |
* The returned object will have the same observable type as this object. |
|
270 |
* <p> |
|
271 |
* In some cases, changing a field is not fully defined. For example, if the target object is |
|
272 |
* a date representing the 31st January, then adding one month would be unclear. |
|
273 |
* In cases like this, the field is responsible for resolving the result. Typically it will choose |
|
274 |
* the previous valid date, which would be the last valid day of February in this example. |
|
275 |
* |
|
17474 | 276 |
* @implSpec |
15289 | 277 |
* Implementations must check and handle all units defined in {@link ChronoUnit}. |
278 |
* If the unit is supported, then the addition must be performed. |
|
16852 | 279 |
* If unsupported, then an {@code UnsupportedTemporalTypeException} must be thrown. |
15289 | 280 |
* <p> |
281 |
* If the unit is not a {@code ChronoUnit}, then the result of this method |
|
15658 | 282 |
* is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)} |
15289 | 283 |
* passing {@code this} as the first argument. |
284 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
285 |
* Implementations must not alter this object. |
15289 | 286 |
* Instead, an adjusted copy of the original must be returned. |
287 |
* This provides equivalent, safe behavior for immutable and mutable implementations. |
|
288 |
* |
|
289 |
* @param amountToAdd the amount of the specified unit to add, may be negative |
|
290 |
* @param unit the unit of the period to add, not null |
|
291 |
* @return an object of the same type with the specified period added, not null |
|
292 |
* @throws DateTimeException if the unit cannot be added |
|
16852 | 293 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15289 | 294 |
* @throws ArithmeticException if numeric overflow occurs |
295 |
*/ |
|
296 |
Temporal plus(long amountToAdd, TemporalUnit unit); |
|
297 |
||
298 |
//----------------------------------------------------------------------- |
|
299 |
/** |
|
300 |
* Returns an object of the same type as this object with an amount subtracted. |
|
301 |
* <p> |
|
15658 | 302 |
* This adjusts this temporal, subtracting according to the rules of the specified amount. |
303 |
* The amount is typically a {@link java.time.Period} but may be any other type implementing |
|
304 |
* the {@link TemporalAmount} interface, such as {@link java.time.Duration}. |
|
15289 | 305 |
* <p> |
306 |
* Some example code indicating how and why this method is used: |
|
307 |
* <pre> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
308 |
* date = date.minus(period); // subtract a Period instance |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
309 |
* date = date.minus(duration); // subtract a Duration instance |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
310 |
* date = date.minus(workingDays(6)); // example user-written workingDays method |
15289 | 311 |
* </pre> |
312 |
* <p> |
|
313 |
* Note that calling {@code plus} followed by {@code minus} is not guaranteed to |
|
314 |
* return the same date-time. |
|
315 |
* |
|
17474 | 316 |
* @implSpec |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
317 |
* <p> |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
318 |
* Implementations must not alter either this object or the specified temporal object. |
15289 | 319 |
* Instead, an adjusted copy of the original must be returned. |
320 |
* This provides equivalent, safe behavior for immutable and mutable implementations. |
|
321 |
* <p> |
|
322 |
* The default implementation must behave equivalent to this code: |
|
323 |
* <pre> |
|
15658 | 324 |
* return amount.subtractFrom(this); |
15289 | 325 |
* </pre> |
326 |
* |
|
15658 | 327 |
* @param amount the amount to subtract, not null |
15289 | 328 |
* @return an object of the same type with the specified adjustment made, not null |
329 |
* @throws DateTimeException if the subtraction cannot be made |
|
330 |
* @throws ArithmeticException if numeric overflow occurs |
|
331 |
*/ |
|
16852 | 332 |
default Temporal minus(TemporalAmount amount) { |
15658 | 333 |
return amount.subtractFrom(this); |
15289 | 334 |
} |
335 |
||
336 |
/** |
|
337 |
* Returns an object of the same type as this object with the specified period subtracted. |
|
338 |
* <p> |
|
339 |
* This method returns a new object based on this one with the specified period subtracted. |
|
340 |
* For example, on a {@code LocalDate}, this could be used to subtract a number of years, months or days. |
|
341 |
* The returned object will have the same observable type as this object. |
|
342 |
* <p> |
|
343 |
* In some cases, changing a field is not fully defined. For example, if the target object is |
|
344 |
* a date representing the 31st March, then subtracting one month would be unclear. |
|
345 |
* In cases like this, the field is responsible for resolving the result. Typically it will choose |
|
346 |
* the previous valid date, which would be the last valid day of February in this example. |
|
347 |
* |
|
17474 | 348 |
* @implSpec |
15289 | 349 |
* Implementations must behave in a manor equivalent to the default method behavior. |
350 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19030
diff
changeset
|
351 |
* Implementations must not alter this object. |
15289 | 352 |
* Instead, an adjusted copy of the original must be returned. |
353 |
* This provides equivalent, safe behavior for immutable and mutable implementations. |
|
354 |
* <p> |
|
355 |
* The default implementation must behave equivalent to this code: |
|
356 |
* <pre> |
|
357 |
* return (amountToSubtract == Long.MIN_VALUE ? |
|
358 |
* plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
|
359 |
* </pre> |
|
360 |
* |
|
361 |
* @param amountToSubtract the amount of the specified unit to subtract, may be negative |
|
362 |
* @param unit the unit of the period to subtract, not null |
|
363 |
* @return an object of the same type with the specified period subtracted, not null |
|
364 |
* @throws DateTimeException if the unit cannot be subtracted |
|
16852 | 365 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15289 | 366 |
* @throws ArithmeticException if numeric overflow occurs |
367 |
*/ |
|
16852 | 368 |
default Temporal minus(long amountToSubtract, TemporalUnit unit) { |
15289 | 369 |
return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
370 |
} |
|
371 |
||
372 |
//----------------------------------------------------------------------- |
|
373 |
/** |
|
17474 | 374 |
* Calculates the amount of time until another temporal in terms of the specified unit. |
15289 | 375 |
* <p> |
17474 | 376 |
* This calculates the amount of time between two temporal objects |
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
377 |
* in terms of a single {@code TemporalUnit}. |
15289 | 378 |
* The start and end points are {@code this} and the specified temporal. |
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
379 |
* The end point is converted to be of the same type as the start point if different. |
15289 | 380 |
* The result will be negative if the end is before the start. |
381 |
* For example, the period in hours between two temporal objects can be |
|
19030 | 382 |
* calculated using {@code startTime.until(endTime, HOURS)}. |
15289 | 383 |
* <p> |
384 |
* The calculation returns a whole number, representing the number of |
|
385 |
* complete units between the two temporals. |
|
386 |
* For example, the period in hours between the times 11:30 and 13:29 |
|
387 |
* will only be one hour as it is one minute short of two hours. |
|
388 |
* <p> |
|
15658 | 389 |
* There are two equivalent ways of using this method. |
390 |
* The first is to invoke this method directly. |
|
391 |
* The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: |
|
15289 | 392 |
* <pre> |
15658 | 393 |
* // these two lines are equivalent |
19030 | 394 |
* temporal = start.until(end, unit); |
15658 | 395 |
* temporal = unit.between(start, end); |
396 |
* </pre> |
|
397 |
* The choice should be made based on which makes the code more readable. |
|
398 |
* <p> |
|
399 |
* For example, this method allows the number of days between two dates to |
|
400 |
* be calculated: |
|
401 |
* <pre> |
|
19030 | 402 |
* long daysBetween = start.until(end, DAYS); |
15658 | 403 |
* // or alternatively |
404 |
* long daysBetween = DAYS.between(start, end); |
|
15289 | 405 |
* </pre> |
406 |
* |
|
17474 | 407 |
* @implSpec |
15289 | 408 |
* Implementations must begin by checking to ensure that the input temporal |
409 |
* object is of the same observable type as the implementation. |
|
410 |
* They must then perform the calculation for all instances of {@link ChronoUnit}. |
|
16852 | 411 |
* An {@code UnsupportedTemporalTypeException} must be thrown for {@code ChronoUnit} |
15289 | 412 |
* instances that are unsupported. |
413 |
* <p> |
|
414 |
* If the unit is not a {@code ChronoUnit}, then the result of this method |
|
415 |
* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
416 |
* passing {@code this} as the first argument and the converted input temporal as |
15289 | 417 |
* the second argument. |
418 |
* <p> |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
419 |
* In summary, implementations must behave in a manner equivalent to this pseudo-code: |
15289 | 420 |
* <pre> |
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
421 |
* // convert the end temporal to the same type as this class |
15289 | 422 |
* if (unit instanceof ChronoUnit) { |
423 |
* // if unit is supported, then calculate and return result |
|
16852 | 424 |
* // else throw UnsupportedTemporalTypeException for unsupported units |
15289 | 425 |
* } |
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
426 |
* return unit.between(this, convertedEndTemporal); |
15289 | 427 |
* </pre> |
428 |
* <p> |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
429 |
* Note that the unit's {@code between} method must only be invoked if the |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
430 |
* two temporal objects have exactly the same type evaluated by {@code getClass()}. |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
431 |
* <p> |
19030 | 432 |
* Implementations must ensure that no observable state is altered when this |
433 |
* read-only method is invoked. |
|
15289 | 434 |
* |
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
435 |
* @param endExclusive the end temporal, exclusive, converted to be of the |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
436 |
* same type as this object, not null |
17474 | 437 |
* @param unit the unit to measure the amount in, not null |
438 |
* @return the amount of time between this temporal object and the specified one |
|
439 |
* in terms of the unit; positive if the specified object is later than this one, |
|
440 |
* negative if it is earlier than this one |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
441 |
* @throws DateTimeException if the amount cannot be calculated, or the end |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
442 |
* temporal cannot be converted to the same type as this temporal |
16852 | 443 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15289 | 444 |
* @throws ArithmeticException if numeric overflow occurs |
445 |
*/ |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
446 |
long until(Temporal endExclusive, TemporalUnit unit); |
15289 | 447 |
|
448 |
} |