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