author | mduigou |
Tue, 05 Nov 2013 19:44:41 -0800 | |
changeset 21615 | 0231a565a5b7 |
parent 20795 | 8ec9e5b79828 |
child 22081 | 86eb26ff8f2b |
permissions | -rw-r--r-- |
15289 | 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.LocalTime.SECONDS_PER_DAY; |
|
65 |
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; |
|
66 |
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; |
|
67 |
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; |
|
68 |
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; |
|
69 |
import static java.time.temporal.ChronoField.DAY_OF_MONTH; |
|
70 |
import static java.time.temporal.ChronoField.DAY_OF_YEAR; |
|
71 |
import static java.time.temporal.ChronoField.EPOCH_DAY; |
|
72 |
import static java.time.temporal.ChronoField.ERA; |
|
73 |
import static java.time.temporal.ChronoField.MONTH_OF_YEAR; |
|
16852 | 74 |
import static java.time.temporal.ChronoField.PROLEPTIC_MONTH; |
15289 | 75 |
import static java.time.temporal.ChronoField.YEAR; |
76 |
||
77 |
import java.io.DataInput; |
|
78 |
import java.io.DataOutput; |
|
79 |
import java.io.IOException; |
|
80 |
import java.io.InvalidObjectException; |
|
81 |
import java.io.Serializable; |
|
15658 | 82 |
import java.time.chrono.ChronoLocalDate; |
83 |
import java.time.chrono.Era; |
|
84 |
import java.time.chrono.IsoChronology; |
|
15289 | 85 |
import java.time.format.DateTimeFormatter; |
86 |
import java.time.format.DateTimeParseException; |
|
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; |
|
15658 | 92 |
import java.time.temporal.TemporalAmount; |
15289 | 93 |
import java.time.temporal.TemporalField; |
20795 | 94 |
import java.time.temporal.TemporalQueries; |
15289 | 95 |
import java.time.temporal.TemporalQuery; |
96 |
import java.time.temporal.TemporalUnit; |
|
16852 | 97 |
import java.time.temporal.UnsupportedTemporalTypeException; |
15289 | 98 |
import java.time.temporal.ValueRange; |
99 |
import java.time.zone.ZoneOffsetTransition; |
|
100 |
import java.time.zone.ZoneRules; |
|
101 |
import java.util.Objects; |
|
102 |
||
103 |
/** |
|
104 |
* A date without a time-zone in the ISO-8601 calendar system, |
|
105 |
* such as {@code 2007-12-03}. |
|
106 |
* <p> |
|
107 |
* {@code LocalDate} is an immutable date-time object that represents a date, |
|
108 |
* often viewed as year-month-day. Other date fields, such as day-of-year, |
|
109 |
* day-of-week and week-of-year, can also be accessed. |
|
110 |
* For example, the value "2nd October 2007" can be stored in a {@code LocalDate}. |
|
111 |
* <p> |
|
112 |
* This class does not store or represent a time or time-zone. |
|
113 |
* Instead, it is a description of the date, as used for birthdays. |
|
114 |
* It cannot represent an instant on the time-line without additional information |
|
115 |
* such as an offset or time-zone. |
|
116 |
* <p> |
|
117 |
* The ISO-8601 calendar system is the modern civil calendar system used today |
|
118 |
* in most of the world. It is equivalent to the proleptic Gregorian calendar |
|
119 |
* system, in which today's rules for leap years are applied for all time. |
|
120 |
* For most applications written today, the ISO-8601 rules are entirely suitable. |
|
121 |
* However, any application that makes use of historical dates, and requires them |
|
122 |
* to be accurate will find the ISO-8601 approach unsuitable. |
|
123 |
* |
|
17474 | 124 |
* @implSpec |
15289 | 125 |
* This class is immutable and thread-safe. |
126 |
* |
|
127 |
* @since 1.8 |
|
128 |
*/ |
|
129 |
public final class LocalDate |
|
19030 | 130 |
implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable { |
15289 | 131 |
|
132 |
/** |
|
133 |
* The minimum supported {@code LocalDate}, '-999999999-01-01'. |
|
134 |
* This could be used by an application as a "far past" date. |
|
135 |
*/ |
|
136 |
public static final LocalDate MIN = LocalDate.of(Year.MIN_VALUE, 1, 1); |
|
137 |
/** |
|
138 |
* The maximum supported {@code LocalDate}, '+999999999-12-31'. |
|
139 |
* This could be used by an application as a "far future" date. |
|
140 |
*/ |
|
141 |
public static final LocalDate MAX = LocalDate.of(Year.MAX_VALUE, 12, 31); |
|
142 |
||
143 |
/** |
|
144 |
* Serialization version. |
|
145 |
*/ |
|
146 |
private static final long serialVersionUID = 2942565459149668126L; |
|
147 |
/** |
|
148 |
* The number of days in a 400 year cycle. |
|
149 |
*/ |
|
150 |
private static final int DAYS_PER_CYCLE = 146097; |
|
151 |
/** |
|
152 |
* The number of days from year zero to year 1970. |
|
153 |
* There are five 400 year cycles from year zero to 2000. |
|
154 |
* There are 7 leap years from 1970 to 2000. |
|
155 |
*/ |
|
156 |
static final long DAYS_0000_TO_1970 = (DAYS_PER_CYCLE * 5L) - (30L * 365L + 7L); |
|
157 |
||
158 |
/** |
|
159 |
* The year. |
|
160 |
*/ |
|
161 |
private final int year; |
|
162 |
/** |
|
163 |
* The month-of-year. |
|
164 |
*/ |
|
165 |
private final short month; |
|
166 |
/** |
|
167 |
* The day-of-month. |
|
168 |
*/ |
|
169 |
private final short day; |
|
170 |
||
171 |
//----------------------------------------------------------------------- |
|
172 |
/** |
|
173 |
* Obtains the current date from the system clock in the default time-zone. |
|
174 |
* <p> |
|
175 |
* This will query the {@link Clock#systemDefaultZone() system clock} in the default |
|
176 |
* time-zone to obtain the current date. |
|
177 |
* <p> |
|
178 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
179 |
* because the clock is hard-coded. |
|
180 |
* |
|
181 |
* @return the current date using the system clock and default time-zone, not null |
|
182 |
*/ |
|
183 |
public static LocalDate now() { |
|
184 |
return now(Clock.systemDefaultZone()); |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Obtains the current date from the system clock in the specified time-zone. |
|
189 |
* <p> |
|
190 |
* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date. |
|
191 |
* Specifying the time-zone avoids dependence on the default time-zone. |
|
192 |
* <p> |
|
193 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
194 |
* because the clock is hard-coded. |
|
195 |
* |
|
196 |
* @param zone the zone ID to use, not null |
|
197 |
* @return the current date using the system clock, not null |
|
198 |
*/ |
|
199 |
public static LocalDate now(ZoneId zone) { |
|
200 |
return now(Clock.system(zone)); |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Obtains the current date from the specified clock. |
|
205 |
* <p> |
|
206 |
* This will query the specified clock to obtain the current date - today. |
|
207 |
* Using this method allows the use of an alternate clock for testing. |
|
208 |
* The alternate clock may be introduced using {@link Clock dependency injection}. |
|
209 |
* |
|
210 |
* @param clock the clock to use, not null |
|
211 |
* @return the current date, not null |
|
212 |
*/ |
|
213 |
public static LocalDate now(Clock clock) { |
|
214 |
Objects.requireNonNull(clock, "clock"); |
|
15658 | 215 |
// inline to avoid creating object and Instant checks |
15289 | 216 |
final Instant now = clock.instant(); // called once |
217 |
ZoneOffset offset = clock.getZone().getRules().getOffset(now); |
|
218 |
long epochSec = now.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later |
|
219 |
long epochDay = Math.floorDiv(epochSec, SECONDS_PER_DAY); |
|
220 |
return LocalDate.ofEpochDay(epochDay); |
|
221 |
} |
|
222 |
||
223 |
//----------------------------------------------------------------------- |
|
224 |
/** |
|
225 |
* Obtains an instance of {@code LocalDate} from a year, month and day. |
|
226 |
* <p> |
|
15658 | 227 |
* This returns a {@code LocalDate} with the specified year, month and day-of-month. |
15289 | 228 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
229 |
* |
|
230 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR |
|
231 |
* @param month the month-of-year to represent, not null |
|
232 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31 |
|
233 |
* @return the local date, not null |
|
15658 | 234 |
* @throws DateTimeException if the value of any field is out of range, |
235 |
* or if the day-of-month is invalid for the month-year |
|
15289 | 236 |
*/ |
237 |
public static LocalDate of(int year, Month month, int dayOfMonth) { |
|
238 |
YEAR.checkValidValue(year); |
|
239 |
Objects.requireNonNull(month, "month"); |
|
240 |
DAY_OF_MONTH.checkValidValue(dayOfMonth); |
|
16852 | 241 |
return create(year, month.getValue(), dayOfMonth); |
15289 | 242 |
} |
243 |
||
244 |
/** |
|
245 |
* Obtains an instance of {@code LocalDate} from a year, month and day. |
|
246 |
* <p> |
|
15658 | 247 |
* This returns a {@code LocalDate} with the specified year, month and day-of-month. |
15289 | 248 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
249 |
* |
|
250 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR |
|
251 |
* @param month the month-of-year to represent, from 1 (January) to 12 (December) |
|
252 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31 |
|
253 |
* @return the local date, not null |
|
15658 | 254 |
* @throws DateTimeException if the value of any field is out of range, |
255 |
* or if the day-of-month is invalid for the month-year |
|
15289 | 256 |
*/ |
257 |
public static LocalDate of(int year, int month, int dayOfMonth) { |
|
258 |
YEAR.checkValidValue(year); |
|
259 |
MONTH_OF_YEAR.checkValidValue(month); |
|
260 |
DAY_OF_MONTH.checkValidValue(dayOfMonth); |
|
16852 | 261 |
return create(year, month, dayOfMonth); |
15289 | 262 |
} |
263 |
||
264 |
//----------------------------------------------------------------------- |
|
265 |
/** |
|
266 |
* Obtains an instance of {@code LocalDate} from a year and day-of-year. |
|
267 |
* <p> |
|
15658 | 268 |
* This returns a {@code LocalDate} with the specified year and day-of-year. |
15289 | 269 |
* The day-of-year must be valid for the year, otherwise an exception will be thrown. |
270 |
* |
|
271 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR |
|
272 |
* @param dayOfYear the day-of-year to represent, from 1 to 366 |
|
273 |
* @return the local date, not null |
|
15658 | 274 |
* @throws DateTimeException if the value of any field is out of range, |
275 |
* or if the day-of-year is invalid for the month-year |
|
15289 | 276 |
*/ |
277 |
public static LocalDate ofYearDay(int year, int dayOfYear) { |
|
278 |
YEAR.checkValidValue(year); |
|
279 |
DAY_OF_YEAR.checkValidValue(dayOfYear); |
|
15658 | 280 |
boolean leap = IsoChronology.INSTANCE.isLeapYear(year); |
15289 | 281 |
if (dayOfYear == 366 && leap == false) { |
282 |
throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year"); |
|
283 |
} |
|
284 |
Month moy = Month.of((dayOfYear - 1) / 31 + 1); |
|
285 |
int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1; |
|
286 |
if (dayOfYear > monthEnd) { |
|
287 |
moy = moy.plus(1); |
|
288 |
} |
|
289 |
int dom = dayOfYear - moy.firstDayOfYear(leap) + 1; |
|
16852 | 290 |
return new LocalDate(year, moy.getValue(), dom); |
15289 | 291 |
} |
292 |
||
293 |
//----------------------------------------------------------------------- |
|
294 |
/** |
|
295 |
* Obtains an instance of {@code LocalDate} from the epoch day count. |
|
296 |
* <p> |
|
15658 | 297 |
* This returns a {@code LocalDate} with the specified epoch-day. |
298 |
* The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count |
|
299 |
* of days where day 0 is 1970-01-01. Negative numbers represent earlier days. |
|
15289 | 300 |
* |
301 |
* @param epochDay the Epoch Day to convert, based on the epoch 1970-01-01 |
|
302 |
* @return the local date, not null |
|
303 |
* @throws DateTimeException if the epoch days exceeds the supported date range |
|
304 |
*/ |
|
305 |
public static LocalDate ofEpochDay(long epochDay) { |
|
306 |
long zeroDay = epochDay + DAYS_0000_TO_1970; |
|
307 |
// find the march-based year |
|
308 |
zeroDay -= 60; // adjust to 0000-03-01 so leap day is at end of four year cycle |
|
309 |
long adjust = 0; |
|
310 |
if (zeroDay < 0) { |
|
311 |
// adjust negative years to positive for calculation |
|
312 |
long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1; |
|
313 |
adjust = adjustCycles * 400; |
|
314 |
zeroDay += -adjustCycles * DAYS_PER_CYCLE; |
|
315 |
} |
|
316 |
long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE; |
|
317 |
long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400); |
|
318 |
if (doyEst < 0) { |
|
319 |
// fix estimate |
|
320 |
yearEst--; |
|
321 |
doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400); |
|
322 |
} |
|
323 |
yearEst += adjust; // reset any negative year |
|
324 |
int marchDoy0 = (int) doyEst; |
|
325 |
||
326 |
// convert march-based values back to january-based |
|
327 |
int marchMonth0 = (marchDoy0 * 5 + 2) / 153; |
|
328 |
int month = (marchMonth0 + 2) % 12 + 1; |
|
329 |
int dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1; |
|
330 |
yearEst += marchMonth0 / 10; |
|
331 |
||
332 |
// check year now we are certain it is correct |
|
333 |
int year = YEAR.checkValidIntValue(yearEst); |
|
334 |
return new LocalDate(year, month, dom); |
|
335 |
} |
|
336 |
||
337 |
//----------------------------------------------------------------------- |
|
338 |
/** |
|
339 |
* Obtains an instance of {@code LocalDate} from a temporal object. |
|
340 |
* <p> |
|
15658 | 341 |
* This obtains a local date based on the specified temporal. |
342 |
* A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
|
343 |
* which this factory converts to an instance of {@code LocalDate}. |
|
15289 | 344 |
* <p> |
20795 | 345 |
* The conversion uses the {@link TemporalQueries#localDate()} query, which relies |
15658 | 346 |
* on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field. |
15289 | 347 |
* <p> |
348 |
* This method matches the signature of the functional interface {@link TemporalQuery} |
|
349 |
* allowing it to be used as a query via method reference, {@code LocalDate::from}. |
|
350 |
* |
|
351 |
* @param temporal the temporal object to convert, not null |
|
352 |
* @return the local date, not null |
|
353 |
* @throws DateTimeException if unable to convert to a {@code LocalDate} |
|
354 |
*/ |
|
355 |
public static LocalDate from(TemporalAccessor temporal) { |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
356 |
Objects.requireNonNull(temporal, "temporal"); |
20795 | 357 |
LocalDate date = temporal.query(TemporalQueries.localDate()); |
15658 | 358 |
if (date == null) { |
20747 | 359 |
throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + |
360 |
temporal + " of type " + temporal.getClass().getName()); |
|
15289 | 361 |
} |
15658 | 362 |
return date; |
15289 | 363 |
} |
364 |
||
365 |
//----------------------------------------------------------------------- |
|
366 |
/** |
|
367 |
* Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}. |
|
368 |
* <p> |
|
369 |
* The string must represent a valid date and is parsed using |
|
15658 | 370 |
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE}. |
15289 | 371 |
* |
372 |
* @param text the text to parse such as "2007-12-03", not null |
|
373 |
* @return the parsed local date, not null |
|
374 |
* @throws DateTimeParseException if the text cannot be parsed |
|
375 |
*/ |
|
376 |
public static LocalDate parse(CharSequence text) { |
|
15658 | 377 |
return parse(text, DateTimeFormatter.ISO_LOCAL_DATE); |
15289 | 378 |
} |
379 |
||
380 |
/** |
|
381 |
* Obtains an instance of {@code LocalDate} from a text string using a specific formatter. |
|
382 |
* <p> |
|
383 |
* The text is parsed using the formatter, returning a date. |
|
384 |
* |
|
385 |
* @param text the text to parse, not null |
|
386 |
* @param formatter the formatter to use, not null |
|
387 |
* @return the parsed local date, not null |
|
388 |
* @throws DateTimeParseException if the text cannot be parsed |
|
389 |
*/ |
|
390 |
public static LocalDate parse(CharSequence text, DateTimeFormatter formatter) { |
|
391 |
Objects.requireNonNull(formatter, "formatter"); |
|
392 |
return formatter.parse(text, LocalDate::from); |
|
393 |
} |
|
394 |
||
395 |
//----------------------------------------------------------------------- |
|
396 |
/** |
|
397 |
* Creates a local date from the year, month and day fields. |
|
398 |
* |
|
399 |
* @param year the year to represent, validated from MIN_YEAR to MAX_YEAR |
|
16852 | 400 |
* @param month the month-of-year to represent, from 1 to 12, validated |
15289 | 401 |
* @param dayOfMonth the day-of-month to represent, validated from 1 to 31 |
402 |
* @return the local date, not null |
|
403 |
* @throws DateTimeException if the day-of-month is invalid for the month-year |
|
404 |
*/ |
|
16852 | 405 |
private static LocalDate create(int year, int month, int dayOfMonth) { |
406 |
if (dayOfMonth > 28) { |
|
407 |
int dom = 31; |
|
408 |
switch (month) { |
|
409 |
case 2: |
|
410 |
dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28); |
|
411 |
break; |
|
412 |
case 4: |
|
413 |
case 6: |
|
414 |
case 9: |
|
415 |
case 11: |
|
416 |
dom = 30; |
|
417 |
break; |
|
418 |
} |
|
419 |
if (dayOfMonth > dom) { |
|
420 |
if (dayOfMonth == 29) { |
|
421 |
throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year"); |
|
422 |
} else { |
|
423 |
throw new DateTimeException("Invalid date '" + Month.of(month).name() + " " + dayOfMonth + "'"); |
|
424 |
} |
|
15289 | 425 |
} |
426 |
} |
|
16852 | 427 |
return new LocalDate(year, month, dayOfMonth); |
15289 | 428 |
} |
429 |
||
430 |
/** |
|
431 |
* Resolves the date, resolving days past the end of month. |
|
432 |
* |
|
433 |
* @param year the year to represent, validated from MIN_YEAR to MAX_YEAR |
|
434 |
* @param month the month-of-year to represent, validated from 1 to 12 |
|
435 |
* @param day the day-of-month to represent, validated from 1 to 31 |
|
436 |
* @return the resolved date, not null |
|
437 |
*/ |
|
438 |
private static LocalDate resolvePreviousValid(int year, int month, int day) { |
|
439 |
switch (month) { |
|
440 |
case 2: |
|
15658 | 441 |
day = Math.min(day, IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28); |
15289 | 442 |
break; |
443 |
case 4: |
|
444 |
case 6: |
|
445 |
case 9: |
|
446 |
case 11: |
|
447 |
day = Math.min(day, 30); |
|
448 |
break; |
|
449 |
} |
|
16852 | 450 |
return new LocalDate(year, month, day); |
15289 | 451 |
} |
452 |
||
453 |
/** |
|
454 |
* Constructor, previously validated. |
|
455 |
* |
|
456 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR |
|
457 |
* @param month the month-of-year to represent, not null |
|
458 |
* @param dayOfMonth the day-of-month to represent, valid for year-month, from 1 to 31 |
|
459 |
*/ |
|
460 |
private LocalDate(int year, int month, int dayOfMonth) { |
|
461 |
this.year = year; |
|
462 |
this.month = (short) month; |
|
463 |
this.day = (short) dayOfMonth; |
|
464 |
} |
|
465 |
||
466 |
//----------------------------------------------------------------------- |
|
467 |
/** |
|
468 |
* Checks if the specified field is supported. |
|
469 |
* <p> |
|
470 |
* This checks if this date can be queried for the specified field. |
|
19030 | 471 |
* If false, then calling the {@link #range(TemporalField) range}, |
472 |
* {@link #get(TemporalField) get} and {@link #with(TemporalField, long)} |
|
473 |
* methods will throw an exception. |
|
15289 | 474 |
* <p> |
475 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
476 |
* The supported fields are: |
|
477 |
* <ul> |
|
478 |
* <li>{@code DAY_OF_WEEK} |
|
479 |
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} |
|
480 |
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} |
|
481 |
* <li>{@code DAY_OF_MONTH} |
|
482 |
* <li>{@code DAY_OF_YEAR} |
|
483 |
* <li>{@code EPOCH_DAY} |
|
484 |
* <li>{@code ALIGNED_WEEK_OF_MONTH} |
|
485 |
* <li>{@code ALIGNED_WEEK_OF_YEAR} |
|
486 |
* <li>{@code MONTH_OF_YEAR} |
|
16852 | 487 |
* <li>{@code PROLEPTIC_MONTH} |
15289 | 488 |
* <li>{@code YEAR_OF_ERA} |
489 |
* <li>{@code YEAR} |
|
490 |
* <li>{@code ERA} |
|
491 |
* </ul> |
|
492 |
* All other {@code ChronoField} instances will return false. |
|
493 |
* <p> |
|
494 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
15658 | 495 |
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} |
15289 | 496 |
* passing {@code this} as the argument. |
497 |
* Whether the field is supported is determined by the field. |
|
498 |
* |
|
499 |
* @param field the field to check, null returns false |
|
500 |
* @return true if the field is supported on this date, false if not |
|
501 |
*/ |
|
502 |
@Override // override for Javadoc |
|
503 |
public boolean isSupported(TemporalField field) { |
|
504 |
return ChronoLocalDate.super.isSupported(field); |
|
505 |
} |
|
506 |
||
507 |
/** |
|
19030 | 508 |
* Checks if the specified unit is supported. |
509 |
* <p> |
|
510 |
* This checks if the specified unit can be added to, or subtracted from, this date-time. |
|
511 |
* If false, then calling the {@link #plus(long, TemporalUnit)} and |
|
512 |
* {@link #minus(long, TemporalUnit) minus} methods will throw an exception. |
|
513 |
* <p> |
|
514 |
* If the unit is a {@link ChronoUnit} then the query is implemented here. |
|
515 |
* The supported units are: |
|
516 |
* <ul> |
|
517 |
* <li>{@code DAYS} |
|
518 |
* <li>{@code WEEKS} |
|
519 |
* <li>{@code MONTHS} |
|
520 |
* <li>{@code YEARS} |
|
521 |
* <li>{@code DECADES} |
|
522 |
* <li>{@code CENTURIES} |
|
523 |
* <li>{@code MILLENNIA} |
|
524 |
* <li>{@code ERAS} |
|
525 |
* </ul> |
|
526 |
* All other {@code ChronoUnit} instances will return false. |
|
527 |
* <p> |
|
528 |
* If the unit is not a {@code ChronoUnit}, then the result of this method |
|
529 |
* is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)} |
|
530 |
* passing {@code this} as the argument. |
|
531 |
* Whether the unit is supported is determined by the unit. |
|
532 |
* |
|
533 |
* @param unit the unit to check, null returns false |
|
534 |
* @return true if the unit can be added/subtracted, false if not |
|
535 |
*/ |
|
536 |
@Override // override for Javadoc |
|
537 |
public boolean isSupported(TemporalUnit unit) { |
|
538 |
return ChronoLocalDate.super.isSupported(unit); |
|
539 |
} |
|
540 |
||
541 |
//----------------------------------------------------------------------- |
|
542 |
/** |
|
15289 | 543 |
* Gets the range of valid values for the specified field. |
544 |
* <p> |
|
545 |
* The range object expresses the minimum and maximum valid values for a field. |
|
546 |
* This date is used to enhance the accuracy of the returned range. |
|
547 |
* If it is not possible to return the range, because the field is not supported |
|
548 |
* or for some other reason, an exception is thrown. |
|
549 |
* <p> |
|
550 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
551 |
* The {@link #isSupported(TemporalField) supported fields} will return |
|
552 |
* appropriate range instances. |
|
16852 | 553 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15289 | 554 |
* <p> |
555 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
15658 | 556 |
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} |
15289 | 557 |
* passing {@code this} as the argument. |
558 |
* Whether the range can be obtained is determined by the field. |
|
559 |
* |
|
560 |
* @param field the field to query the range for, not null |
|
561 |
* @return the range of valid values for the field, not null |
|
562 |
* @throws DateTimeException if the range for the field cannot be obtained |
|
16852 | 563 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15289 | 564 |
*/ |
565 |
@Override |
|
566 |
public ValueRange range(TemporalField field) { |
|
567 |
if (field instanceof ChronoField) { |
|
568 |
ChronoField f = (ChronoField) field; |
|
16852 | 569 |
if (f.isDateBased()) { |
15289 | 570 |
switch (f) { |
571 |
case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); |
|
572 |
case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); |
|
573 |
case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5); |
|
574 |
case YEAR_OF_ERA: |
|
575 |
return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE)); |
|
576 |
} |
|
577 |
return field.range(); |
|
578 |
} |
|
19030 | 579 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
15289 | 580 |
} |
15658 | 581 |
return field.rangeRefinedBy(this); |
15289 | 582 |
} |
583 |
||
584 |
/** |
|
585 |
* Gets the value of the specified field from this date as an {@code int}. |
|
586 |
* <p> |
|
587 |
* This queries this date for the value for the specified field. |
|
588 |
* The returned value will always be within the valid range of values for the field. |
|
589 |
* If it is not possible to return the value, because the field is not supported |
|
590 |
* or for some other reason, an exception is thrown. |
|
591 |
* <p> |
|
592 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
593 |
* The {@link #isSupported(TemporalField) supported fields} will return valid |
|
16852 | 594 |
* values based on this date, except {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH} |
15289 | 595 |
* which are too large to fit in an {@code int} and throw a {@code DateTimeException}. |
16852 | 596 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15289 | 597 |
* <p> |
598 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
15658 | 599 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
15289 | 600 |
* passing {@code this} as the argument. Whether the value can be obtained, |
601 |
* and what the value represents, is determined by the field. |
|
602 |
* |
|
603 |
* @param field the field to get, not null |
|
604 |
* @return the value for the field |
|
16852 | 605 |
* @throws DateTimeException if a value for the field cannot be obtained or |
606 |
* the value is outside the range of valid values for the field |
|
607 |
* @throws UnsupportedTemporalTypeException if the field is not supported or |
|
608 |
* the range of values exceeds an {@code int} |
|
15289 | 609 |
* @throws ArithmeticException if numeric overflow occurs |
610 |
*/ |
|
611 |
@Override // override for Javadoc and performance |
|
612 |
public int get(TemporalField field) { |
|
613 |
if (field instanceof ChronoField) { |
|
614 |
return get0(field); |
|
615 |
} |
|
616 |
return ChronoLocalDate.super.get(field); |
|
617 |
} |
|
618 |
||
619 |
/** |
|
620 |
* Gets the value of the specified field from this date as a {@code long}. |
|
621 |
* <p> |
|
622 |
* This queries this date for the value for the specified field. |
|
623 |
* If it is not possible to return the value, because the field is not supported |
|
624 |
* or for some other reason, an exception is thrown. |
|
625 |
* <p> |
|
626 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
627 |
* The {@link #isSupported(TemporalField) supported fields} will return valid |
|
628 |
* values based on this date. |
|
16852 | 629 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15289 | 630 |
* <p> |
631 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
15658 | 632 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
15289 | 633 |
* passing {@code this} as the argument. Whether the value can be obtained, |
634 |
* and what the value represents, is determined by the field. |
|
635 |
* |
|
636 |
* @param field the field to get, not null |
|
637 |
* @return the value for the field |
|
638 |
* @throws DateTimeException if a value for the field cannot be obtained |
|
16852 | 639 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15289 | 640 |
* @throws ArithmeticException if numeric overflow occurs |
641 |
*/ |
|
642 |
@Override |
|
643 |
public long getLong(TemporalField field) { |
|
644 |
if (field instanceof ChronoField) { |
|
645 |
if (field == EPOCH_DAY) { |
|
646 |
return toEpochDay(); |
|
647 |
} |
|
16852 | 648 |
if (field == PROLEPTIC_MONTH) { |
649 |
return getProlepticMonth(); |
|
15289 | 650 |
} |
651 |
return get0(field); |
|
652 |
} |
|
15658 | 653 |
return field.getFrom(this); |
15289 | 654 |
} |
655 |
||
656 |
private int get0(TemporalField field) { |
|
657 |
switch ((ChronoField) field) { |
|
658 |
case DAY_OF_WEEK: return getDayOfWeek().getValue(); |
|
659 |
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1; |
|
660 |
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1; |
|
661 |
case DAY_OF_MONTH: return day; |
|
662 |
case DAY_OF_YEAR: return getDayOfYear(); |
|
16852 | 663 |
case EPOCH_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead"); |
15289 | 664 |
case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1; |
665 |
case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1; |
|
666 |
case MONTH_OF_YEAR: return month; |
|
16852 | 667 |
case PROLEPTIC_MONTH: throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead"); |
15289 | 668 |
case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year); |
669 |
case YEAR: return year; |
|
670 |
case ERA: return (year >= 1 ? 1 : 0); |
|
671 |
} |
|
19030 | 672 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
15289 | 673 |
} |
674 |
||
16852 | 675 |
private long getProlepticMonth() { |
676 |
return (year * 12L + month - 1); |
|
15289 | 677 |
} |
678 |
||
679 |
//----------------------------------------------------------------------- |
|
680 |
/** |
|
681 |
* Gets the chronology of this date, which is the ISO calendar system. |
|
682 |
* <p> |
|
15658 | 683 |
* The {@code Chronology} represents the calendar system in use. |
15289 | 684 |
* The ISO-8601 calendar system is the modern civil calendar system used today |
685 |
* in most of the world. It is equivalent to the proleptic Gregorian calendar |
|
16852 | 686 |
* system, in which today's rules for leap years are applied for all time. |
15289 | 687 |
* |
688 |
* @return the ISO chronology, not null |
|
689 |
*/ |
|
690 |
@Override |
|
15658 | 691 |
public IsoChronology getChronology() { |
692 |
return IsoChronology.INSTANCE; |
|
15289 | 693 |
} |
694 |
||
695 |
/** |
|
696 |
* Gets the era applicable at this date. |
|
697 |
* <p> |
|
15658 | 698 |
* The official ISO-8601 standard does not define eras, however {@code IsoChronology} does. |
15289 | 699 |
* It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards. |
700 |
* Since dates before the Julian-Gregorian cutover are not in line with history, |
|
701 |
* the cutover between 'BCE' and 'CE' is also not aligned with the commonly used |
|
702 |
* eras, often referred to using 'BC' and 'AD'. |
|
703 |
* <p> |
|
704 |
* Users of this class should typically ignore this method as it exists primarily |
|
705 |
* to fulfill the {@link ChronoLocalDate} contract where it is necessary to support |
|
706 |
* the Japanese calendar system. |
|
707 |
* <p> |
|
708 |
* The returned era will be a singleton capable of being compared with the constants |
|
15658 | 709 |
* in {@link IsoChronology} using the {@code ==} operator. |
15289 | 710 |
* |
15658 | 711 |
* @return the {@code IsoChronology} era constant applicable at this date, not null |
15289 | 712 |
*/ |
713 |
@Override // override for Javadoc |
|
15658 | 714 |
public Era getEra() { |
15289 | 715 |
return ChronoLocalDate.super.getEra(); |
716 |
} |
|
717 |
||
718 |
/** |
|
719 |
* Gets the year field. |
|
720 |
* <p> |
|
721 |
* This method returns the primitive {@code int} value for the year. |
|
722 |
* <p> |
|
723 |
* The year returned by this method is proleptic as per {@code get(YEAR)}. |
|
15658 | 724 |
* To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}. |
15289 | 725 |
* |
726 |
* @return the year, from MIN_YEAR to MAX_YEAR |
|
727 |
*/ |
|
728 |
public int getYear() { |
|
729 |
return year; |
|
730 |
} |
|
731 |
||
732 |
/** |
|
733 |
* Gets the month-of-year field from 1 to 12. |
|
734 |
* <p> |
|
735 |
* This method returns the month as an {@code int} from 1 to 12. |
|
736 |
* Application code is frequently clearer if the enum {@link Month} |
|
737 |
* is used by calling {@link #getMonth()}. |
|
738 |
* |
|
739 |
* @return the month-of-year, from 1 to 12 |
|
740 |
* @see #getMonth() |
|
741 |
*/ |
|
742 |
public int getMonthValue() { |
|
743 |
return month; |
|
744 |
} |
|
745 |
||
746 |
/** |
|
747 |
* Gets the month-of-year field using the {@code Month} enum. |
|
748 |
* <p> |
|
749 |
* This method returns the enum {@link Month} for the month. |
|
750 |
* This avoids confusion as to what {@code int} values mean. |
|
751 |
* If you need access to the primitive {@code int} value then the enum |
|
752 |
* provides the {@link Month#getValue() int value}. |
|
753 |
* |
|
754 |
* @return the month-of-year, not null |
|
755 |
* @see #getMonthValue() |
|
756 |
*/ |
|
757 |
public Month getMonth() { |
|
758 |
return Month.of(month); |
|
759 |
} |
|
760 |
||
761 |
/** |
|
762 |
* Gets the day-of-month field. |
|
763 |
* <p> |
|
764 |
* This method returns the primitive {@code int} value for the day-of-month. |
|
765 |
* |
|
766 |
* @return the day-of-month, from 1 to 31 |
|
767 |
*/ |
|
768 |
public int getDayOfMonth() { |
|
769 |
return day; |
|
770 |
} |
|
771 |
||
772 |
/** |
|
773 |
* Gets the day-of-year field. |
|
774 |
* <p> |
|
775 |
* This method returns the primitive {@code int} value for the day-of-year. |
|
776 |
* |
|
777 |
* @return the day-of-year, from 1 to 365, or 366 in a leap year |
|
778 |
*/ |
|
779 |
public int getDayOfYear() { |
|
780 |
return getMonth().firstDayOfYear(isLeapYear()) + day - 1; |
|
781 |
} |
|
782 |
||
783 |
/** |
|
784 |
* Gets the day-of-week field, which is an enum {@code DayOfWeek}. |
|
785 |
* <p> |
|
786 |
* This method returns the enum {@link DayOfWeek} for the day-of-week. |
|
787 |
* This avoids confusion as to what {@code int} values mean. |
|
788 |
* If you need access to the primitive {@code int} value then the enum |
|
789 |
* provides the {@link DayOfWeek#getValue() int value}. |
|
790 |
* <p> |
|
791 |
* Additional information can be obtained from the {@code DayOfWeek}. |
|
792 |
* This includes textual names of the values. |
|
793 |
* |
|
794 |
* @return the day-of-week, not null |
|
795 |
*/ |
|
796 |
public DayOfWeek getDayOfWeek() { |
|
797 |
int dow0 = (int)Math.floorMod(toEpochDay() + 3, 7); |
|
798 |
return DayOfWeek.of(dow0 + 1); |
|
799 |
} |
|
800 |
||
801 |
//----------------------------------------------------------------------- |
|
802 |
/** |
|
803 |
* Checks if the year is a leap year, according to the ISO proleptic |
|
804 |
* calendar system rules. |
|
805 |
* <p> |
|
806 |
* This method applies the current rules for leap years across the whole time-line. |
|
807 |
* In general, a year is a leap year if it is divisible by four without |
|
808 |
* remainder. However, years divisible by 100, are not leap years, with |
|
809 |
* the exception of years divisible by 400 which are. |
|
810 |
* <p> |
|
811 |
* For example, 1904 is a leap year it is divisible by 4. |
|
812 |
* 1900 was not a leap year as it is divisible by 100, however 2000 was a |
|
813 |
* leap year as it is divisible by 400. |
|
814 |
* <p> |
|
815 |
* The calculation is proleptic - applying the same rules into the far future and far past. |
|
816 |
* This is historically inaccurate, but is correct for the ISO-8601 standard. |
|
817 |
* |
|
818 |
* @return true if the year is leap, false otherwise |
|
819 |
*/ |
|
820 |
@Override // override for Javadoc and performance |
|
821 |
public boolean isLeapYear() { |
|
15658 | 822 |
return IsoChronology.INSTANCE.isLeapYear(year); |
15289 | 823 |
} |
824 |
||
825 |
/** |
|
826 |
* Returns the length of the month represented by this date. |
|
827 |
* <p> |
|
828 |
* This returns the length of the month in days. |
|
829 |
* For example, a date in January would return 31. |
|
830 |
* |
|
831 |
* @return the length of the month in days |
|
832 |
*/ |
|
833 |
@Override |
|
834 |
public int lengthOfMonth() { |
|
835 |
switch (month) { |
|
836 |
case 2: |
|
837 |
return (isLeapYear() ? 29 : 28); |
|
838 |
case 4: |
|
839 |
case 6: |
|
840 |
case 9: |
|
841 |
case 11: |
|
842 |
return 30; |
|
843 |
default: |
|
844 |
return 31; |
|
845 |
} |
|
846 |
} |
|
847 |
||
848 |
/** |
|
849 |
* Returns the length of the year represented by this date. |
|
850 |
* <p> |
|
851 |
* This returns the length of the year in days, either 365 or 366. |
|
852 |
* |
|
853 |
* @return 366 if the year is leap, 365 otherwise |
|
854 |
*/ |
|
855 |
@Override // override for Javadoc and performance |
|
856 |
public int lengthOfYear() { |
|
857 |
return (isLeapYear() ? 366 : 365); |
|
858 |
} |
|
859 |
||
860 |
//----------------------------------------------------------------------- |
|
861 |
/** |
|
862 |
* Returns an adjusted copy of this date. |
|
863 |
* <p> |
|
15658 | 864 |
* This returns a {@code LocalDate}, based on this one, with the date adjusted. |
15289 | 865 |
* The adjustment takes place using the specified adjuster strategy object. |
866 |
* Read the documentation of the adjuster to understand what adjustment will be made. |
|
867 |
* <p> |
|
868 |
* A simple adjuster might simply set the one of the fields, such as the year field. |
|
869 |
* A more complex adjuster might set the date to the last day of the month. |
|
16852 | 870 |
* A selection of common adjustments is provided in {@link TemporalAdjuster}. |
15289 | 871 |
* These include finding the "last day of the month" and "next Wednesday". |
872 |
* Key date-time classes also implement the {@code TemporalAdjuster} interface, |
|
15658 | 873 |
* such as {@link Month} and {@link java.time.MonthDay MonthDay}. |
15289 | 874 |
* The adjuster is responsible for handling special cases, such as the varying |
875 |
* lengths of month and leap years. |
|
876 |
* <p> |
|
877 |
* For example this code returns a date on the last day of July: |
|
878 |
* <pre> |
|
879 |
* import static java.time.Month.*; |
|
880 |
* import static java.time.temporal.Adjusters.*; |
|
881 |
* |
|
882 |
* result = localDate.with(JULY).with(lastDayOfMonth()); |
|
883 |
* </pre> |
|
884 |
* <p> |
|
885 |
* The result of this method is obtained by invoking the |
|
886 |
* {@link TemporalAdjuster#adjustInto(Temporal)} method on the |
|
887 |
* specified adjuster passing {@code this} as the argument. |
|
888 |
* <p> |
|
889 |
* This instance is immutable and unaffected by this method call. |
|
890 |
* |
|
891 |
* @param adjuster the adjuster to use, not null |
|
892 |
* @return a {@code LocalDate} based on {@code this} with the adjustment made, not null |
|
893 |
* @throws DateTimeException if the adjustment cannot be made |
|
894 |
* @throws ArithmeticException if numeric overflow occurs |
|
895 |
*/ |
|
896 |
@Override |
|
897 |
public LocalDate with(TemporalAdjuster adjuster) { |
|
898 |
// optimizations |
|
899 |
if (adjuster instanceof LocalDate) { |
|
900 |
return (LocalDate) adjuster; |
|
901 |
} |
|
902 |
return (LocalDate) adjuster.adjustInto(this); |
|
903 |
} |
|
904 |
||
905 |
/** |
|
906 |
* Returns a copy of this date with the specified field set to a new value. |
|
907 |
* <p> |
|
15658 | 908 |
* This returns a {@code LocalDate}, based on this one, with the value |
15289 | 909 |
* for the specified field changed. |
910 |
* This can be used to change any supported field, such as the year, month or day-of-month. |
|
911 |
* If it is not possible to set the value, because the field is not supported or for |
|
912 |
* some other reason, an exception is thrown. |
|
913 |
* <p> |
|
914 |
* In some cases, changing the specified field can cause the resulting date to become invalid, |
|
915 |
* such as changing the month from 31st January to February would make the day-of-month invalid. |
|
916 |
* In cases like this, the field is responsible for resolving the date. Typically it will choose |
|
917 |
* the previous valid date, which would be the last valid day of February in this example. |
|
918 |
* <p> |
|
919 |
* If the field is a {@link ChronoField} then the adjustment is implemented here. |
|
920 |
* The supported fields behave as follows: |
|
921 |
* <ul> |
|
922 |
* <li>{@code DAY_OF_WEEK} - |
|
923 |
* Returns a {@code LocalDate} with the specified day-of-week. |
|
924 |
* The date is adjusted up to 6 days forward or backward within the boundary |
|
925 |
* of a Monday to Sunday week. |
|
926 |
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} - |
|
927 |
* Returns a {@code LocalDate} with the specified aligned-day-of-week. |
|
928 |
* The date is adjusted to the specified month-based aligned-day-of-week. |
|
929 |
* Aligned weeks are counted such that the first week of a given month starts |
|
930 |
* on the first day of that month. |
|
931 |
* This may cause the date to be moved up to 6 days into the following month. |
|
932 |
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} - |
|
933 |
* Returns a {@code LocalDate} with the specified aligned-day-of-week. |
|
934 |
* The date is adjusted to the specified year-based aligned-day-of-week. |
|
935 |
* Aligned weeks are counted such that the first week of a given year starts |
|
936 |
* on the first day of that year. |
|
937 |
* This may cause the date to be moved up to 6 days into the following year. |
|
938 |
* <li>{@code DAY_OF_MONTH} - |
|
939 |
* Returns a {@code LocalDate} with the specified day-of-month. |
|
940 |
* The month and year will be unchanged. If the day-of-month is invalid for the |
|
941 |
* year and month, then a {@code DateTimeException} is thrown. |
|
942 |
* <li>{@code DAY_OF_YEAR} - |
|
943 |
* Returns a {@code LocalDate} with the specified day-of-year. |
|
944 |
* The year will be unchanged. If the day-of-year is invalid for the |
|
945 |
* year, then a {@code DateTimeException} is thrown. |
|
946 |
* <li>{@code EPOCH_DAY} - |
|
947 |
* Returns a {@code LocalDate} with the specified epoch-day. |
|
948 |
* This completely replaces the date and is equivalent to {@link #ofEpochDay(long)}. |
|
949 |
* <li>{@code ALIGNED_WEEK_OF_MONTH} - |
|
950 |
* Returns a {@code LocalDate} with the specified aligned-week-of-month. |
|
951 |
* Aligned weeks are counted such that the first week of a given month starts |
|
952 |
* on the first day of that month. |
|
953 |
* This adjustment moves the date in whole week chunks to match the specified week. |
|
954 |
* The result will have the same day-of-week as this date. |
|
955 |
* This may cause the date to be moved into the following month. |
|
956 |
* <li>{@code ALIGNED_WEEK_OF_YEAR} - |
|
957 |
* Returns a {@code LocalDate} with the specified aligned-week-of-year. |
|
958 |
* Aligned weeks are counted such that the first week of a given year starts |
|
959 |
* on the first day of that year. |
|
960 |
* This adjustment moves the date in whole week chunks to match the specified week. |
|
961 |
* The result will have the same day-of-week as this date. |
|
962 |
* This may cause the date to be moved into the following year. |
|
963 |
* <li>{@code MONTH_OF_YEAR} - |
|
964 |
* Returns a {@code LocalDate} with the specified month-of-year. |
|
965 |
* The year will be unchanged. The day-of-month will also be unchanged, |
|
966 |
* unless it would be invalid for the new month and year. In that case, the |
|
967 |
* day-of-month is adjusted to the maximum valid value for the new month and year. |
|
16852 | 968 |
* <li>{@code PROLEPTIC_MONTH} - |
969 |
* Returns a {@code LocalDate} with the specified proleptic-month. |
|
15289 | 970 |
* The day-of-month will be unchanged, unless it would be invalid for the new month |
971 |
* and year. In that case, the day-of-month is adjusted to the maximum valid value |
|
972 |
* for the new month and year. |
|
973 |
* <li>{@code YEAR_OF_ERA} - |
|
974 |
* Returns a {@code LocalDate} with the specified year-of-era. |
|
975 |
* The era and month will be unchanged. The day-of-month will also be unchanged, |
|
976 |
* unless it would be invalid for the new month and year. In that case, the |
|
977 |
* day-of-month is adjusted to the maximum valid value for the new month and year. |
|
978 |
* <li>{@code YEAR} - |
|
979 |
* Returns a {@code LocalDate} with the specified year. |
|
980 |
* The month will be unchanged. The day-of-month will also be unchanged, |
|
981 |
* unless it would be invalid for the new month and year. In that case, the |
|
982 |
* day-of-month is adjusted to the maximum valid value for the new month and year. |
|
983 |
* <li>{@code ERA} - |
|
984 |
* Returns a {@code LocalDate} with the specified era. |
|
985 |
* The year-of-era and month will be unchanged. The day-of-month will also be unchanged, |
|
986 |
* unless it would be invalid for the new month and year. In that case, the |
|
987 |
* day-of-month is adjusted to the maximum valid value for the new month and year. |
|
988 |
* </ul> |
|
989 |
* <p> |
|
990 |
* In all cases, if the new value is outside the valid range of values for the field |
|
991 |
* then a {@code DateTimeException} will be thrown. |
|
992 |
* <p> |
|
16852 | 993 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15289 | 994 |
* <p> |
995 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
15658 | 996 |
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} |
15289 | 997 |
* passing {@code this} as the argument. In this case, the field determines |
998 |
* whether and how to adjust the instant. |
|
999 |
* <p> |
|
1000 |
* This instance is immutable and unaffected by this method call. |
|
1001 |
* |
|
1002 |
* @param field the field to set in the result, not null |
|
1003 |
* @param newValue the new value of the field in the result |
|
1004 |
* @return a {@code LocalDate} based on {@code this} with the specified field set, not null |
|
1005 |
* @throws DateTimeException if the field cannot be set |
|
16852 | 1006 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15289 | 1007 |
* @throws ArithmeticException if numeric overflow occurs |
1008 |
*/ |
|
1009 |
@Override |
|
1010 |
public LocalDate with(TemporalField field, long newValue) { |
|
1011 |
if (field instanceof ChronoField) { |
|
1012 |
ChronoField f = (ChronoField) field; |
|
1013 |
f.checkValidValue(newValue); |
|
1014 |
switch (f) { |
|
1015 |
case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek().getValue()); |
|
1016 |
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH)); |
|
1017 |
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR)); |
|
1018 |
case DAY_OF_MONTH: return withDayOfMonth((int) newValue); |
|
1019 |
case DAY_OF_YEAR: return withDayOfYear((int) newValue); |
|
1020 |
case EPOCH_DAY: return LocalDate.ofEpochDay(newValue); |
|
1021 |
case ALIGNED_WEEK_OF_MONTH: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH)); |
|
1022 |
case ALIGNED_WEEK_OF_YEAR: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR)); |
|
1023 |
case MONTH_OF_YEAR: return withMonth((int) newValue); |
|
16852 | 1024 |
case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth()); |
15289 | 1025 |
case YEAR_OF_ERA: return withYear((int) (year >= 1 ? newValue : 1 - newValue)); |
1026 |
case YEAR: return withYear((int) newValue); |
|
1027 |
case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year)); |
|
1028 |
} |
|
19030 | 1029 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
15289 | 1030 |
} |
15658 | 1031 |
return field.adjustInto(this, newValue); |
15289 | 1032 |
} |
1033 |
||
1034 |
//----------------------------------------------------------------------- |
|
1035 |
/** |
|
1036 |
* Returns a copy of this date with the year altered. |
|
1037 |
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month. |
|
1038 |
* <p> |
|
1039 |
* This instance is immutable and unaffected by this method call. |
|
1040 |
* |
|
1041 |
* @param year the year to set in the result, from MIN_YEAR to MAX_YEAR |
|
1042 |
* @return a {@code LocalDate} based on this date with the requested year, not null |
|
1043 |
* @throws DateTimeException if the year value is invalid |
|
1044 |
*/ |
|
1045 |
public LocalDate withYear(int year) { |
|
1046 |
if (this.year == year) { |
|
1047 |
return this; |
|
1048 |
} |
|
1049 |
YEAR.checkValidValue(year); |
|
1050 |
return resolvePreviousValid(year, month, day); |
|
1051 |
} |
|
1052 |
||
1053 |
/** |
|
1054 |
* Returns a copy of this date with the month-of-year altered. |
|
1055 |
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month. |
|
1056 |
* <p> |
|
1057 |
* This instance is immutable and unaffected by this method call. |
|
1058 |
* |
|
1059 |
* @param month the month-of-year to set in the result, from 1 (January) to 12 (December) |
|
1060 |
* @return a {@code LocalDate} based on this date with the requested month, not null |
|
1061 |
* @throws DateTimeException if the month-of-year value is invalid |
|
1062 |
*/ |
|
1063 |
public LocalDate withMonth(int month) { |
|
1064 |
if (this.month == month) { |
|
1065 |
return this; |
|
1066 |
} |
|
1067 |
MONTH_OF_YEAR.checkValidValue(month); |
|
1068 |
return resolvePreviousValid(year, month, day); |
|
1069 |
} |
|
1070 |
||
1071 |
/** |
|
1072 |
* Returns a copy of this date with the day-of-month altered. |
|
1073 |
* If the resulting date is invalid, an exception is thrown. |
|
1074 |
* <p> |
|
1075 |
* This instance is immutable and unaffected by this method call. |
|
1076 |
* |
|
1077 |
* @param dayOfMonth the day-of-month to set in the result, from 1 to 28-31 |
|
1078 |
* @return a {@code LocalDate} based on this date with the requested day, not null |
|
15658 | 1079 |
* @throws DateTimeException if the day-of-month value is invalid, |
1080 |
* or if the day-of-month is invalid for the month-year |
|
15289 | 1081 |
*/ |
1082 |
public LocalDate withDayOfMonth(int dayOfMonth) { |
|
1083 |
if (this.day == dayOfMonth) { |
|
1084 |
return this; |
|
1085 |
} |
|
1086 |
return of(year, month, dayOfMonth); |
|
1087 |
} |
|
1088 |
||
1089 |
/** |
|
1090 |
* Returns a copy of this date with the day-of-year altered. |
|
1091 |
* If the resulting date is invalid, an exception is thrown. |
|
1092 |
* <p> |
|
1093 |
* This instance is immutable and unaffected by this method call. |
|
1094 |
* |
|
1095 |
* @param dayOfYear the day-of-year to set in the result, from 1 to 365-366 |
|
1096 |
* @return a {@code LocalDate} based on this date with the requested day, not null |
|
15658 | 1097 |
* @throws DateTimeException if the day-of-year value is invalid, |
1098 |
* or if the day-of-year is invalid for the year |
|
15289 | 1099 |
*/ |
1100 |
public LocalDate withDayOfYear(int dayOfYear) { |
|
1101 |
if (this.getDayOfYear() == dayOfYear) { |
|
1102 |
return this; |
|
1103 |
} |
|
1104 |
return ofYearDay(year, dayOfYear); |
|
1105 |
} |
|
1106 |
||
1107 |
//----------------------------------------------------------------------- |
|
1108 |
/** |
|
15658 | 1109 |
* Returns a copy of this date with the specified amount added. |
1110 |
* <p> |
|
1111 |
* This returns a {@code LocalDate}, based on this one, with the specified amount added. |
|
1112 |
* The amount is typically {@link Period} but may be any other type implementing |
|
1113 |
* the {@link TemporalAmount} interface. |
|
15289 | 1114 |
* <p> |
15658 | 1115 |
* The calculation is delegated to the amount object by calling |
1116 |
* {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free |
|
1117 |
* to implement the addition in any way it wishes, however it typically |
|
1118 |
* calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation |
|
1119 |
* of the amount implementation to determine if it can be successfully added. |
|
15289 | 1120 |
* <p> |
1121 |
* This instance is immutable and unaffected by this method call. |
|
1122 |
* |
|
15658 | 1123 |
* @param amountToAdd the amount to add, not null |
15289 | 1124 |
* @return a {@code LocalDate} based on this date with the addition made, not null |
1125 |
* @throws DateTimeException if the addition cannot be made |
|
1126 |
* @throws ArithmeticException if numeric overflow occurs |
|
1127 |
*/ |
|
1128 |
@Override |
|
15658 | 1129 |
public LocalDate plus(TemporalAmount amountToAdd) { |
20517 | 1130 |
if (amountToAdd instanceof Period) { |
1131 |
Period periodToAdd = (Period) amountToAdd; |
|
1132 |
return plusMonths(periodToAdd.toTotalMonths()).plusDays(periodToAdd.getDays()); |
|
1133 |
} |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1134 |
Objects.requireNonNull(amountToAdd, "amountToAdd"); |
15658 | 1135 |
return (LocalDate) amountToAdd.addTo(this); |
15289 | 1136 |
} |
1137 |
||
1138 |
/** |
|
15658 | 1139 |
* Returns a copy of this date with the specified amount added. |
1140 |
* <p> |
|
1141 |
* This returns a {@code LocalDate}, based on this one, with the amount |
|
1142 |
* in terms of the unit added. If it is not possible to add the amount, because the |
|
1143 |
* unit is not supported or for some other reason, an exception is thrown. |
|
1144 |
* <p> |
|
1145 |
* In some cases, adding the amount can cause the resulting date to become invalid. |
|
1146 |
* For example, adding one month to 31st January would result in 31st February. |
|
1147 |
* In cases like this, the unit is responsible for resolving the date. |
|
1148 |
* Typically it will choose the previous valid date, which would be the last valid |
|
1149 |
* day of February in this example. |
|
15289 | 1150 |
* <p> |
15658 | 1151 |
* If the field is a {@link ChronoUnit} then the addition is implemented here. |
1152 |
* The supported fields behave as follows: |
|
1153 |
* <ul> |
|
1154 |
* <li>{@code DAYS} - |
|
1155 |
* Returns a {@code LocalDate} with the specified number of days added. |
|
1156 |
* This is equivalent to {@link #plusDays(long)}. |
|
1157 |
* <li>{@code WEEKS} - |
|
1158 |
* Returns a {@code LocalDate} with the specified number of weeks added. |
|
1159 |
* This is equivalent to {@link #plusWeeks(long)} and uses a 7 day week. |
|
1160 |
* <li>{@code MONTHS} - |
|
1161 |
* Returns a {@code LocalDate} with the specified number of months added. |
|
1162 |
* This is equivalent to {@link #plusMonths(long)}. |
|
1163 |
* The day-of-month will be unchanged unless it would be invalid for the new |
|
1164 |
* month and year. In that case, the day-of-month is adjusted to the maximum |
|
1165 |
* valid value for the new month and year. |
|
1166 |
* <li>{@code YEARS} - |
|
1167 |
* Returns a {@code LocalDate} with the specified number of years added. |
|
1168 |
* This is equivalent to {@link #plusYears(long)}. |
|
1169 |
* The day-of-month will be unchanged unless it would be invalid for the new |
|
1170 |
* month and year. In that case, the day-of-month is adjusted to the maximum |
|
1171 |
* valid value for the new month and year. |
|
1172 |
* <li>{@code DECADES} - |
|
1173 |
* Returns a {@code LocalDate} with the specified number of decades added. |
|
1174 |
* This is equivalent to calling {@link #plusYears(long)} with the amount |
|
1175 |
* multiplied by 10. |
|
1176 |
* The day-of-month will be unchanged unless it would be invalid for the new |
|
1177 |
* month and year. In that case, the day-of-month is adjusted to the maximum |
|
1178 |
* valid value for the new month and year. |
|
1179 |
* <li>{@code CENTURIES} - |
|
1180 |
* Returns a {@code LocalDate} with the specified number of centuries added. |
|
1181 |
* This is equivalent to calling {@link #plusYears(long)} with the amount |
|
1182 |
* multiplied by 100. |
|
1183 |
* The day-of-month will be unchanged unless it would be invalid for the new |
|
1184 |
* month and year. In that case, the day-of-month is adjusted to the maximum |
|
1185 |
* valid value for the new month and year. |
|
1186 |
* <li>{@code MILLENNIA} - |
|
1187 |
* Returns a {@code LocalDate} with the specified number of millennia added. |
|
1188 |
* This is equivalent to calling {@link #plusYears(long)} with the amount |
|
1189 |
* multiplied by 1,000. |
|
1190 |
* The day-of-month will be unchanged unless it would be invalid for the new |
|
1191 |
* month and year. In that case, the day-of-month is adjusted to the maximum |
|
1192 |
* valid value for the new month and year. |
|
1193 |
* <li>{@code ERAS} - |
|
1194 |
* Returns a {@code LocalDate} with the specified number of eras added. |
|
1195 |
* Only two eras are supported so the amount must be one, zero or minus one. |
|
1196 |
* If the amount is non-zero then the year is changed such that the year-of-era |
|
1197 |
* is unchanged. |
|
1198 |
* The day-of-month will be unchanged unless it would be invalid for the new |
|
1199 |
* month and year. In that case, the day-of-month is adjusted to the maximum |
|
1200 |
* valid value for the new month and year. |
|
1201 |
* </ul> |
|
1202 |
* <p> |
|
16852 | 1203 |
* All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 1204 |
* <p> |
1205 |
* If the field is not a {@code ChronoUnit}, then the result of this method |
|
1206 |
* is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)} |
|
1207 |
* passing {@code this} as the argument. In this case, the unit determines |
|
1208 |
* whether and how to perform the addition. |
|
15289 | 1209 |
* <p> |
1210 |
* This instance is immutable and unaffected by this method call. |
|
1211 |
* |
|
1212 |
* @param amountToAdd the amount of the unit to add to the result, may be negative |
|
15658 | 1213 |
* @param unit the unit of the amount to add, not null |
1214 |
* @return a {@code LocalDate} based on this date with the specified amount added, not null |
|
1215 |
* @throws DateTimeException if the addition cannot be made |
|
16852 | 1216 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 1217 |
* @throws ArithmeticException if numeric overflow occurs |
15289 | 1218 |
*/ |
1219 |
@Override |
|
1220 |
public LocalDate plus(long amountToAdd, TemporalUnit unit) { |
|
1221 |
if (unit instanceof ChronoUnit) { |
|
1222 |
ChronoUnit f = (ChronoUnit) unit; |
|
1223 |
switch (f) { |
|
1224 |
case DAYS: return plusDays(amountToAdd); |
|
1225 |
case WEEKS: return plusWeeks(amountToAdd); |
|
1226 |
case MONTHS: return plusMonths(amountToAdd); |
|
1227 |
case YEARS: return plusYears(amountToAdd); |
|
1228 |
case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10)); |
|
1229 |
case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100)); |
|
1230 |
case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000)); |
|
1231 |
case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd)); |
|
1232 |
} |
|
19030 | 1233 |
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); |
15289 | 1234 |
} |
15658 | 1235 |
return unit.addTo(this, amountToAdd); |
15289 | 1236 |
} |
1237 |
||
1238 |
//----------------------------------------------------------------------- |
|
1239 |
/** |
|
1240 |
* Returns a copy of this {@code LocalDate} with the specified period in years added. |
|
1241 |
* <p> |
|
1242 |
* This method adds the specified amount to the years field in three steps: |
|
1243 |
* <ol> |
|
1244 |
* <li>Add the input years to the year field</li> |
|
1245 |
* <li>Check if the resulting date would be invalid</li> |
|
1246 |
* <li>Adjust the day-of-month to the last valid day if necessary</li> |
|
1247 |
* </ol> |
|
1248 |
* <p> |
|
1249 |
* For example, 2008-02-29 (leap year) plus one year would result in the |
|
1250 |
* invalid date 2009-02-29 (standard year). Instead of returning an invalid |
|
1251 |
* result, the last valid day of the month, 2009-02-28, is selected instead. |
|
1252 |
* <p> |
|
1253 |
* This instance is immutable and unaffected by this method call. |
|
1254 |
* |
|
1255 |
* @param yearsToAdd the years to add, may be negative |
|
1256 |
* @return a {@code LocalDate} based on this date with the years added, not null |
|
1257 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1258 |
*/ |
|
1259 |
public LocalDate plusYears(long yearsToAdd) { |
|
1260 |
if (yearsToAdd == 0) { |
|
1261 |
return this; |
|
1262 |
} |
|
1263 |
int newYear = YEAR.checkValidIntValue(year + yearsToAdd); // safe overflow |
|
1264 |
return resolvePreviousValid(newYear, month, day); |
|
1265 |
} |
|
1266 |
||
1267 |
/** |
|
1268 |
* Returns a copy of this {@code LocalDate} with the specified period in months added. |
|
1269 |
* <p> |
|
1270 |
* This method adds the specified amount to the months field in three steps: |
|
1271 |
* <ol> |
|
1272 |
* <li>Add the input months to the month-of-year field</li> |
|
1273 |
* <li>Check if the resulting date would be invalid</li> |
|
1274 |
* <li>Adjust the day-of-month to the last valid day if necessary</li> |
|
1275 |
* </ol> |
|
1276 |
* <p> |
|
1277 |
* For example, 2007-03-31 plus one month would result in the invalid date |
|
1278 |
* 2007-04-31. Instead of returning an invalid result, the last valid day |
|
1279 |
* of the month, 2007-04-30, is selected instead. |
|
1280 |
* <p> |
|
1281 |
* This instance is immutable and unaffected by this method call. |
|
1282 |
* |
|
1283 |
* @param monthsToAdd the months to add, may be negative |
|
1284 |
* @return a {@code LocalDate} based on this date with the months added, not null |
|
1285 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1286 |
*/ |
|
1287 |
public LocalDate plusMonths(long monthsToAdd) { |
|
1288 |
if (monthsToAdd == 0) { |
|
1289 |
return this; |
|
1290 |
} |
|
1291 |
long monthCount = year * 12L + (month - 1); |
|
1292 |
long calcMonths = monthCount + monthsToAdd; // safe overflow |
|
1293 |
int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcMonths, 12)); |
|
1294 |
int newMonth = (int)Math.floorMod(calcMonths, 12) + 1; |
|
1295 |
return resolvePreviousValid(newYear, newMonth, day); |
|
1296 |
} |
|
1297 |
||
1298 |
/** |
|
1299 |
* Returns a copy of this {@code LocalDate} with the specified period in weeks added. |
|
1300 |
* <p> |
|
1301 |
* This method adds the specified amount in weeks to the days field incrementing |
|
1302 |
* the month and year fields as necessary to ensure the result remains valid. |
|
1303 |
* The result is only invalid if the maximum/minimum year is exceeded. |
|
1304 |
* <p> |
|
1305 |
* For example, 2008-12-31 plus one week would result in 2009-01-07. |
|
1306 |
* <p> |
|
1307 |
* This instance is immutable and unaffected by this method call. |
|
1308 |
* |
|
1309 |
* @param weeksToAdd the weeks to add, may be negative |
|
1310 |
* @return a {@code LocalDate} based on this date with the weeks added, not null |
|
1311 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1312 |
*/ |
|
1313 |
public LocalDate plusWeeks(long weeksToAdd) { |
|
1314 |
return plusDays(Math.multiplyExact(weeksToAdd, 7)); |
|
1315 |
} |
|
1316 |
||
1317 |
/** |
|
1318 |
* Returns a copy of this {@code LocalDate} with the specified number of days added. |
|
1319 |
* <p> |
|
1320 |
* This method adds the specified amount to the days field incrementing the |
|
1321 |
* month and year fields as necessary to ensure the result remains valid. |
|
1322 |
* The result is only invalid if the maximum/minimum year is exceeded. |
|
1323 |
* <p> |
|
1324 |
* For example, 2008-12-31 plus one day would result in 2009-01-01. |
|
1325 |
* <p> |
|
1326 |
* This instance is immutable and unaffected by this method call. |
|
1327 |
* |
|
1328 |
* @param daysToAdd the days to add, may be negative |
|
1329 |
* @return a {@code LocalDate} based on this date with the days added, not null |
|
1330 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1331 |
*/ |
|
1332 |
public LocalDate plusDays(long daysToAdd) { |
|
1333 |
if (daysToAdd == 0) { |
|
1334 |
return this; |
|
1335 |
} |
|
1336 |
long mjDay = Math.addExact(toEpochDay(), daysToAdd); |
|
1337 |
return LocalDate.ofEpochDay(mjDay); |
|
1338 |
} |
|
1339 |
||
1340 |
//----------------------------------------------------------------------- |
|
1341 |
/** |
|
15658 | 1342 |
* Returns a copy of this date with the specified amount subtracted. |
1343 |
* <p> |
|
1344 |
* This returns a {@code LocalDate}, based on this one, with the specified amount subtracted. |
|
1345 |
* The amount is typically {@link Period} but may be any other type implementing |
|
1346 |
* the {@link TemporalAmount} interface. |
|
15289 | 1347 |
* <p> |
15658 | 1348 |
* The calculation is delegated to the amount object by calling |
1349 |
* {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free |
|
1350 |
* to implement the subtraction in any way it wishes, however it typically |
|
1351 |
* calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation |
|
1352 |
* of the amount implementation to determine if it can be successfully subtracted. |
|
15289 | 1353 |
* <p> |
1354 |
* This instance is immutable and unaffected by this method call. |
|
1355 |
* |
|
15658 | 1356 |
* @param amountToSubtract the amount to subtract, not null |
15289 | 1357 |
* @return a {@code LocalDate} based on this date with the subtraction made, not null |
1358 |
* @throws DateTimeException if the subtraction cannot be made |
|
1359 |
* @throws ArithmeticException if numeric overflow occurs |
|
1360 |
*/ |
|
1361 |
@Override |
|
15658 | 1362 |
public LocalDate minus(TemporalAmount amountToSubtract) { |
20517 | 1363 |
if (amountToSubtract instanceof Period) { |
1364 |
Period periodToSubtract = (Period) amountToSubtract; |
|
1365 |
return minusMonths(periodToSubtract.toTotalMonths()).minusDays(periodToSubtract.getDays()); |
|
1366 |
} |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1367 |
Objects.requireNonNull(amountToSubtract, "amountToSubtract"); |
15658 | 1368 |
return (LocalDate) amountToSubtract.subtractFrom(this); |
15289 | 1369 |
} |
1370 |
||
1371 |
/** |
|
15658 | 1372 |
* Returns a copy of this date with the specified amount subtracted. |
15289 | 1373 |
* <p> |
15658 | 1374 |
* This returns a {@code LocalDate}, based on this one, with the amount |
1375 |
* in terms of the unit subtracted. If it is not possible to subtract the amount, |
|
1376 |
* because the unit is not supported or for some other reason, an exception is thrown. |
|
1377 |
* <p> |
|
1378 |
* This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated. |
|
1379 |
* See that method for a full description of how addition, and thus subtraction, works. |
|
15289 | 1380 |
* <p> |
1381 |
* This instance is immutable and unaffected by this method call. |
|
1382 |
* |
|
1383 |
* @param amountToSubtract the amount of the unit to subtract from the result, may be negative |
|
15658 | 1384 |
* @param unit the unit of the amount to subtract, not null |
1385 |
* @return a {@code LocalDate} based on this date with the specified amount subtracted, not null |
|
1386 |
* @throws DateTimeException if the subtraction cannot be made |
|
16852 | 1387 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 1388 |
* @throws ArithmeticException if numeric overflow occurs |
15289 | 1389 |
*/ |
1390 |
@Override |
|
1391 |
public LocalDate minus(long amountToSubtract, TemporalUnit unit) { |
|
1392 |
return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
|
1393 |
} |
|
1394 |
||
1395 |
//----------------------------------------------------------------------- |
|
1396 |
/** |
|
1397 |
* Returns a copy of this {@code LocalDate} with the specified period in years subtracted. |
|
1398 |
* <p> |
|
1399 |
* This method subtracts the specified amount from the years field in three steps: |
|
1400 |
* <ol> |
|
1401 |
* <li>Subtract the input years to the year field</li> |
|
1402 |
* <li>Check if the resulting date would be invalid</li> |
|
1403 |
* <li>Adjust the day-of-month to the last valid day if necessary</li> |
|
1404 |
* </ol> |
|
1405 |
* <p> |
|
1406 |
* For example, 2008-02-29 (leap year) minus one year would result in the |
|
1407 |
* invalid date 2007-02-29 (standard year). Instead of returning an invalid |
|
1408 |
* result, the last valid day of the month, 2007-02-28, is selected instead. |
|
1409 |
* <p> |
|
1410 |
* This instance is immutable and unaffected by this method call. |
|
1411 |
* |
|
1412 |
* @param yearsToSubtract the years to subtract, may be negative |
|
1413 |
* @return a {@code LocalDate} based on this date with the years subtracted, not null |
|
1414 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1415 |
*/ |
|
1416 |
public LocalDate minusYears(long yearsToSubtract) { |
|
1417 |
return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract)); |
|
1418 |
} |
|
1419 |
||
1420 |
/** |
|
1421 |
* Returns a copy of this {@code LocalDate} with the specified period in months subtracted. |
|
1422 |
* <p> |
|
1423 |
* This method subtracts the specified amount from the months field in three steps: |
|
1424 |
* <ol> |
|
1425 |
* <li>Subtract the input months to the month-of-year field</li> |
|
1426 |
* <li>Check if the resulting date would be invalid</li> |
|
1427 |
* <li>Adjust the day-of-month to the last valid day if necessary</li> |
|
1428 |
* </ol> |
|
1429 |
* <p> |
|
1430 |
* For example, 2007-03-31 minus one month would result in the invalid date |
|
1431 |
* 2007-02-31. Instead of returning an invalid result, the last valid day |
|
1432 |
* of the month, 2007-02-28, is selected instead. |
|
1433 |
* <p> |
|
1434 |
* This instance is immutable and unaffected by this method call. |
|
1435 |
* |
|
1436 |
* @param monthsToSubtract the months to subtract, may be negative |
|
1437 |
* @return a {@code LocalDate} based on this date with the months subtracted, not null |
|
1438 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1439 |
*/ |
|
1440 |
public LocalDate minusMonths(long monthsToSubtract) { |
|
1441 |
return (monthsToSubtract == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-monthsToSubtract)); |
|
1442 |
} |
|
1443 |
||
1444 |
/** |
|
1445 |
* Returns a copy of this {@code LocalDate} with the specified period in weeks subtracted. |
|
1446 |
* <p> |
|
1447 |
* This method subtracts the specified amount in weeks from the days field decrementing |
|
1448 |
* the month and year fields as necessary to ensure the result remains valid. |
|
1449 |
* The result is only invalid if the maximum/minimum year is exceeded. |
|
1450 |
* <p> |
|
1451 |
* For example, 2009-01-07 minus one week would result in 2008-12-31. |
|
1452 |
* <p> |
|
1453 |
* This instance is immutable and unaffected by this method call. |
|
1454 |
* |
|
1455 |
* @param weeksToSubtract the weeks to subtract, may be negative |
|
1456 |
* @return a {@code LocalDate} based on this date with the weeks subtracted, not null |
|
1457 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1458 |
*/ |
|
1459 |
public LocalDate minusWeeks(long weeksToSubtract) { |
|
1460 |
return (weeksToSubtract == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeksToSubtract)); |
|
1461 |
} |
|
1462 |
||
1463 |
/** |
|
1464 |
* Returns a copy of this {@code LocalDate} with the specified number of days subtracted. |
|
1465 |
* <p> |
|
1466 |
* This method subtracts the specified amount from the days field decrementing the |
|
1467 |
* month and year fields as necessary to ensure the result remains valid. |
|
1468 |
* The result is only invalid if the maximum/minimum year is exceeded. |
|
1469 |
* <p> |
|
1470 |
* For example, 2009-01-01 minus one day would result in 2008-12-31. |
|
1471 |
* <p> |
|
1472 |
* This instance is immutable and unaffected by this method call. |
|
1473 |
* |
|
1474 |
* @param daysToSubtract the days to subtract, may be negative |
|
1475 |
* @return a {@code LocalDate} based on this date with the days subtracted, not null |
|
1476 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1477 |
*/ |
|
1478 |
public LocalDate minusDays(long daysToSubtract) { |
|
1479 |
return (daysToSubtract == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-daysToSubtract)); |
|
1480 |
} |
|
1481 |
||
1482 |
//----------------------------------------------------------------------- |
|
1483 |
/** |
|
1484 |
* Queries this date using the specified query. |
|
1485 |
* <p> |
|
1486 |
* This queries this date using the specified query strategy object. |
|
1487 |
* The {@code TemporalQuery} object defines the logic to be used to |
|
1488 |
* obtain the result. Read the documentation of the query to understand |
|
1489 |
* what the result of this method will be. |
|
1490 |
* <p> |
|
1491 |
* The result of this method is obtained by invoking the |
|
1492 |
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the |
|
1493 |
* specified query passing {@code this} as the argument. |
|
1494 |
* |
|
1495 |
* @param <R> the type of the result |
|
1496 |
* @param query the query to invoke, not null |
|
1497 |
* @return the query result, null may be returned (defined by the query) |
|
1498 |
* @throws DateTimeException if unable to query (defined by the query) |
|
1499 |
* @throws ArithmeticException if numeric overflow occurs (defined by the query) |
|
1500 |
*/ |
|
15658 | 1501 |
@SuppressWarnings("unchecked") |
1502 |
@Override |
|
15289 | 1503 |
public <R> R query(TemporalQuery<R> query) { |
20795 | 1504 |
if (query == TemporalQueries.localDate()) { |
15658 | 1505 |
return (R) this; |
1506 |
} |
|
15289 | 1507 |
return ChronoLocalDate.super.query(query); |
1508 |
} |
|
1509 |
||
1510 |
/** |
|
1511 |
* Adjusts the specified temporal object to have the same date as this object. |
|
1512 |
* <p> |
|
1513 |
* This returns a temporal object of the same observable type as the input |
|
1514 |
* with the date changed to be the same as this. |
|
1515 |
* <p> |
|
1516 |
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} |
|
1517 |
* passing {@link ChronoField#EPOCH_DAY} as the field. |
|
1518 |
* <p> |
|
1519 |
* In most cases, it is clearer to reverse the calling pattern by using |
|
1520 |
* {@link Temporal#with(TemporalAdjuster)}: |
|
1521 |
* <pre> |
|
1522 |
* // these two lines are equivalent, but the second approach is recommended |
|
1523 |
* temporal = thisLocalDate.adjustInto(temporal); |
|
1524 |
* temporal = temporal.with(thisLocalDate); |
|
1525 |
* </pre> |
|
1526 |
* <p> |
|
1527 |
* This instance is immutable and unaffected by this method call. |
|
1528 |
* |
|
1529 |
* @param temporal the target object to be adjusted, not null |
|
1530 |
* @return the adjusted object, not null |
|
1531 |
* @throws DateTimeException if unable to make the adjustment |
|
1532 |
* @throws ArithmeticException if numeric overflow occurs |
|
1533 |
*/ |
|
1534 |
@Override // override for Javadoc |
|
1535 |
public Temporal adjustInto(Temporal temporal) { |
|
1536 |
return ChronoLocalDate.super.adjustInto(temporal); |
|
1537 |
} |
|
1538 |
||
1539 |
/** |
|
17474 | 1540 |
* Calculates the amount of time until another date in terms of the specified unit. |
15289 | 1541 |
* <p> |
17474 | 1542 |
* This calculates the amount of time between two {@code LocalDate} |
1543 |
* objects in terms of a single {@code TemporalUnit}. |
|
15289 | 1544 |
* The start and end points are {@code this} and the specified date. |
1545 |
* The result will be negative if the end is before the start. |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1546 |
* The {@code Temporal} passed to this method is converted to a |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1547 |
* {@code LocalDate} using {@link #from(TemporalAccessor)}. |
17474 | 1548 |
* For example, the amount in days between two dates can be calculated |
19030 | 1549 |
* using {@code startDate.until(endDate, DAYS)}. |
15289 | 1550 |
* <p> |
1551 |
* The calculation returns a whole number, representing the number of |
|
1552 |
* complete units between the two dates. |
|
17474 | 1553 |
* For example, the amount in months between 2012-06-15 and 2012-08-14 |
15289 | 1554 |
* will only be one month as it is one day short of two months. |
1555 |
* <p> |
|
15658 | 1556 |
* There are two equivalent ways of using this method. |
1557 |
* The first is to invoke this method. |
|
1558 |
* The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: |
|
15289 | 1559 |
* <pre> |
15658 | 1560 |
* // these two lines are equivalent |
19030 | 1561 |
* amount = start.until(end, MONTHS); |
15658 | 1562 |
* amount = MONTHS.between(start, end); |
15289 | 1563 |
* </pre> |
15658 | 1564 |
* The choice should be made based on which makes the code more readable. |
15289 | 1565 |
* <p> |
1566 |
* The calculation is implemented in this method for {@link ChronoUnit}. |
|
1567 |
* The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS}, |
|
1568 |
* {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} |
|
1569 |
* are supported. Other {@code ChronoUnit} values will throw an exception. |
|
1570 |
* <p> |
|
1571 |
* If the unit is not a {@code ChronoUnit}, then the result of this method |
|
1572 |
* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1573 |
* passing {@code this} as the first argument and the converted input temporal |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1574 |
* as the second argument. |
15289 | 1575 |
* <p> |
1576 |
* This instance is immutable and unaffected by this method call. |
|
1577 |
* |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1578 |
* @param endExclusive the end date, exclusive, which is converted to a {@code LocalDate}, not null |
17474 | 1579 |
* @param unit the unit to measure the amount in, not null |
1580 |
* @return the amount of time between this date and the end date |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1581 |
* @throws DateTimeException if the amount cannot be calculated, or the end |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1582 |
* temporal cannot be converted to a {@code LocalDate} |
16852 | 1583 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15289 | 1584 |
* @throws ArithmeticException if numeric overflow occurs |
1585 |
*/ |
|
1586 |
@Override |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1587 |
public long until(Temporal endExclusive, TemporalUnit unit) { |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1588 |
LocalDate end = LocalDate.from(endExclusive); |
15289 | 1589 |
if (unit instanceof ChronoUnit) { |
1590 |
switch ((ChronoUnit) unit) { |
|
1591 |
case DAYS: return daysUntil(end); |
|
1592 |
case WEEKS: return daysUntil(end) / 7; |
|
1593 |
case MONTHS: return monthsUntil(end); |
|
1594 |
case YEARS: return monthsUntil(end) / 12; |
|
1595 |
case DECADES: return monthsUntil(end) / 120; |
|
1596 |
case CENTURIES: return monthsUntil(end) / 1200; |
|
1597 |
case MILLENNIA: return monthsUntil(end) / 12000; |
|
1598 |
case ERAS: return end.getLong(ERA) - getLong(ERA); |
|
1599 |
} |
|
19030 | 1600 |
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); |
15289 | 1601 |
} |
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20519
diff
changeset
|
1602 |
return unit.between(this, end); |
15289 | 1603 |
} |
1604 |
||
1605 |
long daysUntil(LocalDate end) { |
|
1606 |
return end.toEpochDay() - toEpochDay(); // no overflow |
|
1607 |
} |
|
1608 |
||
1609 |
private long monthsUntil(LocalDate end) { |
|
16852 | 1610 |
long packed1 = getProlepticMonth() * 32L + getDayOfMonth(); // no overflow |
1611 |
long packed2 = end.getProlepticMonth() * 32L + end.getDayOfMonth(); // no overflow |
|
15289 | 1612 |
return (packed2 - packed1) / 32; |
1613 |
} |
|
1614 |
||
15658 | 1615 |
/** |
1616 |
* Calculates the period between this date and another date as a {@code Period}. |
|
1617 |
* <p> |
|
1618 |
* This calculates the period between two dates in terms of years, months and days. |
|
1619 |
* The start and end points are {@code this} and the specified date. |
|
1620 |
* The result will be negative if the end is before the start. |
|
16852 | 1621 |
* The negative sign will be the same in each of year, month and day. |
15658 | 1622 |
* <p> |
1623 |
* The calculation is performed using the ISO calendar system. |
|
1624 |
* If necessary, the input date will be converted to ISO. |
|
1625 |
* <p> |
|
1626 |
* The start date is included, but the end date is not. |
|
1627 |
* The period is calculated by removing complete months, then calculating |
|
1628 |
* the remaining number of days, adjusting to ensure that both have the same sign. |
|
1629 |
* The number of months is then normalized into years and months based on a 12 month year. |
|
1630 |
* A month is considered to be complete if the end day-of-month is greater |
|
1631 |
* than or equal to the start day-of-month. |
|
1632 |
* For example, from {@code 2010-01-15} to {@code 2011-03-18} is "1 year, 2 months and 3 days". |
|
1633 |
* <p> |
|
1634 |
* There are two equivalent ways of using this method. |
|
1635 |
* The first is to invoke this method. |
|
1636 |
* The second is to use {@link Period#between(LocalDate, LocalDate)}: |
|
1637 |
* <pre> |
|
1638 |
* // these two lines are equivalent |
|
19030 | 1639 |
* period = start.until(end); |
15658 | 1640 |
* period = Period.between(start, end); |
1641 |
* </pre> |
|
1642 |
* The choice should be made based on which makes the code more readable. |
|
1643 |
* |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
20517
diff
changeset
|
1644 |
* @param endDateExclusive the end date, exclusive, which may be in any chronology, not null |
15658 | 1645 |
* @return the period between this date and the end date, not null |
1646 |
*/ |
|
1647 |
@Override |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
20517
diff
changeset
|
1648 |
public Period until(ChronoLocalDate endDateExclusive) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
20517
diff
changeset
|
1649 |
LocalDate end = LocalDate.from(endDateExclusive); |
16852 | 1650 |
long totalMonths = end.getProlepticMonth() - this.getProlepticMonth(); // safe |
15658 | 1651 |
int days = end.day - this.day; |
1652 |
if (totalMonths > 0 && days < 0) { |
|
1653 |
totalMonths--; |
|
1654 |
LocalDate calcDate = this.plusMonths(totalMonths); |
|
1655 |
days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe |
|
1656 |
} else if (totalMonths < 0 && days > 0) { |
|
1657 |
totalMonths++; |
|
1658 |
days -= end.lengthOfMonth(); |
|
1659 |
} |
|
1660 |
long years = totalMonths / 12; // safe |
|
1661 |
int months = (int) (totalMonths % 12); // safe |
|
1662 |
return Period.of(Math.toIntExact(years), months, days); |
|
1663 |
} |
|
1664 |
||
16852 | 1665 |
/** |
1666 |
* Formats this date using the specified formatter. |
|
1667 |
* <p> |
|
1668 |
* This date will be passed to the formatter to produce a string. |
|
1669 |
* |
|
1670 |
* @param formatter the formatter to use, not null |
|
1671 |
* @return the formatted date string, not null |
|
1672 |
* @throws DateTimeException if an error occurs during printing |
|
1673 |
*/ |
|
1674 |
@Override // override for Javadoc and performance |
|
1675 |
public String format(DateTimeFormatter formatter) { |
|
1676 |
Objects.requireNonNull(formatter, "formatter"); |
|
1677 |
return formatter.format(this); |
|
1678 |
} |
|
1679 |
||
15289 | 1680 |
//----------------------------------------------------------------------- |
1681 |
/** |
|
15658 | 1682 |
* Combines this date with a time to create a {@code LocalDateTime}. |
15289 | 1683 |
* <p> |
15658 | 1684 |
* This returns a {@code LocalDateTime} formed from this date at the specified time. |
15289 | 1685 |
* All possible combinations of date and time are valid. |
1686 |
* |
|
1687 |
* @param time the time to combine with, not null |
|
1688 |
* @return the local date-time formed from this date and the specified time, not null |
|
1689 |
*/ |
|
1690 |
@Override |
|
1691 |
public LocalDateTime atTime(LocalTime time) { |
|
1692 |
return LocalDateTime.of(this, time); |
|
1693 |
} |
|
1694 |
||
1695 |
/** |
|
15658 | 1696 |
* Combines this date with a time to create a {@code LocalDateTime}. |
15289 | 1697 |
* <p> |
15658 | 1698 |
* This returns a {@code LocalDateTime} formed from this date at the |
1699 |
* specified hour and minute. |
|
1700 |
* The seconds and nanosecond fields will be set to zero. |
|
15289 | 1701 |
* The individual time fields must be within their valid range. |
1702 |
* All possible combinations of date and time are valid. |
|
1703 |
* |
|
1704 |
* @param hour the hour-of-day to use, from 0 to 23 |
|
1705 |
* @param minute the minute-of-hour to use, from 0 to 59 |
|
1706 |
* @return the local date-time formed from this date and the specified time, not null |
|
1707 |
* @throws DateTimeException if the value of any field is out of range |
|
1708 |
*/ |
|
1709 |
public LocalDateTime atTime(int hour, int minute) { |
|
1710 |
return atTime(LocalTime.of(hour, minute)); |
|
1711 |
} |
|
1712 |
||
1713 |
/** |
|
15658 | 1714 |
* Combines this date with a time to create a {@code LocalDateTime}. |
15289 | 1715 |
* <p> |
15658 | 1716 |
* This returns a {@code LocalDateTime} formed from this date at the |
1717 |
* specified hour, minute and second. |
|
1718 |
* The nanosecond field will be set to zero. |
|
15289 | 1719 |
* The individual time fields must be within their valid range. |
1720 |
* All possible combinations of date and time are valid. |
|
1721 |
* |
|
1722 |
* @param hour the hour-of-day to use, from 0 to 23 |
|
1723 |
* @param minute the minute-of-hour to use, from 0 to 59 |
|
1724 |
* @param second the second-of-minute to represent, from 0 to 59 |
|
1725 |
* @return the local date-time formed from this date and the specified time, not null |
|
1726 |
* @throws DateTimeException if the value of any field is out of range |
|
1727 |
*/ |
|
1728 |
public LocalDateTime atTime(int hour, int minute, int second) { |
|
1729 |
return atTime(LocalTime.of(hour, minute, second)); |
|
1730 |
} |
|
1731 |
||
1732 |
/** |
|
15658 | 1733 |
* Combines this date with a time to create a {@code LocalDateTime}. |
15289 | 1734 |
* <p> |
15658 | 1735 |
* This returns a {@code LocalDateTime} formed from this date at the |
1736 |
* specified hour, minute, second and nanosecond. |
|
15289 | 1737 |
* The individual time fields must be within their valid range. |
1738 |
* All possible combinations of date and time are valid. |
|
1739 |
* |
|
1740 |
* @param hour the hour-of-day to use, from 0 to 23 |
|
1741 |
* @param minute the minute-of-hour to use, from 0 to 59 |
|
1742 |
* @param second the second-of-minute to represent, from 0 to 59 |
|
1743 |
* @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 |
|
1744 |
* @return the local date-time formed from this date and the specified time, not null |
|
1745 |
* @throws DateTimeException if the value of any field is out of range |
|
1746 |
*/ |
|
1747 |
public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) { |
|
1748 |
return atTime(LocalTime.of(hour, minute, second, nanoOfSecond)); |
|
1749 |
} |
|
1750 |
||
1751 |
/** |
|
15658 | 1752 |
* Combines this date with an offset time to create an {@code OffsetDateTime}. |
15289 | 1753 |
* <p> |
15658 | 1754 |
* This returns an {@code OffsetDateTime} formed from this date at the specified time. |
1755 |
* All possible combinations of date and time are valid. |
|
15289 | 1756 |
* |
15658 | 1757 |
* @param time the time to combine with, not null |
1758 |
* @return the offset date-time formed from this date and the specified time, not null |
|
15289 | 1759 |
*/ |
15658 | 1760 |
public OffsetDateTime atTime(OffsetTime time) { |
1761 |
return OffsetDateTime.of(LocalDateTime.of(this, time.toLocalTime()), time.getOffset()); |
|
1762 |
} |
|
1763 |
||
1764 |
/** |
|
1765 |
* Combines this date with the time of midnight to create a {@code LocalDateTime} |
|
1766 |
* at the start of this date. |
|
1767 |
* <p> |
|
1768 |
* This returns a {@code LocalDateTime} formed from this date at the time of |
|
1769 |
* midnight, 00:00, at the start of this date. |
|
1770 |
* |
|
1771 |
* @return the local date-time of midnight at the start of this date, not null |
|
1772 |
*/ |
|
1773 |
public LocalDateTime atStartOfDay() { |
|
1774 |
return LocalDateTime.of(this, LocalTime.MIDNIGHT); |
|
15289 | 1775 |
} |
1776 |
||
1777 |
/** |
|
1778 |
* Returns a zoned date-time from this date at the earliest valid time according |
|
1779 |
* to the rules in the time-zone. |
|
1780 |
* <p> |
|
1781 |
* Time-zone rules, such as daylight savings, mean that not every local date-time |
|
1782 |
* is valid for the specified zone, thus the local date-time may not be midnight. |
|
1783 |
* <p> |
|
1784 |
* In most cases, there is only one valid offset for a local date-time. |
|
1785 |
* In the case of an overlap, there are two valid offsets, and the earlier one is used, |
|
1786 |
* corresponding to the first occurrence of midnight on the date. |
|
1787 |
* In the case of a gap, the zoned date-time will represent the instant just after the gap. |
|
1788 |
* <p> |
|
1789 |
* If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight. |
|
1790 |
* <p> |
|
1791 |
* To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)} |
|
1792 |
* followed by {@link LocalDateTime#atZone(ZoneId)}. |
|
1793 |
* |
|
1794 |
* @param zone the zone ID to use, not null |
|
1795 |
* @return the zoned date-time formed from this date and the earliest valid time for the zone, not null |
|
1796 |
*/ |
|
1797 |
public ZonedDateTime atStartOfDay(ZoneId zone) { |
|
1798 |
Objects.requireNonNull(zone, "zone"); |
|
1799 |
// need to handle case where there is a gap from 11:30 to 00:30 |
|
1800 |
// standard ZDT factory would result in 01:00 rather than 00:30 |
|
1801 |
LocalDateTime ldt = atTime(LocalTime.MIDNIGHT); |
|
1802 |
if (zone instanceof ZoneOffset == false) { |
|
1803 |
ZoneRules rules = zone.getRules(); |
|
1804 |
ZoneOffsetTransition trans = rules.getTransition(ldt); |
|
1805 |
if (trans != null && trans.isGap()) { |
|
1806 |
ldt = trans.getDateTimeAfter(); |
|
1807 |
} |
|
1808 |
} |
|
1809 |
return ZonedDateTime.of(ldt, zone); |
|
1810 |
} |
|
1811 |
||
1812 |
//----------------------------------------------------------------------- |
|
1813 |
@Override |
|
1814 |
public long toEpochDay() { |
|
1815 |
long y = year; |
|
1816 |
long m = month; |
|
1817 |
long total = 0; |
|
1818 |
total += 365 * y; |
|
1819 |
if (y >= 0) { |
|
1820 |
total += (y + 3) / 4 - (y + 99) / 100 + (y + 399) / 400; |
|
1821 |
} else { |
|
1822 |
total -= y / -4 - y / -100 + y / -400; |
|
1823 |
} |
|
1824 |
total += ((367 * m - 362) / 12); |
|
1825 |
total += day - 1; |
|
1826 |
if (m > 2) { |
|
1827 |
total--; |
|
1828 |
if (isLeapYear() == false) { |
|
1829 |
total--; |
|
1830 |
} |
|
1831 |
} |
|
1832 |
return total - DAYS_0000_TO_1970; |
|
1833 |
} |
|
1834 |
||
1835 |
//----------------------------------------------------------------------- |
|
1836 |
/** |
|
1837 |
* Compares this date to another date. |
|
1838 |
* <p> |
|
1839 |
* The comparison is primarily based on the date, from earliest to latest. |
|
1840 |
* It is "consistent with equals", as defined by {@link Comparable}. |
|
1841 |
* <p> |
|
1842 |
* If all the dates being compared are instances of {@code LocalDate}, |
|
1843 |
* then the comparison will be entirely based on the date. |
|
1844 |
* If some dates being compared are in different chronologies, then the |
|
15658 | 1845 |
* chronology is also considered, see {@link java.time.chrono.ChronoLocalDate#compareTo}. |
15289 | 1846 |
* |
1847 |
* @param other the other date to compare to, not null |
|
1848 |
* @return the comparator value, negative if less, positive if greater |
|
1849 |
*/ |
|
1850 |
@Override // override for Javadoc and performance |
|
19030 | 1851 |
public int compareTo(ChronoLocalDate other) { |
15289 | 1852 |
if (other instanceof LocalDate) { |
1853 |
return compareTo0((LocalDate) other); |
|
1854 |
} |
|
1855 |
return ChronoLocalDate.super.compareTo(other); |
|
1856 |
} |
|
1857 |
||
1858 |
int compareTo0(LocalDate otherDate) { |
|
1859 |
int cmp = (year - otherDate.year); |
|
1860 |
if (cmp == 0) { |
|
1861 |
cmp = (month - otherDate.month); |
|
1862 |
if (cmp == 0) { |
|
1863 |
cmp = (day - otherDate.day); |
|
1864 |
} |
|
1865 |
} |
|
1866 |
return cmp; |
|
1867 |
} |
|
1868 |
||
1869 |
/** |
|
1870 |
* Checks if this date is after the specified date. |
|
1871 |
* <p> |
|
1872 |
* This checks to see if this date represents a point on the |
|
1873 |
* local time-line after the other date. |
|
1874 |
* <pre> |
|
1875 |
* LocalDate a = LocalDate.of(2012, 6, 30); |
|
1876 |
* LocalDate b = LocalDate.of(2012, 7, 1); |
|
1877 |
* a.isAfter(b) == false |
|
1878 |
* a.isAfter(a) == false |
|
1879 |
* b.isAfter(a) == true |
|
1880 |
* </pre> |
|
1881 |
* <p> |
|
1882 |
* This method only considers the position of the two dates on the local time-line. |
|
1883 |
* It does not take into account the chronology, or calendar system. |
|
1884 |
* This is different from the comparison in {@link #compareTo(ChronoLocalDate)}, |
|
16852 | 1885 |
* but is the same approach as {@link ChronoLocalDate#timeLineOrder()}. |
15289 | 1886 |
* |
1887 |
* @param other the other date to compare to, not null |
|
1888 |
* @return true if this date is after the specified date |
|
1889 |
*/ |
|
1890 |
@Override // override for Javadoc and performance |
|
19030 | 1891 |
public boolean isAfter(ChronoLocalDate other) { |
15289 | 1892 |
if (other instanceof LocalDate) { |
1893 |
return compareTo0((LocalDate) other) > 0; |
|
1894 |
} |
|
1895 |
return ChronoLocalDate.super.isAfter(other); |
|
1896 |
} |
|
1897 |
||
1898 |
/** |
|
1899 |
* Checks if this date is before the specified date. |
|
1900 |
* <p> |
|
1901 |
* This checks to see if this date represents a point on the |
|
1902 |
* local time-line before the other date. |
|
1903 |
* <pre> |
|
1904 |
* LocalDate a = LocalDate.of(2012, 6, 30); |
|
1905 |
* LocalDate b = LocalDate.of(2012, 7, 1); |
|
1906 |
* a.isBefore(b) == true |
|
1907 |
* a.isBefore(a) == false |
|
1908 |
* b.isBefore(a) == false |
|
1909 |
* </pre> |
|
1910 |
* <p> |
|
1911 |
* This method only considers the position of the two dates on the local time-line. |
|
1912 |
* It does not take into account the chronology, or calendar system. |
|
1913 |
* This is different from the comparison in {@link #compareTo(ChronoLocalDate)}, |
|
16852 | 1914 |
* but is the same approach as {@link ChronoLocalDate#timeLineOrder()}. |
15289 | 1915 |
* |
1916 |
* @param other the other date to compare to, not null |
|
1917 |
* @return true if this date is before the specified date |
|
1918 |
*/ |
|
1919 |
@Override // override for Javadoc and performance |
|
19030 | 1920 |
public boolean isBefore(ChronoLocalDate other) { |
15289 | 1921 |
if (other instanceof LocalDate) { |
1922 |
return compareTo0((LocalDate) other) < 0; |
|
1923 |
} |
|
1924 |
return ChronoLocalDate.super.isBefore(other); |
|
1925 |
} |
|
1926 |
||
1927 |
/** |
|
1928 |
* Checks if this date is equal to the specified date. |
|
1929 |
* <p> |
|
1930 |
* This checks to see if this date represents the same point on the |
|
1931 |
* local time-line as the other date. |
|
1932 |
* <pre> |
|
1933 |
* LocalDate a = LocalDate.of(2012, 6, 30); |
|
1934 |
* LocalDate b = LocalDate.of(2012, 7, 1); |
|
1935 |
* a.isEqual(b) == false |
|
1936 |
* a.isEqual(a) == true |
|
1937 |
* b.isEqual(a) == false |
|
1938 |
* </pre> |
|
1939 |
* <p> |
|
1940 |
* This method only considers the position of the two dates on the local time-line. |
|
1941 |
* It does not take into account the chronology, or calendar system. |
|
1942 |
* This is different from the comparison in {@link #compareTo(ChronoLocalDate)} |
|
16852 | 1943 |
* but is the same approach as {@link ChronoLocalDate#timeLineOrder()}. |
15289 | 1944 |
* |
1945 |
* @param other the other date to compare to, not null |
|
1946 |
* @return true if this date is equal to the specified date |
|
1947 |
*/ |
|
1948 |
@Override // override for Javadoc and performance |
|
19030 | 1949 |
public boolean isEqual(ChronoLocalDate other) { |
15289 | 1950 |
if (other instanceof LocalDate) { |
1951 |
return compareTo0((LocalDate) other) == 0; |
|
1952 |
} |
|
1953 |
return ChronoLocalDate.super.isEqual(other); |
|
1954 |
} |
|
1955 |
||
1956 |
//----------------------------------------------------------------------- |
|
1957 |
/** |
|
1958 |
* Checks if this date is equal to another date. |
|
1959 |
* <p> |
|
1960 |
* Compares this {@code LocalDate} with another ensuring that the date is the same. |
|
1961 |
* <p> |
|
1962 |
* Only objects of type {@code LocalDate} are compared, other types return false. |
|
1963 |
* To compare the dates of two {@code TemporalAccessor} instances, including dates |
|
1964 |
* in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator. |
|
1965 |
* |
|
1966 |
* @param obj the object to check, null returns false |
|
1967 |
* @return true if this is equal to the other date |
|
1968 |
*/ |
|
1969 |
@Override |
|
1970 |
public boolean equals(Object obj) { |
|
1971 |
if (this == obj) { |
|
1972 |
return true; |
|
1973 |
} |
|
1974 |
if (obj instanceof LocalDate) { |
|
1975 |
return compareTo0((LocalDate) obj) == 0; |
|
1976 |
} |
|
1977 |
return false; |
|
1978 |
} |
|
1979 |
||
1980 |
/** |
|
1981 |
* A hash code for this date. |
|
1982 |
* |
|
1983 |
* @return a suitable hash code |
|
1984 |
*/ |
|
1985 |
@Override |
|
1986 |
public int hashCode() { |
|
1987 |
int yearValue = year; |
|
1988 |
int monthValue = month; |
|
1989 |
int dayValue = day; |
|
1990 |
return (yearValue & 0xFFFFF800) ^ ((yearValue << 11) + (monthValue << 6) + (dayValue)); |
|
1991 |
} |
|
1992 |
||
1993 |
//----------------------------------------------------------------------- |
|
1994 |
/** |
|
1995 |
* Outputs this date as a {@code String}, such as {@code 2007-12-03}. |
|
1996 |
* <p> |
|
16852 | 1997 |
* The output will be in the ISO-8601 format {@code uuuu-MM-dd}. |
15289 | 1998 |
* |
1999 |
* @return a string representation of this date, not null |
|
2000 |
*/ |
|
2001 |
@Override |
|
2002 |
public String toString() { |
|
2003 |
int yearValue = year; |
|
2004 |
int monthValue = month; |
|
2005 |
int dayValue = day; |
|
2006 |
int absYear = Math.abs(yearValue); |
|
2007 |
StringBuilder buf = new StringBuilder(10); |
|
2008 |
if (absYear < 1000) { |
|
2009 |
if (yearValue < 0) { |
|
2010 |
buf.append(yearValue - 10000).deleteCharAt(1); |
|
2011 |
} else { |
|
2012 |
buf.append(yearValue + 10000).deleteCharAt(0); |
|
2013 |
} |
|
2014 |
} else { |
|
2015 |
if (yearValue > 9999) { |
|
2016 |
buf.append('+'); |
|
2017 |
} |
|
2018 |
buf.append(yearValue); |
|
2019 |
} |
|
2020 |
return buf.append(monthValue < 10 ? "-0" : "-") |
|
2021 |
.append(monthValue) |
|
2022 |
.append(dayValue < 10 ? "-0" : "-") |
|
2023 |
.append(dayValue) |
|
2024 |
.toString(); |
|
2025 |
} |
|
2026 |
||
2027 |
//----------------------------------------------------------------------- |
|
2028 |
/** |
|
2029 |
* Writes the object using a |
|
2030 |
* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>. |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
2031 |
* @serialData |
15289 | 2032 |
* <pre> |
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
2033 |
* out.writeByte(3); // identifies a LocalDate |
15289 | 2034 |
* out.writeInt(year); |
2035 |
* out.writeByte(month); |
|
2036 |
* out.writeByte(day); |
|
2037 |
* </pre> |
|
2038 |
* |
|
2039 |
* @return the instance of {@code Ser}, not null |
|
2040 |
*/ |
|
2041 |
private Object writeReplace() { |
|
2042 |
return new Ser(Ser.LOCAL_DATE_TYPE, this); |
|
2043 |
} |
|
2044 |
||
2045 |
/** |
|
2046 |
* Defend against malicious streams. |
|
2047 |
* @return never |
|
2048 |
* @throws InvalidObjectException always |
|
2049 |
*/ |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
2050 |
private Object readResolve() throws InvalidObjectException { |
15289 | 2051 |
throw new InvalidObjectException("Deserialization via serialization delegate"); |
2052 |
} |
|
2053 |
||
2054 |
void writeExternal(DataOutput out) throws IOException { |
|
2055 |
out.writeInt(year); |
|
2056 |
out.writeByte(month); |
|
2057 |
out.writeByte(day); |
|
2058 |
} |
|
2059 |
||
2060 |
static LocalDate readExternal(DataInput in) throws IOException { |
|
2061 |
int year = in.readInt(); |
|
2062 |
int month = in.readByte(); |
|
2063 |
int dayOfMonth = in.readByte(); |
|
2064 |
return LocalDate.of(year, month, dayOfMonth); |
|
2065 |
} |
|
2066 |
||
2067 |
} |