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.ObjectStreamException;
|
|
72 |
import java.io.Serializable;
|
|
73 |
import java.time.chrono.Chronology;
|
|
74 |
import java.time.chrono.IsoChronology;
|
|
75 |
import java.time.format.DateTimeFormatter;
|
|
76 |
import java.time.format.DateTimeFormatterBuilder;
|
|
77 |
import java.time.format.DateTimeParseException;
|
|
78 |
import java.time.temporal.ChronoField;
|
|
79 |
import java.time.temporal.Temporal;
|
|
80 |
import java.time.temporal.TemporalAccessor;
|
|
81 |
import java.time.temporal.TemporalAdjuster;
|
|
82 |
import java.time.temporal.TemporalField;
|
|
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.
|
|
249 |
* The extraction is only permitted if the date-time has an ISO chronology.
|
|
250 |
* <p>
|
|
251 |
* This method matches the signature of the functional interface {@link TemporalQuery}
|
|
252 |
* allowing it to be used in queries via method reference, {@code MonthDay::from}.
|
|
253 |
*
|
|
254 |
* @param temporal the temporal object to convert, not null
|
|
255 |
* @return the month-day, not null
|
|
256 |
* @throws DateTimeException if unable to convert to a {@code MonthDay}
|
|
257 |
*/
|
|
258 |
public static MonthDay from(TemporalAccessor temporal) {
|
|
259 |
if (temporal instanceof MonthDay) {
|
|
260 |
return (MonthDay) temporal;
|
|
261 |
}
|
|
262 |
try {
|
|
263 |
if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) {
|
|
264 |
temporal = LocalDate.from(temporal);
|
|
265 |
}
|
|
266 |
return of(temporal.get(MONTH_OF_YEAR), temporal.get(DAY_OF_MONTH));
|
|
267 |
} catch (DateTimeException ex) {
|
|
268 |
throw new DateTimeException("Unable to obtain MonthDay from TemporalAccessor: " + temporal.getClass(), ex);
|
|
269 |
}
|
|
270 |
}
|
|
271 |
|
|
272 |
//-----------------------------------------------------------------------
|
|
273 |
/**
|
|
274 |
* Obtains an instance of {@code MonthDay} from a text string such as {@code --12-03}.
|
|
275 |
* <p>
|
|
276 |
* The string must represent a valid month-day.
|
|
277 |
* The format is {@code --MM-dd}.
|
|
278 |
*
|
|
279 |
* @param text the text to parse such as "--12-03", not null
|
|
280 |
* @return the parsed month-day, not null
|
|
281 |
* @throws DateTimeParseException if the text cannot be parsed
|
|
282 |
*/
|
|
283 |
public static MonthDay parse(CharSequence text) {
|
|
284 |
return parse(text, PARSER);
|
|
285 |
}
|
|
286 |
|
|
287 |
/**
|
|
288 |
* Obtains an instance of {@code MonthDay} from a text string using a specific formatter.
|
|
289 |
* <p>
|
|
290 |
* The text is parsed using the formatter, returning a month-day.
|
|
291 |
*
|
|
292 |
* @param text the text to parse, not null
|
|
293 |
* @param formatter the formatter to use, not null
|
|
294 |
* @return the parsed month-day, not null
|
|
295 |
* @throws DateTimeParseException if the text cannot be parsed
|
|
296 |
*/
|
|
297 |
public static MonthDay parse(CharSequence text, DateTimeFormatter formatter) {
|
|
298 |
Objects.requireNonNull(formatter, "formatter");
|
|
299 |
return formatter.parse(text, MonthDay::from);
|
|
300 |
}
|
|
301 |
|
|
302 |
//-----------------------------------------------------------------------
|
|
303 |
/**
|
|
304 |
* Constructor, previously validated.
|
|
305 |
*
|
|
306 |
* @param month the month-of-year to represent, validated from 1 to 12
|
|
307 |
* @param dayOfMonth the day-of-month to represent, validated from 1 to 29-31
|
|
308 |
*/
|
|
309 |
private MonthDay(int month, int dayOfMonth) {
|
|
310 |
this.month = month;
|
|
311 |
this.day = dayOfMonth;
|
|
312 |
}
|
|
313 |
|
|
314 |
//-----------------------------------------------------------------------
|
|
315 |
/**
|
|
316 |
* Checks if the specified field is supported.
|
|
317 |
* <p>
|
|
318 |
* This checks if this month-day can be queried for the specified field.
|
|
319 |
* If false, then calling the {@link #range(TemporalField) range} and
|
|
320 |
* {@link #get(TemporalField) get} methods will throw an exception.
|
|
321 |
* <p>
|
|
322 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
323 |
* The supported fields are:
|
|
324 |
* <ul>
|
|
325 |
* <li>{@code MONTH_OF_YEAR}
|
|
326 |
* <li>{@code YEAR}
|
|
327 |
* </ul>
|
|
328 |
* All other {@code ChronoField} instances will return false.
|
|
329 |
* <p>
|
|
330 |
* If the field is not a {@code ChronoField}, then the result of this method
|
|
331 |
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
|
|
332 |
* passing {@code this} as the argument.
|
|
333 |
* Whether the field is supported is determined by the field.
|
|
334 |
*
|
|
335 |
* @param field the field to check, null returns false
|
|
336 |
* @return true if the field is supported on this month-day, false if not
|
|
337 |
*/
|
|
338 |
@Override
|
|
339 |
public boolean isSupported(TemporalField field) {
|
|
340 |
if (field instanceof ChronoField) {
|
|
341 |
return field == MONTH_OF_YEAR || field == DAY_OF_MONTH;
|
|
342 |
}
|
|
343 |
return field != null && field.isSupportedBy(this);
|
|
344 |
}
|
|
345 |
|
|
346 |
/**
|
|
347 |
* Gets the range of valid values for the specified field.
|
|
348 |
* <p>
|
|
349 |
* The range object expresses the minimum and maximum valid values for a field.
|
|
350 |
* This month-day is used to enhance the accuracy of the returned range.
|
|
351 |
* If it is not possible to return the range, because the field is not supported
|
|
352 |
* or for some other reason, an exception is thrown.
|
|
353 |
* <p>
|
|
354 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
355 |
* The {@link #isSupported(TemporalField) supported fields} will return
|
|
356 |
* appropriate range instances.
|
16852
|
357 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
|
15658
|
358 |
* <p>
|
|
359 |
* If the field is not a {@code ChronoField}, then the result of this method
|
|
360 |
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
|
|
361 |
* passing {@code this} as the argument.
|
|
362 |
* Whether the range can be obtained is determined by the field.
|
|
363 |
*
|
|
364 |
* @param field the field to query the range for, not null
|
|
365 |
* @return the range of valid values for the field, not null
|
|
366 |
* @throws DateTimeException if the range for the field cannot be obtained
|
16852
|
367 |
* @throws UnsupportedTemporalTypeException if the field is not supported
|
15658
|
368 |
*/
|
|
369 |
@Override
|
|
370 |
public ValueRange range(TemporalField field) {
|
|
371 |
if (field == MONTH_OF_YEAR) {
|
|
372 |
return field.range();
|
|
373 |
} else if (field == DAY_OF_MONTH) {
|
|
374 |
return ValueRange.of(1, getMonth().minLength(), getMonth().maxLength());
|
|
375 |
}
|
|
376 |
return TemporalAccessor.super.range(field);
|
|
377 |
}
|
|
378 |
|
|
379 |
/**
|
|
380 |
* Gets the value of the specified field from this month-day as an {@code int}.
|
|
381 |
* <p>
|
|
382 |
* This queries this month-day for the value for the specified field.
|
|
383 |
* The returned value will always be within the valid range of values for the field.
|
|
384 |
* If it is not possible to return the value, because the field is not supported
|
|
385 |
* or for some other reason, an exception is thrown.
|
|
386 |
* <p>
|
|
387 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
388 |
* The {@link #isSupported(TemporalField) supported fields} will return valid
|
|
389 |
* values based on this month-day.
|
16852
|
390 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
|
15658
|
391 |
* <p>
|
|
392 |
* If the field is not a {@code ChronoField}, then the result of this method
|
|
393 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
|
|
394 |
* passing {@code this} as the argument. Whether the value can be obtained,
|
|
395 |
* and what the value represents, is determined by the field.
|
|
396 |
*
|
|
397 |
* @param field the field to get, not null
|
|
398 |
* @return the value for the field
|
16852
|
399 |
* @throws DateTimeException if a value for the field cannot be obtained or
|
|
400 |
* the value is outside the range of valid values for the field
|
|
401 |
* @throws UnsupportedTemporalTypeException if the field is not supported or
|
|
402 |
* the range of values exceeds an {@code int}
|
15658
|
403 |
* @throws ArithmeticException if numeric overflow occurs
|
|
404 |
*/
|
|
405 |
@Override // override for Javadoc
|
|
406 |
public int get(TemporalField field) {
|
|
407 |
return range(field).checkValidIntValue(getLong(field), field);
|
|
408 |
}
|
|
409 |
|
|
410 |
/**
|
|
411 |
* Gets the value of the specified field from this month-day as a {@code long}.
|
|
412 |
* <p>
|
|
413 |
* This queries this month-day for the value for the specified field.
|
|
414 |
* If it is not possible to return the value, because the field is not supported
|
|
415 |
* or for some other reason, an exception is thrown.
|
|
416 |
* <p>
|
|
417 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
418 |
* The {@link #isSupported(TemporalField) supported fields} will return valid
|
|
419 |
* values based on this month-day.
|
16852
|
420 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
|
15658
|
421 |
* <p>
|
|
422 |
* If the field is not a {@code ChronoField}, then the result of this method
|
|
423 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
|
|
424 |
* passing {@code this} as the argument. Whether the value can be obtained,
|
|
425 |
* and what the value represents, is determined by the field.
|
|
426 |
*
|
|
427 |
* @param field the field to get, not null
|
|
428 |
* @return the value for the field
|
|
429 |
* @throws DateTimeException if a value for the field cannot be obtained
|
16852
|
430 |
* @throws UnsupportedTemporalTypeException if the field is not supported
|
15658
|
431 |
* @throws ArithmeticException if numeric overflow occurs
|
|
432 |
*/
|
|
433 |
@Override
|
|
434 |
public long getLong(TemporalField field) {
|
|
435 |
if (field instanceof ChronoField) {
|
|
436 |
switch ((ChronoField) field) {
|
|
437 |
// alignedDOW and alignedWOM not supported because they cannot be set in with()
|
|
438 |
case DAY_OF_MONTH: return day;
|
|
439 |
case MONTH_OF_YEAR: return month;
|
|
440 |
}
|
16852
|
441 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
|
15658
|
442 |
}
|
|
443 |
return field.getFrom(this);
|
|
444 |
}
|
|
445 |
|
|
446 |
//-----------------------------------------------------------------------
|
|
447 |
/**
|
|
448 |
* Gets the month-of-year field from 1 to 12.
|
|
449 |
* <p>
|
|
450 |
* This method returns the month as an {@code int} from 1 to 12.
|
|
451 |
* Application code is frequently clearer if the enum {@link Month}
|
|
452 |
* is used by calling {@link #getMonth()}.
|
|
453 |
*
|
|
454 |
* @return the month-of-year, from 1 to 12
|
|
455 |
* @see #getMonth()
|
|
456 |
*/
|
|
457 |
public int getMonthValue() {
|
|
458 |
return month;
|
|
459 |
}
|
|
460 |
|
|
461 |
/**
|
|
462 |
* Gets the month-of-year field using the {@code Month} enum.
|
|
463 |
* <p>
|
|
464 |
* This method returns the enum {@link Month} for the month.
|
|
465 |
* This avoids confusion as to what {@code int} values mean.
|
|
466 |
* If you need access to the primitive {@code int} value then the enum
|
|
467 |
* provides the {@link Month#getValue() int value}.
|
|
468 |
*
|
|
469 |
* @return the month-of-year, not null
|
|
470 |
* @see #getMonthValue()
|
|
471 |
*/
|
|
472 |
public Month getMonth() {
|
|
473 |
return Month.of(month);
|
|
474 |
}
|
|
475 |
|
|
476 |
/**
|
|
477 |
* Gets the day-of-month field.
|
|
478 |
* <p>
|
|
479 |
* This method returns the primitive {@code int} value for the day-of-month.
|
|
480 |
*
|
|
481 |
* @return the day-of-month, from 1 to 31
|
|
482 |
*/
|
|
483 |
public int getDayOfMonth() {
|
|
484 |
return day;
|
|
485 |
}
|
|
486 |
|
|
487 |
//-----------------------------------------------------------------------
|
|
488 |
/**
|
|
489 |
* Checks if the year is valid for this month-day.
|
|
490 |
* <p>
|
|
491 |
* This method checks whether this month and day and the input year form
|
|
492 |
* a valid date. This can only return false for February 29th.
|
|
493 |
*
|
|
494 |
* @param year the year to validate, an out of range value returns false
|
|
495 |
* @return true if the year is valid for this month-day
|
|
496 |
* @see Year#isValidMonthDay(MonthDay)
|
|
497 |
*/
|
|
498 |
public boolean isValidYear(int year) {
|
|
499 |
return (day == 29 && month == 2 && Year.isLeap(year) == false) == false;
|
|
500 |
}
|
|
501 |
|
|
502 |
//-----------------------------------------------------------------------
|
|
503 |
/**
|
|
504 |
* Returns a copy of this {@code MonthDay} with the month-of-year altered.
|
|
505 |
* <p>
|
|
506 |
* This returns a month-day with the specified month.
|
|
507 |
* If the day-of-month is invalid for the specified month, the day will
|
|
508 |
* be adjusted to the last valid day-of-month.
|
|
509 |
* <p>
|
|
510 |
* This instance is immutable and unaffected by this method call.
|
|
511 |
*
|
|
512 |
* @param month the month-of-year to set in the returned month-day, from 1 (January) to 12 (December)
|
|
513 |
* @return a {@code MonthDay} based on this month-day with the requested month, not null
|
|
514 |
* @throws DateTimeException if the month-of-year value is invalid
|
|
515 |
*/
|
|
516 |
public MonthDay withMonth(int month) {
|
|
517 |
return with(Month.of(month));
|
|
518 |
}
|
|
519 |
|
|
520 |
/**
|
|
521 |
* Returns a copy of this {@code MonthDay} with the month-of-year altered.
|
|
522 |
* <p>
|
|
523 |
* This returns a month-day with the specified month.
|
|
524 |
* If the day-of-month is invalid for the specified month, the day will
|
|
525 |
* be adjusted to the last valid day-of-month.
|
|
526 |
* <p>
|
|
527 |
* This instance is immutable and unaffected by this method call.
|
|
528 |
*
|
|
529 |
* @param month the month-of-year to set in the returned month-day, not null
|
|
530 |
* @return a {@code MonthDay} based on this month-day with the requested month, not null
|
|
531 |
*/
|
|
532 |
public MonthDay with(Month month) {
|
|
533 |
Objects.requireNonNull(month, "month");
|
|
534 |
if (month.getValue() == this.month) {
|
|
535 |
return this;
|
|
536 |
}
|
|
537 |
int day = Math.min(this.day, month.maxLength());
|
|
538 |
return new MonthDay(month.getValue(), day);
|
|
539 |
}
|
|
540 |
|
|
541 |
/**
|
|
542 |
* Returns a copy of this {@code MonthDay} with the day-of-month altered.
|
|
543 |
* <p>
|
|
544 |
* This returns a month-day with the specified day-of-month.
|
|
545 |
* If the day-of-month is invalid for the month, an exception is thrown.
|
|
546 |
* <p>
|
|
547 |
* This instance is immutable and unaffected by this method call.
|
|
548 |
*
|
|
549 |
* @param dayOfMonth the day-of-month to set in the return month-day, from 1 to 31
|
|
550 |
* @return a {@code MonthDay} based on this month-day with the requested day, not null
|
|
551 |
* @throws DateTimeException if the day-of-month value is invalid,
|
|
552 |
* or if the day-of-month is invalid for the month
|
|
553 |
*/
|
|
554 |
public MonthDay withDayOfMonth(int dayOfMonth) {
|
|
555 |
if (dayOfMonth == this.day) {
|
|
556 |
return this;
|
|
557 |
}
|
|
558 |
return of(month, dayOfMonth);
|
|
559 |
}
|
|
560 |
|
|
561 |
//-----------------------------------------------------------------------
|
|
562 |
/**
|
|
563 |
* Queries this month-day using the specified query.
|
|
564 |
* <p>
|
|
565 |
* This queries this month-day using the specified query strategy object.
|
|
566 |
* The {@code TemporalQuery} object defines the logic to be used to
|
|
567 |
* obtain the result. Read the documentation of the query to understand
|
|
568 |
* what the result of this method will be.
|
|
569 |
* <p>
|
|
570 |
* The result of this method is obtained by invoking the
|
|
571 |
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
|
|
572 |
* specified query passing {@code this} as the argument.
|
|
573 |
*
|
|
574 |
* @param <R> the type of the result
|
|
575 |
* @param query the query to invoke, not null
|
|
576 |
* @return the query result, null may be returned (defined by the query)
|
|
577 |
* @throws DateTimeException if unable to query (defined by the query)
|
|
578 |
* @throws ArithmeticException if numeric overflow occurs (defined by the query)
|
|
579 |
*/
|
|
580 |
@SuppressWarnings("unchecked")
|
|
581 |
@Override
|
|
582 |
public <R> R query(TemporalQuery<R> query) {
|
16852
|
583 |
if (query == TemporalQuery.chronology()) {
|
15658
|
584 |
return (R) IsoChronology.INSTANCE;
|
|
585 |
}
|
|
586 |
return TemporalAccessor.super.query(query);
|
|
587 |
}
|
|
588 |
|
|
589 |
/**
|
|
590 |
* Adjusts the specified temporal object to have this month-day.
|
|
591 |
* <p>
|
|
592 |
* This returns a temporal object of the same observable type as the input
|
|
593 |
* with the month and day-of-month changed to be the same as this.
|
|
594 |
* <p>
|
|
595 |
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
|
|
596 |
* twice, passing {@link ChronoField#MONTH_OF_YEAR} and
|
|
597 |
* {@link ChronoField#DAY_OF_MONTH} as the fields.
|
|
598 |
* If the specified temporal object does not use the ISO calendar system then
|
|
599 |
* a {@code DateTimeException} is thrown.
|
|
600 |
* <p>
|
|
601 |
* In most cases, it is clearer to reverse the calling pattern by using
|
|
602 |
* {@link Temporal#with(TemporalAdjuster)}:
|
|
603 |
* <pre>
|
|
604 |
* // these two lines are equivalent, but the second approach is recommended
|
|
605 |
* temporal = thisMonthDay.adjustInto(temporal);
|
|
606 |
* temporal = temporal.with(thisMonthDay);
|
|
607 |
* </pre>
|
|
608 |
* <p>
|
|
609 |
* This instance is immutable and unaffected by this method call.
|
|
610 |
*
|
|
611 |
* @param temporal the target object to be adjusted, not null
|
|
612 |
* @return the adjusted object, not null
|
|
613 |
* @throws DateTimeException if unable to make the adjustment
|
|
614 |
* @throws ArithmeticException if numeric overflow occurs
|
|
615 |
*/
|
|
616 |
@Override
|
|
617 |
public Temporal adjustInto(Temporal temporal) {
|
|
618 |
if (Chronology.from(temporal).equals(IsoChronology.INSTANCE) == false) {
|
|
619 |
throw new DateTimeException("Adjustment only supported on ISO date-time");
|
|
620 |
}
|
|
621 |
temporal = temporal.with(MONTH_OF_YEAR, month);
|
|
622 |
return temporal.with(DAY_OF_MONTH, Math.min(temporal.range(DAY_OF_MONTH).getMaximum(), day));
|
|
623 |
}
|
|
624 |
|
16852
|
625 |
/**
|
|
626 |
* Formats this month-day using the specified formatter.
|
|
627 |
* <p>
|
|
628 |
* This month-day will be passed to the formatter to produce a string.
|
|
629 |
*
|
|
630 |
* @param formatter the formatter to use, not null
|
|
631 |
* @return the formatted month-day string, not null
|
|
632 |
* @throws DateTimeException if an error occurs during printing
|
|
633 |
*/
|
|
634 |
public String format(DateTimeFormatter formatter) {
|
|
635 |
Objects.requireNonNull(formatter, "formatter");
|
|
636 |
return formatter.format(this);
|
|
637 |
}
|
|
638 |
|
15658
|
639 |
//-----------------------------------------------------------------------
|
|
640 |
/**
|
|
641 |
* Combines this month-day with a year to create a {@code LocalDate}.
|
|
642 |
* <p>
|
|
643 |
* This returns a {@code LocalDate} formed from this month-day and the specified year.
|
|
644 |
* <p>
|
|
645 |
* A month-day of February 29th will be adjusted to February 28th in the resulting
|
|
646 |
* date if the year is not a leap year.
|
|
647 |
* <p>
|
|
648 |
* This instance is immutable and unaffected by this method call.
|
|
649 |
*
|
|
650 |
* @param year the year to use, from MIN_YEAR to MAX_YEAR
|
|
651 |
* @return the local date formed from this month-day and the specified year, not null
|
|
652 |
* @throws DateTimeException if the year is outside the valid range of years
|
|
653 |
*/
|
|
654 |
public LocalDate atYear(int year) {
|
|
655 |
return LocalDate.of(year, month, isValidYear(year) ? day : 28);
|
|
656 |
}
|
|
657 |
|
|
658 |
//-----------------------------------------------------------------------
|
|
659 |
/**
|
|
660 |
* Compares this month-day to another month-day.
|
|
661 |
* <p>
|
|
662 |
* The comparison is based first on value of the month, then on the value of the day.
|
|
663 |
* It is "consistent with equals", as defined by {@link Comparable}.
|
|
664 |
*
|
|
665 |
* @param other the other month-day to compare to, not null
|
|
666 |
* @return the comparator value, negative if less, positive if greater
|
|
667 |
*/
|
|
668 |
@Override
|
|
669 |
public int compareTo(MonthDay other) {
|
|
670 |
int cmp = (month - other.month);
|
|
671 |
if (cmp == 0) {
|
|
672 |
cmp = (day - other.day);
|
|
673 |
}
|
|
674 |
return cmp;
|
|
675 |
}
|
|
676 |
|
|
677 |
/**
|
|
678 |
* Is this month-day after the specified month-day.
|
|
679 |
*
|
|
680 |
* @param other the other month-day to compare to, not null
|
|
681 |
* @return true if this is after the specified month-day
|
|
682 |
*/
|
|
683 |
public boolean isAfter(MonthDay other) {
|
|
684 |
return compareTo(other) > 0;
|
|
685 |
}
|
|
686 |
|
|
687 |
/**
|
|
688 |
* Is this month-day before the specified month-day.
|
|
689 |
*
|
|
690 |
* @param other the other month-day to compare to, not null
|
|
691 |
* @return true if this point is before the specified month-day
|
|
692 |
*/
|
|
693 |
public boolean isBefore(MonthDay other) {
|
|
694 |
return compareTo(other) < 0;
|
|
695 |
}
|
|
696 |
|
|
697 |
//-----------------------------------------------------------------------
|
|
698 |
/**
|
|
699 |
* Checks if this month-day is equal to another month-day.
|
|
700 |
* <p>
|
|
701 |
* The comparison is based on the time-line position of the month-day within a year.
|
|
702 |
*
|
|
703 |
* @param obj the object to check, null returns false
|
|
704 |
* @return true if this is equal to the other month-day
|
|
705 |
*/
|
|
706 |
@Override
|
|
707 |
public boolean equals(Object obj) {
|
|
708 |
if (this == obj) {
|
|
709 |
return true;
|
|
710 |
}
|
|
711 |
if (obj instanceof MonthDay) {
|
|
712 |
MonthDay other = (MonthDay) obj;
|
|
713 |
return month == other.month && day == other.day;
|
|
714 |
}
|
|
715 |
return false;
|
|
716 |
}
|
|
717 |
|
|
718 |
/**
|
|
719 |
* A hash code for this month-day.
|
|
720 |
*
|
|
721 |
* @return a suitable hash code
|
|
722 |
*/
|
|
723 |
@Override
|
|
724 |
public int hashCode() {
|
|
725 |
return (month << 6) + day;
|
|
726 |
}
|
|
727 |
|
|
728 |
//-----------------------------------------------------------------------
|
|
729 |
/**
|
|
730 |
* Outputs this month-day as a {@code String}, such as {@code --12-03}.
|
|
731 |
* <p>
|
|
732 |
* The output will be in the format {@code --MM-dd}:
|
|
733 |
*
|
|
734 |
* @return a string representation of this month-day, not null
|
|
735 |
*/
|
|
736 |
@Override
|
|
737 |
public String toString() {
|
|
738 |
return new StringBuilder(10).append("--")
|
|
739 |
.append(month < 10 ? "0" : "").append(month)
|
|
740 |
.append(day < 10 ? "-0" : "-").append(day)
|
|
741 |
.toString();
|
|
742 |
}
|
|
743 |
|
|
744 |
//-----------------------------------------------------------------------
|
|
745 |
/**
|
|
746 |
* Writes the object using a
|
|
747 |
* <a href="../../../serialized-form.html#java.time.temporal.Ser">dedicated serialized form</a>.
|
|
748 |
* <pre>
|
|
749 |
* out.writeByte(13); // identifies this as a MonthDay
|
|
750 |
* out.writeByte(month);
|
|
751 |
* out.writeByte(day);
|
|
752 |
* </pre>
|
|
753 |
*
|
|
754 |
* @return the instance of {@code Ser}, not null
|
|
755 |
*/
|
|
756 |
private Object writeReplace() {
|
|
757 |
return new Ser(Ser.MONTH_DAY_TYPE, this);
|
|
758 |
}
|
|
759 |
|
|
760 |
/**
|
|
761 |
* Defend against malicious streams.
|
|
762 |
* @return never
|
|
763 |
* @throws InvalidObjectException always
|
|
764 |
*/
|
|
765 |
private Object readResolve() throws ObjectStreamException {
|
|
766 |
throw new InvalidObjectException("Deserialization via serialization delegate");
|
|
767 |
}
|
|
768 |
|
|
769 |
void writeExternal(DataOutput out) throws IOException {
|
|
770 |
out.writeByte(month);
|
|
771 |
out.writeByte(day);
|
|
772 |
}
|
|
773 |
|
|
774 |
static MonthDay readExternal(DataInput in) throws IOException {
|
|
775 |
byte month = in.readByte();
|
|
776 |
byte day = in.readByte();
|
|
777 |
return MonthDay.of(month, day);
|
|
778 |
}
|
|
779 |
|
|
780 |
}
|