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