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