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