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.DAY_OF_MONTH; |
|
65 |
import static java.time.temporal.ChronoField.MONTH_OF_YEAR; |
|
66 |
||
67 |
import java.io.DataInput; |
|
68 |
import java.io.DataOutput; |
|
69 |
import java.io.IOException; |
|
70 |
import java.io.InvalidObjectException; |
|
71 |
import java.io.Serializable; |
|
72 |
import java.time.chrono.Chronology; |
|
73 |
import java.time.chrono.IsoChronology; |
|
74 |
import java.time.format.DateTimeFormatter; |
|
75 |
import java.time.format.DateTimeFormatterBuilder; |
|
76 |
import java.time.format.DateTimeParseException; |
|
77 |
import java.time.temporal.ChronoField; |
|
78 |
import java.time.temporal.Temporal; |
|
79 |
import java.time.temporal.TemporalAccessor; |
|
80 |
import java.time.temporal.TemporalAdjuster; |
|
81 |
import java.time.temporal.TemporalField; |
|
20795 | 82 |
import java.time.temporal.TemporalQueries; |
15658 | 83 |
import java.time.temporal.TemporalQuery; |
16852 | 84 |
import java.time.temporal.UnsupportedTemporalTypeException; |
15658 | 85 |
import java.time.temporal.ValueRange; |
86 |
import java.util.Objects; |
|
87 |
||
88 |
/** |
|
89 |
* A month-day in the ISO-8601 calendar system, such as {@code --12-03}. |
|
90 |
* <p> |
|
91 |
* {@code MonthDay} is an immutable date-time object that represents the combination |
|
92 |
* of a year and month. Any field that can be derived from a month and day, such as |
|
93 |
* quarter-of-year, can be obtained. |
|
94 |
* <p> |
|
95 |
* This class does not store or represent a year, time or time-zone. |
|
96 |
* For example, the value "December 3rd" can be stored in a {@code MonthDay}. |
|
97 |
* <p> |
|
98 |
* Since a {@code MonthDay} does not possess a year, the leap day of |
|
99 |
* February 29th is considered valid. |
|
100 |
* <p> |
|
101 |
* This class implements {@link TemporalAccessor} rather than {@link Temporal}. |
|
102 |
* This is because it is not possible to define whether February 29th is valid or not |
|
103 |
* without external information, preventing the implementation of plus/minus. |
|
104 |
* Related to this, {@code MonthDay} only provides access to query and set the fields |
|
105 |
* {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH}. |
|
106 |
* <p> |
|
107 |
* The ISO-8601 calendar system is the modern civil calendar system used today |
|
108 |
* in most of the world. It is equivalent to the proleptic Gregorian calendar |
|
109 |
* system, in which today's rules for leap years are applied for all time. |
|
110 |
* For most applications written today, the ISO-8601 rules are entirely suitable. |
|
111 |
* However, any application that makes use of historical dates, and requires them |
|
112 |
* to be accurate will find the ISO-8601 approach unsuitable. |
|
113 |
* |
|
17474 | 114 |
* @implSpec |
15658 | 115 |
* This class is immutable and thread-safe. |
116 |
* |
|
117 |
* @since 1.8 |
|
118 |
*/ |
|
119 |
public final class MonthDay |
|
120 |
implements TemporalAccessor, TemporalAdjuster, Comparable<MonthDay>, Serializable { |
|
121 |
||
122 |
/** |
|
123 |
* Serialization version. |
|
124 |
*/ |
|
125 |
private static final long serialVersionUID = -939150713474957432L; |
|
126 |
/** |
|
127 |
* Parser. |
|
128 |
*/ |
|
129 |
private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder() |
|
130 |
.appendLiteral("--") |
|
131 |
.appendValue(MONTH_OF_YEAR, 2) |
|
132 |
.appendLiteral('-') |
|
133 |
.appendValue(DAY_OF_MONTH, 2) |
|
134 |
.toFormatter(); |
|
135 |
||
136 |
/** |
|
137 |
* The month-of-year, not null. |
|
138 |
*/ |
|
139 |
private final int month; |
|
140 |
/** |
|
141 |
* The day-of-month. |
|
142 |
*/ |
|
143 |
private final int day; |
|
144 |
||
145 |
//----------------------------------------------------------------------- |
|
146 |
/** |
|
147 |
* Obtains the current month-day from the system clock in the default time-zone. |
|
148 |
* <p> |
|
149 |
* This will query the {@link java.time.Clock#systemDefaultZone() system clock} in the default |
|
150 |
* time-zone to obtain the current month-day. |
|
151 |
* <p> |
|
152 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
153 |
* because the clock is hard-coded. |
|
154 |
* |
|
155 |
* @return the current month-day using the system clock and default time-zone, not null |
|
156 |
*/ |
|
157 |
public static MonthDay now() { |
|
158 |
return now(Clock.systemDefaultZone()); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Obtains the current month-day from the system clock in the specified time-zone. |
|
163 |
* <p> |
|
164 |
* This will query the {@link Clock#system(java.time.ZoneId) system clock} to obtain the current month-day. |
|
165 |
* Specifying the time-zone avoids dependence on the default time-zone. |
|
166 |
* <p> |
|
167 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
168 |
* because the clock is hard-coded. |
|
169 |
* |
|
170 |
* @param zone the zone ID to use, not null |
|
171 |
* @return the current month-day using the system clock, not null |
|
172 |
*/ |
|
173 |
public static MonthDay now(ZoneId zone) { |
|
174 |
return now(Clock.system(zone)); |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Obtains the current month-day from the specified clock. |
|
179 |
* <p> |
|
180 |
* This will query the specified clock to obtain the current month-day. |
|
181 |
* Using this method allows the use of an alternate clock for testing. |
|
182 |
* The alternate clock may be introduced using {@link Clock dependency injection}. |
|
183 |
* |
|
184 |
* @param clock the clock to use, not null |
|
185 |
* @return the current month-day, not null |
|
186 |
*/ |
|
187 |
public static MonthDay now(Clock clock) { |
|
188 |
final LocalDate now = LocalDate.now(clock); // called once |
|
189 |
return MonthDay.of(now.getMonth(), now.getDayOfMonth()); |
|
190 |
} |
|
191 |
||
192 |
//----------------------------------------------------------------------- |
|
193 |
/** |
|
194 |
* Obtains an instance of {@code MonthDay}. |
|
195 |
* <p> |
|
196 |
* The day-of-month must be valid for the month within a leap year. |
|
197 |
* Hence, for February, day 29 is valid. |
|
198 |
* <p> |
|
199 |
* For example, passing in April and day 31 will throw an exception, as |
|
200 |
* there can never be April 31st in any year. By contrast, passing in |
|
201 |
* February 29th is permitted, as that month-day can sometimes be valid. |
|
202 |
* |
|
203 |
* @param month the month-of-year to represent, not null |
|
204 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31 |
|
205 |
* @return the month-day, not null |
|
206 |
* @throws DateTimeException if the value of any field is out of range, |
|
207 |
* or if the day-of-month is invalid for the month |
|
208 |
*/ |
|
209 |
public static MonthDay of(Month month, int dayOfMonth) { |
|
210 |
Objects.requireNonNull(month, "month"); |
|
211 |
DAY_OF_MONTH.checkValidValue(dayOfMonth); |
|
212 |
if (dayOfMonth > month.maxLength()) { |
|
213 |
throw new DateTimeException("Illegal value for DayOfMonth field, value " + dayOfMonth + |
|
214 |
" is not valid for month " + month.name()); |
|
215 |
} |
|
216 |
return new MonthDay(month.getValue(), dayOfMonth); |
|
217 |
} |
|
218 |
||
219 |
/** |
|
220 |
* Obtains an instance of {@code MonthDay}. |
|
221 |
* <p> |
|
222 |
* The day-of-month must be valid for the month within a leap year. |
|
223 |
* Hence, for month 2 (February), day 29 is valid. |
|
224 |
* <p> |
|
225 |
* For example, passing in month 4 (April) and day 31 will throw an exception, as |
|
226 |
* there can never be April 31st in any year. By contrast, passing in |
|
227 |
* February 29th is permitted, as that month-day can sometimes be valid. |
|
228 |
* |
|
229 |
* @param month the month-of-year to represent, from 1 (January) to 12 (December) |
|
230 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31 |
|
231 |
* @return the month-day, not null |
|
232 |
* @throws DateTimeException if the value of any field is out of range, |
|
233 |
* or if the day-of-month is invalid for the month |
|
234 |
*/ |
|
235 |
public static MonthDay of(int month, int dayOfMonth) { |
|
236 |
return of(Month.of(month), dayOfMonth); |
|
237 |
} |
|
238 |
||
239 |
//----------------------------------------------------------------------- |
|
240 |
/** |
|
241 |
* Obtains an instance of {@code MonthDay} from a temporal object. |
|
242 |
* <p> |
|
243 |
* This obtains a month-day based on the specified temporal. |
|
244 |
* A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
|
245 |
* which this factory converts to an instance of {@code MonthDay}. |
|
246 |
* <p> |
|
247 |
* The conversion extracts the {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} and |
|
248 |
* {@link ChronoField#DAY_OF_MONTH DAY_OF_MONTH} fields. |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
249 |
* The extraction is only permitted if the temporal object has an ISO |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
250 |
* chronology, or can be converted to a {@code LocalDate}. |
15658 | 251 |
* <p> |
252 |
* This method matches the signature of the functional interface {@link TemporalQuery} |
|
253 |
* allowing it to be used in queries via method reference, {@code MonthDay::from}. |
|
254 |
* |
|
255 |
* @param temporal the temporal object to convert, not null |
|
256 |
* @return the month-day, not null |
|
257 |
* @throws DateTimeException if unable to convert to a {@code MonthDay} |
|
258 |
*/ |
|
259 |
public static MonthDay from(TemporalAccessor temporal) { |
|
260 |
if (temporal instanceof MonthDay) { |
|
261 |
return (MonthDay) temporal; |
|
262 |
} |
|
263 |
try { |
|
264 |
if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) { |
|
265 |
temporal = LocalDate.from(temporal); |
|
266 |
} |
|
267 |
return of(temporal.get(MONTH_OF_YEAR), temporal.get(DAY_OF_MONTH)); |
|
268 |
} catch (DateTimeException ex) { |
|
20747 | 269 |
throw new DateTimeException("Unable to obtain MonthDay from TemporalAccessor: " + |
270 |
temporal + " of type " + temporal.getClass().getName(), ex); |
|
15658 | 271 |
} |
272 |
} |
|
273 |
||
274 |
//----------------------------------------------------------------------- |
|
275 |
/** |
|
276 |
* Obtains an instance of {@code MonthDay} from a text string such as {@code --12-03}. |
|
277 |
* <p> |
|
278 |
* The string must represent a valid month-day. |
|
279 |
* The format is {@code --MM-dd}. |
|
280 |
* |
|
281 |
* @param text the text to parse such as "--12-03", not null |
|
282 |
* @return the parsed month-day, not null |
|
283 |
* @throws DateTimeParseException if the text cannot be parsed |
|
284 |
*/ |
|
285 |
public static MonthDay parse(CharSequence text) { |
|
286 |
return parse(text, PARSER); |
|
287 |
} |
|
288 |
||
289 |
/** |
|
290 |
* Obtains an instance of {@code MonthDay} from a text string using a specific formatter. |
|
291 |
* <p> |
|
292 |
* The text is parsed using the formatter, returning a month-day. |
|
293 |
* |
|
294 |
* @param text the text to parse, not null |
|
295 |
* @param formatter the formatter to use, not null |
|
296 |
* @return the parsed month-day, not null |
|
297 |
* @throws DateTimeParseException if the text cannot be parsed |
|
298 |
*/ |
|
299 |
public static MonthDay parse(CharSequence text, DateTimeFormatter formatter) { |
|
300 |
Objects.requireNonNull(formatter, "formatter"); |
|
301 |
return formatter.parse(text, MonthDay::from); |
|
302 |
} |
|
303 |
||
304 |
//----------------------------------------------------------------------- |
|
305 |
/** |
|
306 |
* Constructor, previously validated. |
|
307 |
* |
|
308 |
* @param month the month-of-year to represent, validated from 1 to 12 |
|
309 |
* @param dayOfMonth the day-of-month to represent, validated from 1 to 29-31 |
|
310 |
*/ |
|
311 |
private MonthDay(int month, int dayOfMonth) { |
|
312 |
this.month = month; |
|
313 |
this.day = dayOfMonth; |
|
314 |
} |
|
315 |
||
316 |
//----------------------------------------------------------------------- |
|
317 |
/** |
|
318 |
* Checks if the specified field is supported. |
|
319 |
* <p> |
|
320 |
* This checks if this month-day can be queried for the specified field. |
|
321 |
* If false, then calling the {@link #range(TemporalField) range} and |
|
322 |
* {@link #get(TemporalField) get} methods will throw an exception. |
|
323 |
* <p> |
|
324 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
325 |
* The supported fields are: |
|
326 |
* <ul> |
|
327 |
* <li>{@code MONTH_OF_YEAR} |
|
328 |
* <li>{@code YEAR} |
|
329 |
* </ul> |
|
330 |
* All other {@code ChronoField} instances will return false. |
|
331 |
* <p> |
|
332 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
333 |
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} |
|
334 |
* passing {@code this} as the argument. |
|
335 |
* Whether the field is supported is determined by the field. |
|
336 |
* |
|
337 |
* @param field the field to check, null returns false |
|
338 |
* @return true if the field is supported on this month-day, false if not |
|
339 |
*/ |
|
340 |
@Override |
|
341 |
public boolean isSupported(TemporalField field) { |
|
342 |
if (field instanceof ChronoField) { |
|
343 |
return field == MONTH_OF_YEAR || field == DAY_OF_MONTH; |
|
344 |
} |
|
345 |
return field != null && field.isSupportedBy(this); |
|
346 |
} |
|
347 |
||
348 |
/** |
|
349 |
* Gets the range of valid values for the specified field. |
|
350 |
* <p> |
|
351 |
* The range object expresses the minimum and maximum valid values for a field. |
|
352 |
* This month-day is used to enhance the accuracy of the returned range. |
|
353 |
* If it is not possible to return the range, because the field is not supported |
|
354 |
* or for some other reason, an exception is thrown. |
|
355 |
* <p> |
|
356 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
357 |
* The {@link #isSupported(TemporalField) supported fields} will return |
|
358 |
* appropriate range instances. |
|
16852 | 359 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 360 |
* <p> |
361 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
362 |
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} |
|
363 |
* passing {@code this} as the argument. |
|
364 |
* Whether the range can be obtained is determined by the field. |
|
365 |
* |
|
366 |
* @param field the field to query the range for, not null |
|
367 |
* @return the range of valid values for the field, not null |
|
368 |
* @throws DateTimeException if the range for the field cannot be obtained |
|
16852 | 369 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15658 | 370 |
*/ |
371 |
@Override |
|
372 |
public ValueRange range(TemporalField field) { |
|
373 |
if (field == MONTH_OF_YEAR) { |
|
374 |
return field.range(); |
|
375 |
} else if (field == DAY_OF_MONTH) { |
|
376 |
return ValueRange.of(1, getMonth().minLength(), getMonth().maxLength()); |
|
377 |
} |
|
378 |
return TemporalAccessor.super.range(field); |
|
379 |
} |
|
380 |
||
381 |
/** |
|
382 |
* Gets the value of the specified field from this month-day as an {@code int}. |
|
383 |
* <p> |
|
384 |
* This queries this month-day for the value for the specified field. |
|
385 |
* The returned value will always be within the valid range of values for the field. |
|
386 |
* If it is not possible to return the value, because the field is not supported |
|
387 |
* or for some other reason, an exception is thrown. |
|
388 |
* <p> |
|
389 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
390 |
* The {@link #isSupported(TemporalField) supported fields} will return valid |
|
391 |
* values based on this month-day. |
|
16852 | 392 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 393 |
* <p> |
394 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
395 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
|
396 |
* passing {@code this} as the argument. Whether the value can be obtained, |
|
397 |
* and what the value represents, is determined by the field. |
|
398 |
* |
|
399 |
* @param field the field to get, not null |
|
400 |
* @return the value for the field |
|
16852 | 401 |
* @throws DateTimeException if a value for the field cannot be obtained or |
402 |
* the value is outside the range of valid values for the field |
|
403 |
* @throws UnsupportedTemporalTypeException if the field is not supported or |
|
404 |
* the range of values exceeds an {@code int} |
|
15658 | 405 |
* @throws ArithmeticException if numeric overflow occurs |
406 |
*/ |
|
407 |
@Override // override for Javadoc |
|
408 |
public int get(TemporalField field) { |
|
409 |
return range(field).checkValidIntValue(getLong(field), field); |
|
410 |
} |
|
411 |
||
412 |
/** |
|
413 |
* Gets the value of the specified field from this month-day as a {@code long}. |
|
414 |
* <p> |
|
415 |
* This queries this month-day for the value for the specified field. |
|
416 |
* If it is not possible to return the value, because the field is not supported |
|
417 |
* or for some other reason, an exception is thrown. |
|
418 |
* <p> |
|
419 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
420 |
* The {@link #isSupported(TemporalField) supported fields} will return valid |
|
421 |
* values based on this month-day. |
|
16852 | 422 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 423 |
* <p> |
424 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
425 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
|
426 |
* passing {@code this} as the argument. Whether the value can be obtained, |
|
427 |
* and what the value represents, is determined by the field. |
|
428 |
* |
|
429 |
* @param field the field to get, not null |
|
430 |
* @return the value for the field |
|
431 |
* @throws DateTimeException if a value for the field cannot be obtained |
|
16852 | 432 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15658 | 433 |
* @throws ArithmeticException if numeric overflow occurs |
434 |
*/ |
|
435 |
@Override |
|
436 |
public long getLong(TemporalField field) { |
|
437 |
if (field instanceof ChronoField) { |
|
438 |
switch ((ChronoField) field) { |
|
439 |
// alignedDOW and alignedWOM not supported because they cannot be set in with() |
|
440 |
case DAY_OF_MONTH: return day; |
|
441 |
case MONTH_OF_YEAR: return month; |
|
442 |
} |
|
19030 | 443 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
15658 | 444 |
} |
445 |
return field.getFrom(this); |
|
446 |
} |
|
447 |
||
448 |
//----------------------------------------------------------------------- |
|
449 |
/** |
|
450 |
* Gets the month-of-year field from 1 to 12. |
|
451 |
* <p> |
|
452 |
* This method returns the month as an {@code int} from 1 to 12. |
|
453 |
* Application code is frequently clearer if the enum {@link Month} |
|
454 |
* is used by calling {@link #getMonth()}. |
|
455 |
* |
|
456 |
* @return the month-of-year, from 1 to 12 |
|
457 |
* @see #getMonth() |
|
458 |
*/ |
|
459 |
public int getMonthValue() { |
|
460 |
return month; |
|
461 |
} |
|
462 |
||
463 |
/** |
|
464 |
* Gets the month-of-year field using the {@code Month} enum. |
|
465 |
* <p> |
|
466 |
* This method returns the enum {@link Month} for the month. |
|
467 |
* This avoids confusion as to what {@code int} values mean. |
|
468 |
* If you need access to the primitive {@code int} value then the enum |
|
469 |
* provides the {@link Month#getValue() int value}. |
|
470 |
* |
|
471 |
* @return the month-of-year, not null |
|
472 |
* @see #getMonthValue() |
|
473 |
*/ |
|
474 |
public Month getMonth() { |
|
475 |
return Month.of(month); |
|
476 |
} |
|
477 |
||
478 |
/** |
|
479 |
* Gets the day-of-month field. |
|
480 |
* <p> |
|
481 |
* This method returns the primitive {@code int} value for the day-of-month. |
|
482 |
* |
|
483 |
* @return the day-of-month, from 1 to 31 |
|
484 |
*/ |
|
485 |
public int getDayOfMonth() { |
|
486 |
return day; |
|
487 |
} |
|
488 |
||
489 |
//----------------------------------------------------------------------- |
|
490 |
/** |
|
491 |
* Checks if the year is valid for this month-day. |
|
492 |
* <p> |
|
493 |
* This method checks whether this month and day and the input year form |
|
494 |
* a valid date. This can only return false for February 29th. |
|
495 |
* |
|
496 |
* @param year the year to validate, an out of range value returns false |
|
497 |
* @return true if the year is valid for this month-day |
|
498 |
* @see Year#isValidMonthDay(MonthDay) |
|
499 |
*/ |
|
500 |
public boolean isValidYear(int year) { |
|
501 |
return (day == 29 && month == 2 && Year.isLeap(year) == false) == false; |
|
502 |
} |
|
503 |
||
504 |
//----------------------------------------------------------------------- |
|
505 |
/** |
|
506 |
* Returns a copy of this {@code MonthDay} with the month-of-year altered. |
|
507 |
* <p> |
|
508 |
* This returns a month-day with the specified month. |
|
509 |
* If the day-of-month is invalid for the specified month, the day will |
|
510 |
* be adjusted to the last valid day-of-month. |
|
511 |
* <p> |
|
512 |
* This instance is immutable and unaffected by this method call. |
|
513 |
* |
|
514 |
* @param month the month-of-year to set in the returned month-day, from 1 (January) to 12 (December) |
|
515 |
* @return a {@code MonthDay} based on this month-day with the requested month, not null |
|
516 |
* @throws DateTimeException if the month-of-year value is invalid |
|
517 |
*/ |
|
518 |
public MonthDay withMonth(int month) { |
|
519 |
return with(Month.of(month)); |
|
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* Returns a copy of this {@code MonthDay} with the month-of-year altered. |
|
524 |
* <p> |
|
525 |
* This returns a month-day with the specified month. |
|
526 |
* If the day-of-month is invalid for the specified month, the day will |
|
527 |
* be adjusted to the last valid day-of-month. |
|
528 |
* <p> |
|
529 |
* This instance is immutable and unaffected by this method call. |
|
530 |
* |
|
531 |
* @param month the month-of-year to set in the returned month-day, not null |
|
532 |
* @return a {@code MonthDay} based on this month-day with the requested month, not null |
|
533 |
*/ |
|
534 |
public MonthDay with(Month month) { |
|
535 |
Objects.requireNonNull(month, "month"); |
|
536 |
if (month.getValue() == this.month) { |
|
537 |
return this; |
|
538 |
} |
|
539 |
int day = Math.min(this.day, month.maxLength()); |
|
540 |
return new MonthDay(month.getValue(), day); |
|
541 |
} |
|
542 |
||
543 |
/** |
|
544 |
* Returns a copy of this {@code MonthDay} with the day-of-month altered. |
|
545 |
* <p> |
|
546 |
* This returns a month-day with the specified day-of-month. |
|
547 |
* If the day-of-month is invalid for the month, an exception is thrown. |
|
548 |
* <p> |
|
549 |
* This instance is immutable and unaffected by this method call. |
|
550 |
* |
|
551 |
* @param dayOfMonth the day-of-month to set in the return month-day, from 1 to 31 |
|
552 |
* @return a {@code MonthDay} based on this month-day with the requested day, not null |
|
553 |
* @throws DateTimeException if the day-of-month value is invalid, |
|
554 |
* or if the day-of-month is invalid for the month |
|
555 |
*/ |
|
556 |
public MonthDay withDayOfMonth(int dayOfMonth) { |
|
557 |
if (dayOfMonth == this.day) { |
|
558 |
return this; |
|
559 |
} |
|
560 |
return of(month, dayOfMonth); |
|
561 |
} |
|
562 |
||
563 |
//----------------------------------------------------------------------- |
|
564 |
/** |
|
565 |
* Queries this month-day using the specified query. |
|
566 |
* <p> |
|
567 |
* This queries this month-day using the specified query strategy object. |
|
568 |
* The {@code TemporalQuery} object defines the logic to be used to |
|
569 |
* obtain the result. Read the documentation of the query to understand |
|
570 |
* what the result of this method will be. |
|
571 |
* <p> |
|
572 |
* The result of this method is obtained by invoking the |
|
573 |
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the |
|
574 |
* specified query passing {@code this} as the argument. |
|
575 |
* |
|
576 |
* @param <R> the type of the result |
|
577 |
* @param query the query to invoke, not null |
|
578 |
* @return the query result, null may be returned (defined by the query) |
|
579 |
* @throws DateTimeException if unable to query (defined by the query) |
|
580 |
* @throws ArithmeticException if numeric overflow occurs (defined by the query) |
|
581 |
*/ |
|
582 |
@SuppressWarnings("unchecked") |
|
583 |
@Override |
|
584 |
public <R> R query(TemporalQuery<R> query) { |
|
20795 | 585 |
if (query == TemporalQueries.chronology()) { |
15658 | 586 |
return (R) IsoChronology.INSTANCE; |
587 |
} |
|
588 |
return TemporalAccessor.super.query(query); |
|
589 |
} |
|
590 |
||
591 |
/** |
|
592 |
* Adjusts the specified temporal object to have this month-day. |
|
593 |
* <p> |
|
594 |
* This returns a temporal object of the same observable type as the input |
|
595 |
* with the month and day-of-month changed to be the same as this. |
|
596 |
* <p> |
|
597 |
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} |
|
598 |
* twice, passing {@link ChronoField#MONTH_OF_YEAR} and |
|
599 |
* {@link ChronoField#DAY_OF_MONTH} as the fields. |
|
600 |
* If the specified temporal object does not use the ISO calendar system then |
|
601 |
* a {@code DateTimeException} is thrown. |
|
602 |
* <p> |
|
603 |
* In most cases, it is clearer to reverse the calling pattern by using |
|
604 |
* {@link Temporal#with(TemporalAdjuster)}: |
|
605 |
* <pre> |
|
606 |
* // these two lines are equivalent, but the second approach is recommended |
|
607 |
* temporal = thisMonthDay.adjustInto(temporal); |
|
608 |
* temporal = temporal.with(thisMonthDay); |
|
609 |
* </pre> |
|
610 |
* <p> |
|
611 |
* This instance is immutable and unaffected by this method call. |
|
612 |
* |
|
613 |
* @param temporal the target object to be adjusted, not null |
|
614 |
* @return the adjusted object, not null |
|
615 |
* @throws DateTimeException if unable to make the adjustment |
|
616 |
* @throws ArithmeticException if numeric overflow occurs |
|
617 |
*/ |
|
618 |
@Override |
|
619 |
public Temporal adjustInto(Temporal temporal) { |
|
620 |
if (Chronology.from(temporal).equals(IsoChronology.INSTANCE) == false) { |
|
621 |
throw new DateTimeException("Adjustment only supported on ISO date-time"); |
|
622 |
} |
|
623 |
temporal = temporal.with(MONTH_OF_YEAR, month); |
|
624 |
return temporal.with(DAY_OF_MONTH, Math.min(temporal.range(DAY_OF_MONTH).getMaximum(), day)); |
|
625 |
} |
|
626 |
||
16852 | 627 |
/** |
628 |
* Formats this month-day using the specified formatter. |
|
629 |
* <p> |
|
630 |
* This month-day will be passed to the formatter to produce a string. |
|
631 |
* |
|
632 |
* @param formatter the formatter to use, not null |
|
633 |
* @return the formatted month-day string, not null |
|
634 |
* @throws DateTimeException if an error occurs during printing |
|
635 |
*/ |
|
636 |
public String format(DateTimeFormatter formatter) { |
|
637 |
Objects.requireNonNull(formatter, "formatter"); |
|
638 |
return formatter.format(this); |
|
639 |
} |
|
640 |
||
15658 | 641 |
//----------------------------------------------------------------------- |
642 |
/** |
|
643 |
* Combines this month-day with a year to create a {@code LocalDate}. |
|
644 |
* <p> |
|
645 |
* This returns a {@code LocalDate} formed from this month-day and the specified year. |
|
646 |
* <p> |
|
647 |
* A month-day of February 29th will be adjusted to February 28th in the resulting |
|
648 |
* date if the year is not a leap year. |
|
649 |
* <p> |
|
650 |
* This instance is immutable and unaffected by this method call. |
|
651 |
* |
|
652 |
* @param year the year to use, from MIN_YEAR to MAX_YEAR |
|
653 |
* @return the local date formed from this month-day and the specified year, not null |
|
654 |
* @throws DateTimeException if the year is outside the valid range of years |
|
655 |
*/ |
|
656 |
public LocalDate atYear(int year) { |
|
657 |
return LocalDate.of(year, month, isValidYear(year) ? day : 28); |
|
658 |
} |
|
659 |
||
660 |
//----------------------------------------------------------------------- |
|
661 |
/** |
|
662 |
* Compares this month-day to another month-day. |
|
663 |
* <p> |
|
664 |
* The comparison is based first on value of the month, then on the value of the day. |
|
665 |
* It is "consistent with equals", as defined by {@link Comparable}. |
|
666 |
* |
|
667 |
* @param other the other month-day to compare to, not null |
|
668 |
* @return the comparator value, negative if less, positive if greater |
|
669 |
*/ |
|
670 |
@Override |
|
671 |
public int compareTo(MonthDay other) { |
|
672 |
int cmp = (month - other.month); |
|
673 |
if (cmp == 0) { |
|
674 |
cmp = (day - other.day); |
|
675 |
} |
|
676 |
return cmp; |
|
677 |
} |
|
678 |
||
679 |
/** |
|
680 |
* Is this month-day after the specified month-day. |
|
681 |
* |
|
682 |
* @param other the other month-day to compare to, not null |
|
683 |
* @return true if this is after the specified month-day |
|
684 |
*/ |
|
685 |
public boolean isAfter(MonthDay other) { |
|
686 |
return compareTo(other) > 0; |
|
687 |
} |
|
688 |
||
689 |
/** |
|
690 |
* Is this month-day before the specified month-day. |
|
691 |
* |
|
692 |
* @param other the other month-day to compare to, not null |
|
693 |
* @return true if this point is before the specified month-day |
|
694 |
*/ |
|
695 |
public boolean isBefore(MonthDay other) { |
|
696 |
return compareTo(other) < 0; |
|
697 |
} |
|
698 |
||
699 |
//----------------------------------------------------------------------- |
|
700 |
/** |
|
701 |
* Checks if this month-day is equal to another month-day. |
|
702 |
* <p> |
|
703 |
* The comparison is based on the time-line position of the month-day within a year. |
|
704 |
* |
|
705 |
* @param obj the object to check, null returns false |
|
706 |
* @return true if this is equal to the other month-day |
|
707 |
*/ |
|
708 |
@Override |
|
709 |
public boolean equals(Object obj) { |
|
710 |
if (this == obj) { |
|
711 |
return true; |
|
712 |
} |
|
713 |
if (obj instanceof MonthDay) { |
|
714 |
MonthDay other = (MonthDay) obj; |
|
715 |
return month == other.month && day == other.day; |
|
716 |
} |
|
717 |
return false; |
|
718 |
} |
|
719 |
||
720 |
/** |
|
721 |
* A hash code for this month-day. |
|
722 |
* |
|
723 |
* @return a suitable hash code |
|
724 |
*/ |
|
725 |
@Override |
|
726 |
public int hashCode() { |
|
727 |
return (month << 6) + day; |
|
728 |
} |
|
729 |
||
730 |
//----------------------------------------------------------------------- |
|
731 |
/** |
|
732 |
* Outputs this month-day as a {@code String}, such as {@code --12-03}. |
|
733 |
* <p> |
|
734 |
* The output will be in the format {@code --MM-dd}: |
|
735 |
* |
|
736 |
* @return a string representation of this month-day, not null |
|
737 |
*/ |
|
738 |
@Override |
|
739 |
public String toString() { |
|
740 |
return new StringBuilder(10).append("--") |
|
741 |
.append(month < 10 ? "0" : "").append(month) |
|
742 |
.append(day < 10 ? "-0" : "-").append(day) |
|
743 |
.toString(); |
|
744 |
} |
|
745 |
||
746 |
//----------------------------------------------------------------------- |
|
747 |
/** |
|
748 |
* Writes the object using a |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
749 |
* <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
|
750 |
* @serialData |
15658 | 751 |
* <pre> |
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
752 |
* out.writeByte(13); // identifies a MonthDay |
15658 | 753 |
* out.writeByte(month); |
754 |
* out.writeByte(day); |
|
755 |
* </pre> |
|
756 |
* |
|
757 |
* @return the instance of {@code Ser}, not null |
|
758 |
*/ |
|
759 |
private Object writeReplace() { |
|
760 |
return new Ser(Ser.MONTH_DAY_TYPE, this); |
|
761 |
} |
|
762 |
||
763 |
/** |
|
764 |
* Defend against malicious streams. |
|
765 |
* @return never |
|
766 |
* @throws InvalidObjectException always |
|
767 |
*/ |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
768 |
private Object readResolve() throws InvalidObjectException { |
15658 | 769 |
throw new InvalidObjectException("Deserialization via serialization delegate"); |
770 |
} |
|
771 |
||
772 |
void writeExternal(DataOutput out) throws IOException { |
|
773 |
out.writeByte(month); |
|
774 |
out.writeByte(day); |
|
775 |
} |
|
776 |
||
777 |
static MonthDay readExternal(DataInput in) throws IOException { |
|
778 |
byte month = in.readByte(); |
|
779 |
byte day = in.readByte(); |
|
780 |
return MonthDay.of(month, day); |
|
781 |
} |
|
782 |
||
783 |
} |