author | mduigou |
Tue, 05 Nov 2013 19:44:41 -0800 | |
changeset 21615 | 0231a565a5b7 |
parent 20795 | 8ec9e5b79828 |
child 22081 | 86eb26ff8f2b |
permissions | -rw-r--r-- |
15658 | 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) 2007-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; |
|
63 |
||
64 |
import static java.time.temporal.ChronoField.ERA; |
|
65 |
import static java.time.temporal.ChronoField.YEAR; |
|
66 |
import static java.time.temporal.ChronoField.YEAR_OF_ERA; |
|
19030 | 67 |
import static java.time.temporal.ChronoUnit.CENTURIES; |
68 |
import static java.time.temporal.ChronoUnit.DECADES; |
|
69 |
import static java.time.temporal.ChronoUnit.ERAS; |
|
70 |
import static java.time.temporal.ChronoUnit.MILLENNIA; |
|
15658 | 71 |
import static java.time.temporal.ChronoUnit.YEARS; |
72 |
||
73 |
import java.io.DataInput; |
|
74 |
import java.io.DataOutput; |
|
75 |
import java.io.IOException; |
|
76 |
import java.io.InvalidObjectException; |
|
77 |
import java.io.Serializable; |
|
78 |
import java.time.chrono.Chronology; |
|
79 |
import java.time.chrono.IsoChronology; |
|
80 |
import java.time.format.DateTimeFormatter; |
|
81 |
import java.time.format.DateTimeFormatterBuilder; |
|
82 |
import java.time.format.DateTimeParseException; |
|
83 |
import java.time.format.SignStyle; |
|
84 |
import java.time.temporal.ChronoField; |
|
85 |
import java.time.temporal.ChronoUnit; |
|
86 |
import java.time.temporal.Temporal; |
|
87 |
import java.time.temporal.TemporalAccessor; |
|
88 |
import java.time.temporal.TemporalAdjuster; |
|
89 |
import java.time.temporal.TemporalAmount; |
|
90 |
import java.time.temporal.TemporalField; |
|
20795 | 91 |
import java.time.temporal.TemporalQueries; |
15658 | 92 |
import java.time.temporal.TemporalQuery; |
93 |
import java.time.temporal.TemporalUnit; |
|
16852 | 94 |
import java.time.temporal.UnsupportedTemporalTypeException; |
15658 | 95 |
import java.time.temporal.ValueRange; |
96 |
import java.util.Objects; |
|
97 |
||
98 |
/** |
|
99 |
* A year in the ISO-8601 calendar system, such as {@code 2007}. |
|
100 |
* <p> |
|
101 |
* {@code Year} is an immutable date-time object that represents a year. |
|
102 |
* Any field that can be derived from a year can be obtained. |
|
103 |
* <p> |
|
104 |
* <b>Note that years in the ISO chronology only align with years in the |
|
105 |
* Gregorian-Julian system for modern years. Parts of Russia did not switch to the |
|
106 |
* modern Gregorian/ISO rules until 1920. |
|
107 |
* As such, historical years must be treated with caution.</b> |
|
108 |
* <p> |
|
109 |
* This class does not store or represent a month, day, time or time-zone. |
|
110 |
* For example, the value "2007" can be stored in a {@code Year}. |
|
111 |
* <p> |
|
112 |
* Years represented by this class follow the ISO-8601 standard and use |
|
113 |
* the proleptic numbering system. Year 1 is preceded by year 0, then by year -1. |
|
114 |
* <p> |
|
115 |
* The ISO-8601 calendar system is the modern civil calendar system used today |
|
116 |
* in most of the world. It is equivalent to the proleptic Gregorian calendar |
|
117 |
* system, in which today's rules for leap years are applied for all time. |
|
118 |
* For most applications written today, the ISO-8601 rules are entirely suitable. |
|
119 |
* However, any application that makes use of historical dates, and requires them |
|
120 |
* to be accurate will find the ISO-8601 approach unsuitable. |
|
121 |
* |
|
17474 | 122 |
* @implSpec |
15658 | 123 |
* This class is immutable and thread-safe. |
124 |
* |
|
125 |
* @since 1.8 |
|
126 |
*/ |
|
127 |
public final class Year |
|
128 |
implements Temporal, TemporalAdjuster, Comparable<Year>, Serializable { |
|
129 |
||
130 |
/** |
|
131 |
* The minimum supported year, '-999,999,999'. |
|
132 |
*/ |
|
133 |
public static final int MIN_VALUE = -999_999_999; |
|
134 |
/** |
|
135 |
* The maximum supported year, '+999,999,999'. |
|
136 |
*/ |
|
137 |
public static final int MAX_VALUE = 999_999_999; |
|
138 |
||
139 |
/** |
|
140 |
* Serialization version. |
|
141 |
*/ |
|
142 |
private static final long serialVersionUID = -23038383694477807L; |
|
143 |
/** |
|
144 |
* Parser. |
|
145 |
*/ |
|
146 |
private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder() |
|
147 |
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) |
|
148 |
.toFormatter(); |
|
149 |
||
150 |
/** |
|
151 |
* The year being represented. |
|
152 |
*/ |
|
153 |
private final int year; |
|
154 |
||
155 |
//----------------------------------------------------------------------- |
|
156 |
/** |
|
157 |
* Obtains the current year from the system clock in the default time-zone. |
|
158 |
* <p> |
|
159 |
* This will query the {@link java.time.Clock#systemDefaultZone() system clock} in the default |
|
160 |
* time-zone to obtain the current year. |
|
161 |
* <p> |
|
162 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
163 |
* because the clock is hard-coded. |
|
164 |
* |
|
165 |
* @return the current year using the system clock and default time-zone, not null |
|
166 |
*/ |
|
167 |
public static Year now() { |
|
168 |
return now(Clock.systemDefaultZone()); |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Obtains the current year from the system clock in the specified time-zone. |
|
173 |
* <p> |
|
174 |
* This will query the {@link Clock#system(java.time.ZoneId) system clock} to obtain the current year. |
|
175 |
* Specifying the time-zone avoids dependence on the default time-zone. |
|
176 |
* <p> |
|
177 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
178 |
* because the clock is hard-coded. |
|
179 |
* |
|
180 |
* @param zone the zone ID to use, not null |
|
181 |
* @return the current year using the system clock, not null |
|
182 |
*/ |
|
183 |
public static Year now(ZoneId zone) { |
|
184 |
return now(Clock.system(zone)); |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Obtains the current year from the specified clock. |
|
189 |
* <p> |
|
190 |
* This will query the specified clock to obtain the current year. |
|
191 |
* Using this method allows the use of an alternate clock for testing. |
|
192 |
* The alternate clock may be introduced using {@link Clock dependency injection}. |
|
193 |
* |
|
194 |
* @param clock the clock to use, not null |
|
195 |
* @return the current year, not null |
|
196 |
*/ |
|
197 |
public static Year now(Clock clock) { |
|
198 |
final LocalDate now = LocalDate.now(clock); // called once |
|
199 |
return Year.of(now.getYear()); |
|
200 |
} |
|
201 |
||
202 |
//----------------------------------------------------------------------- |
|
203 |
/** |
|
204 |
* Obtains an instance of {@code Year}. |
|
205 |
* <p> |
|
206 |
* This method accepts a year value from the proleptic ISO calendar system. |
|
207 |
* <p> |
|
208 |
* The year 2AD/CE is represented by 2.<br> |
|
209 |
* The year 1AD/CE is represented by 1.<br> |
|
210 |
* The year 1BC/BCE is represented by 0.<br> |
|
211 |
* The year 2BC/BCE is represented by -1.<br> |
|
212 |
* |
|
213 |
* @param isoYear the ISO proleptic year to represent, from {@code MIN_VALUE} to {@code MAX_VALUE} |
|
214 |
* @return the year, not null |
|
215 |
* @throws DateTimeException if the field is invalid |
|
216 |
*/ |
|
217 |
public static Year of(int isoYear) { |
|
218 |
YEAR.checkValidValue(isoYear); |
|
219 |
return new Year(isoYear); |
|
220 |
} |
|
221 |
||
222 |
//----------------------------------------------------------------------- |
|
223 |
/** |
|
224 |
* Obtains an instance of {@code Year} from a temporal object. |
|
225 |
* <p> |
|
226 |
* This obtains a year based on the specified temporal. |
|
227 |
* A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
|
228 |
* which this factory converts to an instance of {@code Year}. |
|
229 |
* <p> |
|
230 |
* The conversion extracts the {@link ChronoField#YEAR year} field. |
|
231 |
* The extraction is only permitted if the temporal object has an ISO |
|
232 |
* chronology, or can be converted to a {@code LocalDate}. |
|
233 |
* <p> |
|
234 |
* This method matches the signature of the functional interface {@link TemporalQuery} |
|
235 |
* allowing it to be used in queries via method reference, {@code Year::from}. |
|
236 |
* |
|
237 |
* @param temporal the temporal object to convert, not null |
|
238 |
* @return the year, not null |
|
239 |
* @throws DateTimeException if unable to convert to a {@code Year} |
|
240 |
*/ |
|
241 |
public static Year from(TemporalAccessor temporal) { |
|
242 |
if (temporal instanceof Year) { |
|
243 |
return (Year) temporal; |
|
244 |
} |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
245 |
Objects.requireNonNull(temporal, "temporal"); |
15658 | 246 |
try { |
247 |
if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) { |
|
248 |
temporal = LocalDate.from(temporal); |
|
249 |
} |
|
250 |
return of(temporal.get(YEAR)); |
|
251 |
} catch (DateTimeException ex) { |
|
20747 | 252 |
throw new DateTimeException("Unable to obtain Year from TemporalAccessor: " + |
253 |
temporal + " of type " + temporal.getClass().getName(), ex); |
|
15658 | 254 |
} |
255 |
} |
|
256 |
||
257 |
//----------------------------------------------------------------------- |
|
258 |
/** |
|
259 |
* Obtains an instance of {@code Year} from a text string such as {@code 2007}. |
|
260 |
* <p> |
|
261 |
* The string must represent a valid year. |
|
262 |
* Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol. |
|
263 |
* |
|
264 |
* @param text the text to parse such as "2007", not null |
|
265 |
* @return the parsed year, not null |
|
266 |
* @throws DateTimeParseException if the text cannot be parsed |
|
267 |
*/ |
|
268 |
public static Year parse(CharSequence text) { |
|
269 |
return parse(text, PARSER); |
|
270 |
} |
|
271 |
||
272 |
/** |
|
273 |
* Obtains an instance of {@code Year} from a text string using a specific formatter. |
|
274 |
* <p> |
|
275 |
* The text is parsed using the formatter, returning a year. |
|
276 |
* |
|
277 |
* @param text the text to parse, not null |
|
278 |
* @param formatter the formatter to use, not null |
|
279 |
* @return the parsed year, not null |
|
280 |
* @throws DateTimeParseException if the text cannot be parsed |
|
281 |
*/ |
|
282 |
public static Year parse(CharSequence text, DateTimeFormatter formatter) { |
|
283 |
Objects.requireNonNull(formatter, "formatter"); |
|
284 |
return formatter.parse(text, Year::from); |
|
285 |
} |
|
286 |
||
287 |
//------------------------------------------------------------------------- |
|
288 |
/** |
|
289 |
* Checks if the year is a leap year, according to the ISO proleptic |
|
290 |
* calendar system rules. |
|
291 |
* <p> |
|
292 |
* This method applies the current rules for leap years across the whole time-line. |
|
293 |
* In general, a year is a leap year if it is divisible by four without |
|
294 |
* remainder. However, years divisible by 100, are not leap years, with |
|
295 |
* the exception of years divisible by 400 which are. |
|
296 |
* <p> |
|
297 |
* For example, 1904 is a leap year it is divisible by 4. |
|
298 |
* 1900 was not a leap year as it is divisible by 100, however 2000 was a |
|
299 |
* leap year as it is divisible by 400. |
|
300 |
* <p> |
|
301 |
* The calculation is proleptic - applying the same rules into the far future and far past. |
|
302 |
* This is historically inaccurate, but is correct for the ISO-8601 standard. |
|
303 |
* |
|
304 |
* @param year the year to check |
|
305 |
* @return true if the year is leap, false otherwise |
|
306 |
*/ |
|
307 |
public static boolean isLeap(long year) { |
|
308 |
return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0); |
|
309 |
} |
|
310 |
||
311 |
//----------------------------------------------------------------------- |
|
312 |
/** |
|
313 |
* Constructor. |
|
314 |
* |
|
315 |
* @param year the year to represent |
|
316 |
*/ |
|
317 |
private Year(int year) { |
|
318 |
this.year = year; |
|
319 |
} |
|
320 |
||
321 |
//----------------------------------------------------------------------- |
|
322 |
/** |
|
323 |
* Gets the year value. |
|
324 |
* <p> |
|
325 |
* The year returned by this method is proleptic as per {@code get(YEAR)}. |
|
326 |
* |
|
327 |
* @return the year, {@code MIN_VALUE} to {@code MAX_VALUE} |
|
328 |
*/ |
|
329 |
public int getValue() { |
|
330 |
return year; |
|
331 |
} |
|
332 |
||
333 |
//----------------------------------------------------------------------- |
|
334 |
/** |
|
335 |
* Checks if the specified field is supported. |
|
336 |
* <p> |
|
337 |
* This checks if this year can be queried for the specified field. |
|
19030 | 338 |
* If false, then calling the {@link #range(TemporalField) range}, |
339 |
* {@link #get(TemporalField) get} and {@link #with(TemporalField, long)} |
|
340 |
* methods will throw an exception. |
|
15658 | 341 |
* <p> |
342 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
343 |
* The supported fields are: |
|
344 |
* <ul> |
|
345 |
* <li>{@code YEAR_OF_ERA} |
|
346 |
* <li>{@code YEAR} |
|
347 |
* <li>{@code ERA} |
|
348 |
* </ul> |
|
349 |
* All other {@code ChronoField} instances will return false. |
|
350 |
* <p> |
|
351 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
352 |
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} |
|
353 |
* passing {@code this} as the argument. |
|
354 |
* Whether the field is supported is determined by the field. |
|
355 |
* |
|
356 |
* @param field the field to check, null returns false |
|
357 |
* @return true if the field is supported on this year, false if not |
|
358 |
*/ |
|
359 |
@Override |
|
360 |
public boolean isSupported(TemporalField field) { |
|
361 |
if (field instanceof ChronoField) { |
|
362 |
return field == YEAR || field == YEAR_OF_ERA || field == ERA; |
|
363 |
} |
|
364 |
return field != null && field.isSupportedBy(this); |
|
365 |
} |
|
366 |
||
367 |
/** |
|
19030 | 368 |
* Checks if the specified unit is supported. |
369 |
* <p> |
|
370 |
* This checks if the specified unit can be added to, or subtracted from, this date-time. |
|
371 |
* If false, then calling the {@link #plus(long, TemporalUnit)} and |
|
372 |
* {@link #minus(long, TemporalUnit) minus} methods will throw an exception. |
|
373 |
* <p> |
|
374 |
* If the unit is a {@link ChronoUnit} then the query is implemented here. |
|
375 |
* The supported units are: |
|
376 |
* <ul> |
|
377 |
* <li>{@code YEARS} |
|
378 |
* <li>{@code DECADES} |
|
379 |
* <li>{@code CENTURIES} |
|
380 |
* <li>{@code MILLENNIA} |
|
381 |
* <li>{@code ERAS} |
|
382 |
* </ul> |
|
383 |
* All other {@code ChronoUnit} instances will return false. |
|
384 |
* <p> |
|
385 |
* If the unit is not a {@code ChronoUnit}, then the result of this method |
|
386 |
* is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)} |
|
387 |
* passing {@code this} as the argument. |
|
388 |
* Whether the unit is supported is determined by the unit. |
|
389 |
* |
|
390 |
* @param unit the unit to check, null returns false |
|
391 |
* @return true if the unit can be added/subtracted, false if not |
|
392 |
*/ |
|
393 |
@Override |
|
394 |
public boolean isSupported(TemporalUnit unit) { |
|
395 |
if (unit instanceof ChronoUnit) { |
|
396 |
return unit == YEARS || unit == DECADES || unit == CENTURIES || unit == MILLENNIA || unit == ERAS; |
|
397 |
} |
|
398 |
return unit != null && unit.isSupportedBy(this); |
|
399 |
} |
|
400 |
||
401 |
//----------------------------------------------------------------------- |
|
402 |
/** |
|
15658 | 403 |
* Gets the range of valid values for the specified field. |
404 |
* <p> |
|
405 |
* The range object expresses the minimum and maximum valid values for a field. |
|
406 |
* This year is used to enhance the accuracy of the returned range. |
|
407 |
* If it is not possible to return the range, because the field is not supported |
|
408 |
* or for some other reason, an exception is thrown. |
|
409 |
* <p> |
|
410 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
411 |
* The {@link #isSupported(TemporalField) supported fields} will return |
|
412 |
* appropriate range instances. |
|
16852 | 413 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 414 |
* <p> |
415 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
416 |
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} |
|
417 |
* passing {@code this} as the argument. |
|
418 |
* Whether the range can be obtained is determined by the field. |
|
419 |
* |
|
420 |
* @param field the field to query the range for, not null |
|
421 |
* @return the range of valid values for the field, not null |
|
422 |
* @throws DateTimeException if the range for the field cannot be obtained |
|
16852 | 423 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15658 | 424 |
*/ |
425 |
@Override |
|
426 |
public ValueRange range(TemporalField field) { |
|
427 |
if (field == YEAR_OF_ERA) { |
|
428 |
return (year <= 0 ? ValueRange.of(1, MAX_VALUE + 1) : ValueRange.of(1, MAX_VALUE)); |
|
429 |
} |
|
430 |
return Temporal.super.range(field); |
|
431 |
} |
|
432 |
||
433 |
/** |
|
434 |
* Gets the value of the specified field from this year as an {@code int}. |
|
435 |
* <p> |
|
436 |
* This queries this year for the value for the specified field. |
|
437 |
* The returned value will always be within the valid range of values for the field. |
|
438 |
* If it is not possible to return the value, because the field is not supported |
|
439 |
* or for some other reason, an exception is thrown. |
|
440 |
* <p> |
|
441 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
442 |
* The {@link #isSupported(TemporalField) supported fields} will return valid |
|
443 |
* values based on this year. |
|
16852 | 444 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 445 |
* <p> |
446 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
447 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
|
448 |
* passing {@code this} as the argument. Whether the value can be obtained, |
|
449 |
* and what the value represents, is determined by the field. |
|
450 |
* |
|
451 |
* @param field the field to get, not null |
|
452 |
* @return the value for the field |
|
16852 | 453 |
* @throws DateTimeException if a value for the field cannot be obtained or |
454 |
* the value is outside the range of valid values for the field |
|
455 |
* @throws UnsupportedTemporalTypeException if the field is not supported or |
|
456 |
* the range of values exceeds an {@code int} |
|
15658 | 457 |
* @throws ArithmeticException if numeric overflow occurs |
458 |
*/ |
|
459 |
@Override // override for Javadoc |
|
460 |
public int get(TemporalField field) { |
|
461 |
return range(field).checkValidIntValue(getLong(field), field); |
|
462 |
} |
|
463 |
||
464 |
/** |
|
465 |
* Gets the value of the specified field from this year as a {@code long}. |
|
466 |
* <p> |
|
467 |
* This queries this year for the value for the specified field. |
|
468 |
* If it is not possible to return the value, because the field is not supported |
|
469 |
* or for some other reason, an exception is thrown. |
|
470 |
* <p> |
|
471 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
472 |
* The {@link #isSupported(TemporalField) supported fields} will return valid |
|
473 |
* values based on this year. |
|
16852 | 474 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 475 |
* <p> |
476 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
477 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
|
478 |
* passing {@code this} as the argument. Whether the value can be obtained, |
|
479 |
* and what the value represents, is determined by the field. |
|
480 |
* |
|
481 |
* @param field the field to get, not null |
|
482 |
* @return the value for the field |
|
483 |
* @throws DateTimeException if a value for the field cannot be obtained |
|
16852 | 484 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15658 | 485 |
* @throws ArithmeticException if numeric overflow occurs |
486 |
*/ |
|
487 |
@Override |
|
488 |
public long getLong(TemporalField field) { |
|
489 |
if (field instanceof ChronoField) { |
|
490 |
switch ((ChronoField) field) { |
|
491 |
case YEAR_OF_ERA: return (year < 1 ? 1 - year : year); |
|
492 |
case YEAR: return year; |
|
493 |
case ERA: return (year < 1 ? 0 : 1); |
|
494 |
} |
|
19030 | 495 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
15658 | 496 |
} |
497 |
return field.getFrom(this); |
|
498 |
} |
|
499 |
||
500 |
//----------------------------------------------------------------------- |
|
501 |
/** |
|
502 |
* Checks if the year is a leap year, according to the ISO proleptic |
|
503 |
* calendar system rules. |
|
504 |
* <p> |
|
505 |
* This method applies the current rules for leap years across the whole time-line. |
|
506 |
* In general, a year is a leap year if it is divisible by four without |
|
507 |
* remainder. However, years divisible by 100, are not leap years, with |
|
508 |
* the exception of years divisible by 400 which are. |
|
509 |
* <p> |
|
510 |
* For example, 1904 is a leap year it is divisible by 4. |
|
511 |
* 1900 was not a leap year as it is divisible by 100, however 2000 was a |
|
512 |
* leap year as it is divisible by 400. |
|
513 |
* <p> |
|
514 |
* The calculation is proleptic - applying the same rules into the far future and far past. |
|
515 |
* This is historically inaccurate, but is correct for the ISO-8601 standard. |
|
516 |
* |
|
517 |
* @return true if the year is leap, false otherwise |
|
518 |
*/ |
|
519 |
public boolean isLeap() { |
|
520 |
return Year.isLeap(year); |
|
521 |
} |
|
522 |
||
523 |
/** |
|
524 |
* Checks if the month-day is valid for this year. |
|
525 |
* <p> |
|
526 |
* This method checks whether this year and the input month and day form |
|
527 |
* a valid date. |
|
528 |
* |
|
529 |
* @param monthDay the month-day to validate, null returns false |
|
530 |
* @return true if the month and day are valid for this year |
|
531 |
*/ |
|
532 |
public boolean isValidMonthDay(MonthDay monthDay) { |
|
533 |
return monthDay != null && monthDay.isValidYear(year); |
|
534 |
} |
|
535 |
||
536 |
/** |
|
537 |
* Gets the length of this year in days. |
|
538 |
* |
|
539 |
* @return the length of this year in days, 365 or 366 |
|
540 |
*/ |
|
541 |
public int length() { |
|
542 |
return isLeap() ? 366 : 365; |
|
543 |
} |
|
544 |
||
545 |
//----------------------------------------------------------------------- |
|
546 |
/** |
|
547 |
* Returns an adjusted copy of this year. |
|
548 |
* <p> |
|
549 |
* This returns a {@code Year}, based on this one, with the year adjusted. |
|
550 |
* The adjustment takes place using the specified adjuster strategy object. |
|
551 |
* Read the documentation of the adjuster to understand what adjustment will be made. |
|
552 |
* <p> |
|
553 |
* The result of this method is obtained by invoking the |
|
554 |
* {@link TemporalAdjuster#adjustInto(Temporal)} method on the |
|
555 |
* specified adjuster passing {@code this} as the argument. |
|
556 |
* <p> |
|
557 |
* This instance is immutable and unaffected by this method call. |
|
558 |
* |
|
559 |
* @param adjuster the adjuster to use, not null |
|
560 |
* @return a {@code Year} based on {@code this} with the adjustment made, not null |
|
561 |
* @throws DateTimeException if the adjustment cannot be made |
|
562 |
* @throws ArithmeticException if numeric overflow occurs |
|
563 |
*/ |
|
564 |
@Override |
|
565 |
public Year with(TemporalAdjuster adjuster) { |
|
566 |
return (Year) adjuster.adjustInto(this); |
|
567 |
} |
|
568 |
||
569 |
/** |
|
570 |
* Returns a copy of this year with the specified field set to a new value. |
|
571 |
* <p> |
|
572 |
* This returns a {@code Year}, based on this one, with the value |
|
573 |
* for the specified field changed. |
|
574 |
* If it is not possible to set the value, because the field is not supported or for |
|
575 |
* some other reason, an exception is thrown. |
|
576 |
* <p> |
|
577 |
* If the field is a {@link ChronoField} then the adjustment is implemented here. |
|
578 |
* The supported fields behave as follows: |
|
579 |
* <ul> |
|
580 |
* <li>{@code YEAR_OF_ERA} - |
|
581 |
* Returns a {@code Year} with the specified year-of-era |
|
582 |
* The era will be unchanged. |
|
583 |
* <li>{@code YEAR} - |
|
584 |
* Returns a {@code Year} with the specified year. |
|
585 |
* This completely replaces the date and is equivalent to {@link #of(int)}. |
|
586 |
* <li>{@code ERA} - |
|
587 |
* Returns a {@code Year} with the specified era. |
|
588 |
* The year-of-era will be unchanged. |
|
589 |
* </ul> |
|
590 |
* <p> |
|
591 |
* In all cases, if the new value is outside the valid range of values for the field |
|
592 |
* then a {@code DateTimeException} will be thrown. |
|
593 |
* <p> |
|
16852 | 594 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 595 |
* <p> |
596 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
597 |
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} |
|
598 |
* passing {@code this} as the argument. In this case, the field determines |
|
599 |
* whether and how to adjust the instant. |
|
600 |
* <p> |
|
601 |
* This instance is immutable and unaffected by this method call. |
|
602 |
* |
|
603 |
* @param field the field to set in the result, not null |
|
604 |
* @param newValue the new value of the field in the result |
|
605 |
* @return a {@code Year} based on {@code this} with the specified field set, not null |
|
606 |
* @throws DateTimeException if the field cannot be set |
|
16852 | 607 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15658 | 608 |
* @throws ArithmeticException if numeric overflow occurs |
609 |
*/ |
|
610 |
@Override |
|
611 |
public Year with(TemporalField field, long newValue) { |
|
612 |
if (field instanceof ChronoField) { |
|
613 |
ChronoField f = (ChronoField) field; |
|
614 |
f.checkValidValue(newValue); |
|
615 |
switch (f) { |
|
616 |
case YEAR_OF_ERA: return Year.of((int) (year < 1 ? 1 - newValue : newValue)); |
|
617 |
case YEAR: return Year.of((int) newValue); |
|
618 |
case ERA: return (getLong(ERA) == newValue ? this : Year.of(1 - year)); |
|
619 |
} |
|
19030 | 620 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
15658 | 621 |
} |
622 |
return field.adjustInto(this, newValue); |
|
623 |
} |
|
624 |
||
625 |
//----------------------------------------------------------------------- |
|
626 |
/** |
|
627 |
* Returns a copy of this year with the specified amount added. |
|
628 |
* <p> |
|
629 |
* This returns a {@code Year}, based on this one, with the specified amount added. |
|
630 |
* The amount is typically {@link Period} but may be any other type implementing |
|
631 |
* the {@link TemporalAmount} interface. |
|
632 |
* <p> |
|
633 |
* The calculation is delegated to the amount object by calling |
|
634 |
* {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free |
|
635 |
* to implement the addition in any way it wishes, however it typically |
|
636 |
* calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation |
|
637 |
* of the amount implementation to determine if it can be successfully added. |
|
638 |
* <p> |
|
639 |
* This instance is immutable and unaffected by this method call. |
|
640 |
* |
|
641 |
* @param amountToAdd the amount to add, not null |
|
642 |
* @return a {@code Year} based on this year with the addition made, not null |
|
643 |
* @throws DateTimeException if the addition cannot be made |
|
644 |
* @throws ArithmeticException if numeric overflow occurs |
|
645 |
*/ |
|
646 |
@Override |
|
647 |
public Year plus(TemporalAmount amountToAdd) { |
|
648 |
return (Year) amountToAdd.addTo(this); |
|
649 |
} |
|
650 |
||
651 |
/** |
|
652 |
* Returns a copy of this year with the specified amount added. |
|
653 |
* <p> |
|
654 |
* This returns a {@code Year}, based on this one, with the amount |
|
655 |
* in terms of the unit added. If it is not possible to add the amount, because the |
|
656 |
* unit is not supported or for some other reason, an exception is thrown. |
|
657 |
* <p> |
|
658 |
* If the field is a {@link ChronoUnit} then the addition is implemented here. |
|
659 |
* The supported fields behave as follows: |
|
660 |
* <ul> |
|
661 |
* <li>{@code YEARS} - |
|
662 |
* Returns a {@code Year} with the specified number of years added. |
|
663 |
* This is equivalent to {@link #plusYears(long)}. |
|
664 |
* <li>{@code DECADES} - |
|
665 |
* Returns a {@code Year} with the specified number of decades added. |
|
666 |
* This is equivalent to calling {@link #plusYears(long)} with the amount |
|
667 |
* multiplied by 10. |
|
668 |
* <li>{@code CENTURIES} - |
|
669 |
* Returns a {@code Year} with the specified number of centuries added. |
|
670 |
* This is equivalent to calling {@link #plusYears(long)} with the amount |
|
671 |
* multiplied by 100. |
|
672 |
* <li>{@code MILLENNIA} - |
|
673 |
* Returns a {@code Year} with the specified number of millennia added. |
|
674 |
* This is equivalent to calling {@link #plusYears(long)} with the amount |
|
675 |
* multiplied by 1,000. |
|
676 |
* <li>{@code ERAS} - |
|
677 |
* Returns a {@code Year} with the specified number of eras added. |
|
678 |
* Only two eras are supported so the amount must be one, zero or minus one. |
|
679 |
* If the amount is non-zero then the year is changed such that the year-of-era |
|
680 |
* is unchanged. |
|
681 |
* </ul> |
|
682 |
* <p> |
|
16852 | 683 |
* All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 684 |
* <p> |
685 |
* If the field is not a {@code ChronoUnit}, then the result of this method |
|
686 |
* is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)} |
|
687 |
* passing {@code this} as the argument. In this case, the unit determines |
|
688 |
* whether and how to perform the addition. |
|
689 |
* <p> |
|
690 |
* This instance is immutable and unaffected by this method call. |
|
691 |
* |
|
692 |
* @param amountToAdd the amount of the unit to add to the result, may be negative |
|
693 |
* @param unit the unit of the amount to add, not null |
|
694 |
* @return a {@code Year} based on this year with the specified amount added, not null |
|
695 |
* @throws DateTimeException if the addition cannot be made |
|
16852 | 696 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 697 |
* @throws ArithmeticException if numeric overflow occurs |
698 |
*/ |
|
699 |
@Override |
|
700 |
public Year plus(long amountToAdd, TemporalUnit unit) { |
|
701 |
if (unit instanceof ChronoUnit) { |
|
702 |
switch ((ChronoUnit) unit) { |
|
703 |
case YEARS: return plusYears(amountToAdd); |
|
704 |
case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10)); |
|
705 |
case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100)); |
|
706 |
case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000)); |
|
707 |
case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd)); |
|
708 |
} |
|
19030 | 709 |
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); |
15658 | 710 |
} |
711 |
return unit.addTo(this, amountToAdd); |
|
712 |
} |
|
713 |
||
714 |
/** |
|
715 |
* Returns a copy of this year with the specified number of years added. |
|
716 |
* <p> |
|
717 |
* This instance is immutable and unaffected by this method call. |
|
718 |
* |
|
719 |
* @param yearsToAdd the years to add, may be negative |
|
720 |
* @return a {@code Year} based on this year with the period added, not null |
|
721 |
* @throws DateTimeException if the result exceeds the supported year range |
|
722 |
*/ |
|
723 |
public Year plusYears(long yearsToAdd) { |
|
724 |
if (yearsToAdd == 0) { |
|
725 |
return this; |
|
726 |
} |
|
727 |
return of(YEAR.checkValidIntValue(year + yearsToAdd)); // overflow safe |
|
728 |
} |
|
729 |
||
730 |
//----------------------------------------------------------------------- |
|
731 |
/** |
|
732 |
* Returns a copy of this year with the specified amount subtracted. |
|
733 |
* <p> |
|
734 |
* This returns a {@code Year}, based on this one, with the specified amount subtracted. |
|
735 |
* The amount is typically {@link Period} but may be any other type implementing |
|
736 |
* the {@link TemporalAmount} interface. |
|
737 |
* <p> |
|
738 |
* The calculation is delegated to the amount object by calling |
|
739 |
* {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free |
|
740 |
* to implement the subtraction in any way it wishes, however it typically |
|
741 |
* calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation |
|
742 |
* of the amount implementation to determine if it can be successfully subtracted. |
|
743 |
* <p> |
|
744 |
* This instance is immutable and unaffected by this method call. |
|
745 |
* |
|
746 |
* @param amountToSubtract the amount to subtract, not null |
|
747 |
* @return a {@code Year} based on this year with the subtraction made, not null |
|
748 |
* @throws DateTimeException if the subtraction cannot be made |
|
749 |
* @throws ArithmeticException if numeric overflow occurs |
|
750 |
*/ |
|
751 |
@Override |
|
752 |
public Year minus(TemporalAmount amountToSubtract) { |
|
753 |
return (Year) amountToSubtract.subtractFrom(this); |
|
754 |
} |
|
755 |
||
756 |
/** |
|
16852 | 757 |
* Returns a copy of this year with the specified amount subtracted. |
15658 | 758 |
* <p> |
16852 | 759 |
* This returns a {@code Year}, based on this one, with the amount |
15658 | 760 |
* in terms of the unit subtracted. If it is not possible to subtract the amount, |
761 |
* because the unit is not supported or for some other reason, an exception is thrown. |
|
762 |
* <p> |
|
763 |
* This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated. |
|
764 |
* See that method for a full description of how addition, and thus subtraction, works. |
|
765 |
* <p> |
|
766 |
* This instance is immutable and unaffected by this method call. |
|
767 |
* |
|
768 |
* @param amountToSubtract the amount of the unit to subtract from the result, may be negative |
|
769 |
* @param unit the unit of the amount to subtract, not null |
|
16852 | 770 |
* @return a {@code Year} based on this year with the specified amount subtracted, not null |
15658 | 771 |
* @throws DateTimeException if the subtraction cannot be made |
16852 | 772 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 773 |
* @throws ArithmeticException if numeric overflow occurs |
774 |
*/ |
|
775 |
@Override |
|
776 |
public Year minus(long amountToSubtract, TemporalUnit unit) { |
|
777 |
return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
|
778 |
} |
|
779 |
||
780 |
/** |
|
781 |
* Returns a copy of this year with the specified number of years subtracted. |
|
782 |
* <p> |
|
783 |
* This instance is immutable and unaffected by this method call. |
|
784 |
* |
|
785 |
* @param yearsToSubtract the years to subtract, may be negative |
|
786 |
* @return a {@code Year} based on this year with the period subtracted, not null |
|
787 |
* @throws DateTimeException if the result exceeds the supported year range |
|
788 |
*/ |
|
789 |
public Year minusYears(long yearsToSubtract) { |
|
790 |
return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract)); |
|
791 |
} |
|
792 |
||
793 |
//----------------------------------------------------------------------- |
|
794 |
/** |
|
795 |
* Queries this year using the specified query. |
|
796 |
* <p> |
|
797 |
* This queries this year using the specified query strategy object. |
|
798 |
* The {@code TemporalQuery} object defines the logic to be used to |
|
799 |
* obtain the result. Read the documentation of the query to understand |
|
800 |
* what the result of this method will be. |
|
801 |
* <p> |
|
802 |
* The result of this method is obtained by invoking the |
|
803 |
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the |
|
804 |
* specified query passing {@code this} as the argument. |
|
805 |
* |
|
806 |
* @param <R> the type of the result |
|
807 |
* @param query the query to invoke, not null |
|
808 |
* @return the query result, null may be returned (defined by the query) |
|
809 |
* @throws DateTimeException if unable to query (defined by the query) |
|
810 |
* @throws ArithmeticException if numeric overflow occurs (defined by the query) |
|
811 |
*/ |
|
812 |
@SuppressWarnings("unchecked") |
|
813 |
@Override |
|
814 |
public <R> R query(TemporalQuery<R> query) { |
|
20795 | 815 |
if (query == TemporalQueries.chronology()) { |
15658 | 816 |
return (R) IsoChronology.INSTANCE; |
20795 | 817 |
} else if (query == TemporalQueries.precision()) { |
15658 | 818 |
return (R) YEARS; |
819 |
} |
|
820 |
return Temporal.super.query(query); |
|
821 |
} |
|
822 |
||
823 |
/** |
|
824 |
* Adjusts the specified temporal object to have this year. |
|
825 |
* <p> |
|
826 |
* This returns a temporal object of the same observable type as the input |
|
827 |
* with the year changed to be the same as this. |
|
828 |
* <p> |
|
829 |
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} |
|
830 |
* passing {@link ChronoField#YEAR} as the field. |
|
831 |
* If the specified temporal object does not use the ISO calendar system then |
|
832 |
* a {@code DateTimeException} is thrown. |
|
833 |
* <p> |
|
834 |
* In most cases, it is clearer to reverse the calling pattern by using |
|
835 |
* {@link Temporal#with(TemporalAdjuster)}: |
|
836 |
* <pre> |
|
837 |
* // these two lines are equivalent, but the second approach is recommended |
|
838 |
* temporal = thisYear.adjustInto(temporal); |
|
839 |
* temporal = temporal.with(thisYear); |
|
840 |
* </pre> |
|
841 |
* <p> |
|
842 |
* This instance is immutable and unaffected by this method call. |
|
843 |
* |
|
844 |
* @param temporal the target object to be adjusted, not null |
|
845 |
* @return the adjusted object, not null |
|
846 |
* @throws DateTimeException if unable to make the adjustment |
|
847 |
* @throws ArithmeticException if numeric overflow occurs |
|
848 |
*/ |
|
849 |
@Override |
|
850 |
public Temporal adjustInto(Temporal temporal) { |
|
851 |
if (Chronology.from(temporal).equals(IsoChronology.INSTANCE) == false) { |
|
852 |
throw new DateTimeException("Adjustment only supported on ISO date-time"); |
|
853 |
} |
|
854 |
return temporal.with(YEAR, year); |
|
855 |
} |
|
856 |
||
857 |
/** |
|
17474 | 858 |
* Calculates the amount of time until another year in terms of the specified unit. |
15658 | 859 |
* <p> |
17474 | 860 |
* This calculates the amount of time between two {@code Year} |
861 |
* objects in terms of a single {@code TemporalUnit}. |
|
15658 | 862 |
* The start and end points are {@code this} and the specified year. |
863 |
* The result will be negative if the end is before the start. |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
864 |
* The {@code Temporal} passed to this method is converted to a |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
865 |
* {@code Year} using {@link #from(TemporalAccessor)}. |
15658 | 866 |
* For example, the period in decades between two year can be calculated |
19030 | 867 |
* using {@code startYear.until(endYear, DECADES)}. |
15658 | 868 |
* <p> |
869 |
* The calculation returns a whole number, representing the number of |
|
870 |
* complete units between the two years. |
|
871 |
* For example, the period in decades between 2012 and 2031 |
|
872 |
* will only be one decade as it is one year short of two decades. |
|
873 |
* <p> |
|
874 |
* There are two equivalent ways of using this method. |
|
875 |
* The first is to invoke this method. |
|
876 |
* The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: |
|
877 |
* <pre> |
|
878 |
* // these two lines are equivalent |
|
19030 | 879 |
* amount = start.until(end, YEARS); |
15658 | 880 |
* amount = YEARS.between(start, end); |
881 |
* </pre> |
|
882 |
* The choice should be made based on which makes the code more readable. |
|
883 |
* <p> |
|
884 |
* The calculation is implemented in this method for {@link ChronoUnit}. |
|
885 |
* The units {@code YEARS}, {@code DECADES}, {@code CENTURIES}, |
|
886 |
* {@code MILLENNIA} and {@code ERAS} are supported. |
|
887 |
* Other {@code ChronoUnit} values will throw an exception. |
|
888 |
* <p> |
|
889 |
* If the unit is not a {@code ChronoUnit}, then the result of this method |
|
890 |
* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
891 |
* passing {@code this} as the first argument and the converted input temporal |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
892 |
* as the second argument. |
15658 | 893 |
* <p> |
894 |
* This instance is immutable and unaffected by this method call. |
|
895 |
* |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
896 |
* @param endExclusive the end date, exclusive, which is converted to a {@code Year}, not null |
17474 | 897 |
* @param unit the unit to measure the amount in, not null |
898 |
* @return the amount of time between this year and the end year |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
899 |
* @throws DateTimeException if the amount cannot be calculated, or the end |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
900 |
* temporal cannot be converted to a {@code Year} |
16852 | 901 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 902 |
* @throws ArithmeticException if numeric overflow occurs |
903 |
*/ |
|
904 |
@Override |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
905 |
public long until(Temporal endExclusive, TemporalUnit unit) { |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
906 |
Year end = Year.from(endExclusive); |
15658 | 907 |
if (unit instanceof ChronoUnit) { |
908 |
long yearsUntil = ((long) end.year) - year; // no overflow |
|
909 |
switch ((ChronoUnit) unit) { |
|
910 |
case YEARS: return yearsUntil; |
|
911 |
case DECADES: return yearsUntil / 10; |
|
912 |
case CENTURIES: return yearsUntil / 100; |
|
913 |
case MILLENNIA: return yearsUntil / 1000; |
|
914 |
case ERAS: return end.getLong(ERA) - getLong(ERA); |
|
915 |
} |
|
19030 | 916 |
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); |
15658 | 917 |
} |
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
918 |
return unit.between(this, end); |
15658 | 919 |
} |
920 |
||
16852 | 921 |
/** |
922 |
* Formats this year using the specified formatter. |
|
923 |
* <p> |
|
924 |
* This year will be passed to the formatter to produce a string. |
|
925 |
* |
|
926 |
* @param formatter the formatter to use, not null |
|
927 |
* @return the formatted year string, not null |
|
928 |
* @throws DateTimeException if an error occurs during printing |
|
929 |
*/ |
|
930 |
public String format(DateTimeFormatter formatter) { |
|
931 |
Objects.requireNonNull(formatter, "formatter"); |
|
932 |
return formatter.format(this); |
|
933 |
} |
|
934 |
||
15658 | 935 |
//----------------------------------------------------------------------- |
936 |
/** |
|
937 |
* Combines this year with a day-of-year to create a {@code LocalDate}. |
|
938 |
* <p> |
|
939 |
* This returns a {@code LocalDate} formed from this year and the specified day-of-year. |
|
940 |
* <p> |
|
941 |
* The day-of-year value 366 is only valid in a leap year. |
|
942 |
* |
|
943 |
* @param dayOfYear the day-of-year to use, not null |
|
944 |
* @return the local date formed from this year and the specified date of year, not null |
|
945 |
* @throws DateTimeException if the day of year is zero or less, 366 or greater or equal |
|
946 |
* to 366 and this is not a leap year |
|
947 |
*/ |
|
948 |
public LocalDate atDay(int dayOfYear) { |
|
949 |
return LocalDate.ofYearDay(year, dayOfYear); |
|
950 |
} |
|
951 |
||
952 |
/** |
|
953 |
* Combines this year with a month to create a {@code YearMonth}. |
|
954 |
* <p> |
|
955 |
* This returns a {@code YearMonth} formed from this year and the specified month. |
|
956 |
* All possible combinations of year and month are valid. |
|
957 |
* <p> |
|
958 |
* This method can be used as part of a chain to produce a date: |
|
959 |
* <pre> |
|
960 |
* LocalDate date = year.atMonth(month).atDay(day); |
|
961 |
* </pre> |
|
962 |
* |
|
963 |
* @param month the month-of-year to use, not null |
|
964 |
* @return the year-month formed from this year and the specified month, not null |
|
965 |
*/ |
|
966 |
public YearMonth atMonth(Month month) { |
|
967 |
return YearMonth.of(year, month); |
|
968 |
} |
|
969 |
||
970 |
/** |
|
971 |
* Combines this year with a month to create a {@code YearMonth}. |
|
972 |
* <p> |
|
973 |
* This returns a {@code YearMonth} formed from this year and the specified month. |
|
974 |
* All possible combinations of year and month are valid. |
|
975 |
* <p> |
|
976 |
* This method can be used as part of a chain to produce a date: |
|
977 |
* <pre> |
|
978 |
* LocalDate date = year.atMonth(month).atDay(day); |
|
979 |
* </pre> |
|
980 |
* |
|
981 |
* @param month the month-of-year to use, from 1 (January) to 12 (December) |
|
982 |
* @return the year-month formed from this year and the specified month, not null |
|
983 |
* @throws DateTimeException if the month is invalid |
|
984 |
*/ |
|
985 |
public YearMonth atMonth(int month) { |
|
986 |
return YearMonth.of(year, month); |
|
987 |
} |
|
988 |
||
989 |
/** |
|
990 |
* Combines this year with a month-day to create a {@code LocalDate}. |
|
991 |
* <p> |
|
992 |
* This returns a {@code LocalDate} formed from this year and the specified month-day. |
|
993 |
* <p> |
|
994 |
* A month-day of February 29th will be adjusted to February 28th in the resulting |
|
995 |
* date if the year is not a leap year. |
|
996 |
* |
|
997 |
* @param monthDay the month-day to use, not null |
|
998 |
* @return the local date formed from this year and the specified month-day, not null |
|
999 |
*/ |
|
1000 |
public LocalDate atMonthDay(MonthDay monthDay) { |
|
1001 |
return monthDay.atYear(year); |
|
1002 |
} |
|
1003 |
||
1004 |
//----------------------------------------------------------------------- |
|
1005 |
/** |
|
1006 |
* Compares this year to another year. |
|
1007 |
* <p> |
|
1008 |
* The comparison is based on the value of the year. |
|
1009 |
* It is "consistent with equals", as defined by {@link Comparable}. |
|
1010 |
* |
|
1011 |
* @param other the other year to compare to, not null |
|
1012 |
* @return the comparator value, negative if less, positive if greater |
|
1013 |
*/ |
|
1014 |
@Override |
|
1015 |
public int compareTo(Year other) { |
|
1016 |
return year - other.year; |
|
1017 |
} |
|
1018 |
||
1019 |
/** |
|
1020 |
* Is this year after the specified year. |
|
1021 |
* |
|
1022 |
* @param other the other year to compare to, not null |
|
1023 |
* @return true if this is after the specified year |
|
1024 |
*/ |
|
1025 |
public boolean isAfter(Year other) { |
|
1026 |
return year > other.year; |
|
1027 |
} |
|
1028 |
||
1029 |
/** |
|
1030 |
* Is this year before the specified year. |
|
1031 |
* |
|
1032 |
* @param other the other year to compare to, not null |
|
1033 |
* @return true if this point is before the specified year |
|
1034 |
*/ |
|
1035 |
public boolean isBefore(Year other) { |
|
1036 |
return year < other.year; |
|
1037 |
} |
|
1038 |
||
1039 |
//----------------------------------------------------------------------- |
|
1040 |
/** |
|
1041 |
* Checks if this year is equal to another year. |
|
1042 |
* <p> |
|
1043 |
* The comparison is based on the time-line position of the years. |
|
1044 |
* |
|
1045 |
* @param obj the object to check, null returns false |
|
1046 |
* @return true if this is equal to the other year |
|
1047 |
*/ |
|
1048 |
@Override |
|
1049 |
public boolean equals(Object obj) { |
|
1050 |
if (this == obj) { |
|
1051 |
return true; |
|
1052 |
} |
|
1053 |
if (obj instanceof Year) { |
|
1054 |
return year == ((Year) obj).year; |
|
1055 |
} |
|
1056 |
return false; |
|
1057 |
} |
|
1058 |
||
1059 |
/** |
|
1060 |
* A hash code for this year. |
|
1061 |
* |
|
1062 |
* @return a suitable hash code |
|
1063 |
*/ |
|
1064 |
@Override |
|
1065 |
public int hashCode() { |
|
1066 |
return year; |
|
1067 |
} |
|
1068 |
||
1069 |
//----------------------------------------------------------------------- |
|
1070 |
/** |
|
1071 |
* Outputs this year as a {@code String}. |
|
1072 |
* |
|
1073 |
* @return a string representation of this year, not null |
|
1074 |
*/ |
|
1075 |
@Override |
|
1076 |
public String toString() { |
|
1077 |
return Integer.toString(year); |
|
1078 |
} |
|
1079 |
||
1080 |
//----------------------------------------------------------------------- |
|
1081 |
/** |
|
1082 |
* Writes the object using a |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1083 |
* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>. |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1084 |
* @serialData |
15658 | 1085 |
* <pre> |
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1086 |
* out.writeByte(11); // identifies a Year |
15658 | 1087 |
* out.writeInt(year); |
1088 |
* </pre> |
|
1089 |
* |
|
1090 |
* @return the instance of {@code Ser}, not null |
|
1091 |
*/ |
|
1092 |
private Object writeReplace() { |
|
1093 |
return new Ser(Ser.YEAR_TYPE, this); |
|
1094 |
} |
|
1095 |
||
1096 |
/** |
|
1097 |
* Defend against malicious streams. |
|
1098 |
* @return never |
|
1099 |
* @throws InvalidObjectException always |
|
1100 |
*/ |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1101 |
private Object readResolve() throws InvalidObjectException { |
15658 | 1102 |
throw new InvalidObjectException("Deserialization via serialization delegate"); |
1103 |
} |
|
1104 |
||
1105 |
void writeExternal(DataOutput out) throws IOException { |
|
1106 |
out.writeInt(year); |
|
1107 |
} |
|
1108 |
||
1109 |
static Year readExternal(DataInput in) throws IOException { |
|
1110 |
return Year.of(in.readInt()); |
|
1111 |
} |
|
1112 |
||
1113 |
} |