author | mduigou |
Tue, 05 Nov 2013 19:44:41 -0800 | |
changeset 21615 | 0231a565a5b7 |
parent 21331 | 8ee181e7e48b |
child 22081 | 86eb26ff8f2b |
permissions | -rw-r--r-- |
15289 | 1 |
/* |
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
* This file is available under and governed by the GNU General Public |
|
28 |
* License version 2 only, as published by the Free Software Foundation. |
|
29 |
* However, the following notice accompanied the original version of this |
|
30 |
* file: |
|
31 |
* |
|
32 |
* Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos |
|
33 |
* |
|
34 |
* All rights reserved. |
|
35 |
* |
|
36 |
* Redistribution and use in source and binary forms, with or without |
|
37 |
* modification, are permitted provided that the following conditions are met: |
|
38 |
* |
|
39 |
* * Redistributions of source code must retain the above copyright notice, |
|
40 |
* this list of conditions and the following disclaimer. |
|
41 |
* |
|
42 |
* * Redistributions in binary form must reproduce the above copyright notice, |
|
43 |
* this list of conditions and the following disclaimer in the documentation |
|
44 |
* and/or other materials provided with the distribution. |
|
45 |
* |
|
46 |
* * Neither the name of JSR-310 nor the names of its contributors |
|
47 |
* may be used to endorse or promote products derived from this software |
|
48 |
* without specific prior written permission. |
|
49 |
* |
|
50 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
51 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
52 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
53 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
54 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
55 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
56 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
57 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
58 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
59 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
60 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
61 |
*/ |
|
62 |
package java.time; |
|
63 |
||
64 |
import static java.time.temporal.ChronoUnit.DAYS; |
|
65 |
import static java.time.temporal.ChronoUnit.MONTHS; |
|
66 |
import static java.time.temporal.ChronoUnit.YEARS; |
|
67 |
||
15658 | 68 |
import java.io.DataInput; |
69 |
import java.io.DataOutput; |
|
70 |
import java.io.IOException; |
|
71 |
import java.io.InvalidObjectException; |
|
15289 | 72 |
import java.io.Serializable; |
15658 | 73 |
import java.time.chrono.ChronoLocalDate; |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
74 |
import java.time.chrono.ChronoPeriod; |
15658 | 75 |
import java.time.chrono.Chronology; |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
76 |
import java.time.chrono.IsoChronology; |
15289 | 77 |
import java.time.format.DateTimeParseException; |
78 |
import java.time.temporal.ChronoUnit; |
|
79 |
import java.time.temporal.Temporal; |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
80 |
import java.time.temporal.TemporalAccessor; |
15658 | 81 |
import java.time.temporal.TemporalAmount; |
20795 | 82 |
import java.time.temporal.TemporalQueries; |
15289 | 83 |
import java.time.temporal.TemporalUnit; |
16852 | 84 |
import java.time.temporal.UnsupportedTemporalTypeException; |
15658 | 85 |
import java.util.Arrays; |
86 |
import java.util.Collections; |
|
87 |
import java.util.List; |
|
15289 | 88 |
import java.util.Objects; |
15658 | 89 |
import java.util.regex.Matcher; |
90 |
import java.util.regex.Pattern; |
|
15289 | 91 |
|
92 |
/** |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
93 |
* A date-based amount of time in the ISO-8601 calendar system, |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
94 |
* such as '2 years, 3 months and 4 days'. |
15658 | 95 |
* <p> |
96 |
* This class models a quantity or amount of time in terms of years, months and days. |
|
97 |
* See {@link Duration} for the time-based equivalent to this class. |
|
98 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
99 |
* Durations and periods differ in their treatment of daylight savings time |
15658 | 100 |
* when added to {@link ZonedDateTime}. A {@code Duration} will add an exact |
101 |
* number of seconds, thus a duration of one day is always exactly 24 hours. |
|
102 |
* By contrast, a {@code Period} will add a conceptual day, trying to maintain |
|
103 |
* the local time. |
|
15289 | 104 |
* <p> |
15658 | 105 |
* For example, consider adding a period of one day and a duration of one day to |
106 |
* 18:00 on the evening before a daylight savings gap. The {@code Period} will add |
|
107 |
* the conceptual day and result in a {@code ZonedDateTime} at 18:00 the following day. |
|
108 |
* By contrast, the {@code Duration} will add exactly 24 hours, resulting in a |
|
109 |
* {@code ZonedDateTime} at 19:00 the following day (assuming a one hour DST gap). |
|
110 |
* <p> |
|
111 |
* The supported units of a period are {@link ChronoUnit#YEARS YEARS}, |
|
112 |
* {@link ChronoUnit#MONTHS MONTHS} and {@link ChronoUnit#DAYS DAYS}. |
|
113 |
* All three fields are always present, but may be set to zero. |
|
114 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
115 |
* The ISO-8601 calendar system is the modern civil calendar system used today |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
116 |
* in most of the world. It is equivalent to the proleptic Gregorian calendar |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
117 |
* system, in which today's rules for leap years are applied for all time. |
15289 | 118 |
* <p> |
119 |
* The period is modeled as a directed amount of time, meaning that individual parts of the |
|
120 |
* period may be negative. |
|
121 |
* |
|
17474 | 122 |
* @implSpec |
15289 | 123 |
* This class is immutable and thread-safe. |
124 |
* |
|
125 |
* @since 1.8 |
|
126 |
*/ |
|
127 |
public final class Period |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
128 |
implements ChronoPeriod, Serializable { |
15289 | 129 |
|
130 |
/** |
|
131 |
* A constant for a period of zero. |
|
132 |
*/ |
|
15658 | 133 |
public static final Period ZERO = new Period(0, 0, 0); |
15289 | 134 |
/** |
135 |
* Serialization version. |
|
136 |
*/ |
|
15658 | 137 |
private static final long serialVersionUID = -3587258372562876L; |
138 |
/** |
|
139 |
* The pattern for parsing. |
|
140 |
*/ |
|
21331
8ee181e7e48b
8024686: Cleanup of java.time serialization source
rriggs
parents:
20795
diff
changeset
|
141 |
private static final Pattern PATTERN = |
19030 | 142 |
Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?", Pattern.CASE_INSENSITIVE); |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
143 |
|
15658 | 144 |
/** |
145 |
* The set of supported units. |
|
146 |
*/ |
|
21331
8ee181e7e48b
8024686: Cleanup of java.time serialization source
rriggs
parents:
20795
diff
changeset
|
147 |
private static final List<TemporalUnit> SUPPORTED_UNITS = |
15658 | 148 |
Collections.unmodifiableList(Arrays.<TemporalUnit>asList(YEARS, MONTHS, DAYS)); |
15289 | 149 |
|
150 |
/** |
|
151 |
* The number of years. |
|
152 |
*/ |
|
153 |
private final int years; |
|
154 |
/** |
|
155 |
* The number of months. |
|
156 |
*/ |
|
157 |
private final int months; |
|
158 |
/** |
|
159 |
* The number of days. |
|
160 |
*/ |
|
161 |
private final int days; |
|
162 |
||
163 |
//----------------------------------------------------------------------- |
|
164 |
/** |
|
15658 | 165 |
* Obtains a {@code Period} representing a number of years. |
15289 | 166 |
* <p> |
15658 | 167 |
* The resulting period will have the specified years. |
168 |
* The months and days units will be zero. |
|
15289 | 169 |
* |
15658 | 170 |
* @param years the number of years, positive or negative |
171 |
* @return the period of years, not null |
|
15289 | 172 |
*/ |
15658 | 173 |
public static Period ofYears(int years) { |
174 |
return create(years, 0, 0); |
|
15289 | 175 |
} |
176 |
||
177 |
/** |
|
15658 | 178 |
* Obtains a {@code Period} representing a number of months. |
15289 | 179 |
* <p> |
15658 | 180 |
* The resulting period will have the specified months. |
181 |
* The years and days units will be zero. |
|
15289 | 182 |
* |
15658 | 183 |
* @param months the number of months, positive or negative |
184 |
* @return the period of months, not null |
|
15289 | 185 |
*/ |
15658 | 186 |
public static Period ofMonths(int months) { |
187 |
return create(0, months, 0); |
|
188 |
} |
|
189 |
||
190 |
/** |
|
19030 | 191 |
* Obtains a {@code Period} representing a number of weeks. |
192 |
* <p> |
|
193 |
* The resulting period will be day-based, with the amount of days |
|
194 |
* equal to the number of weeks multiplied by 7. |
|
195 |
* The years and months units will be zero. |
|
196 |
* |
|
197 |
* @param weeks the number of weeks, positive or negative |
|
198 |
* @return the period, with the input weeks converted to days, not null |
|
199 |
*/ |
|
200 |
public static Period ofWeeks(int weeks) { |
|
201 |
return create(0, 0, Math.multiplyExact(weeks, 7)); |
|
202 |
} |
|
203 |
||
204 |
/** |
|
15658 | 205 |
* Obtains a {@code Period} representing a number of days. |
206 |
* <p> |
|
207 |
* The resulting period will have the specified days. |
|
208 |
* The years and months units will be zero. |
|
209 |
* |
|
210 |
* @param days the number of days, positive or negative |
|
211 |
* @return the period of days, not null |
|
212 |
*/ |
|
213 |
public static Period ofDays(int days) { |
|
214 |
return create(0, 0, days); |
|
15289 | 215 |
} |
216 |
||
217 |
//----------------------------------------------------------------------- |
|
218 |
/** |
|
15658 | 219 |
* Obtains a {@code Period} representing a number of years, months and days. |
15289 | 220 |
* <p> |
221 |
* This creates an instance based on years, months and days. |
|
222 |
* |
|
223 |
* @param years the amount of years, may be negative |
|
224 |
* @param months the amount of months, may be negative |
|
225 |
* @param days the amount of days, may be negative |
|
15658 | 226 |
* @return the period of years, months and days, not null |
15289 | 227 |
*/ |
15658 | 228 |
public static Period of(int years, int months, int days) { |
229 |
return create(years, months, days); |
|
15289 | 230 |
} |
231 |
||
232 |
//----------------------------------------------------------------------- |
|
233 |
/** |
|
16852 | 234 |
* Obtains an instance of {@code Period} from a temporal amount. |
15289 | 235 |
* <p> |
16852 | 236 |
* This obtains a period based on the specified amount. |
237 |
* A {@code TemporalAmount} represents an amount of time, which may be |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
238 |
* date-based or time-based, which this factory extracts to a {@code Period}. |
15289 | 239 |
* <p> |
16852 | 240 |
* The conversion loops around the set of units from the amount and uses |
241 |
* the {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS} |
|
242 |
* and {@link ChronoUnit#DAYS DAYS} units to create a period. |
|
243 |
* If any other units are found then an exception is thrown. |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
244 |
* <p> |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
245 |
* If the amount is a {@code ChronoPeriod} then it must use the ISO chronology. |
15289 | 246 |
* |
16852 | 247 |
* @param amount the temporal amount to convert, not null |
248 |
* @return the equivalent period, not null |
|
249 |
* @throws DateTimeException if unable to convert to a {@code Period} |
|
250 |
* @throws ArithmeticException if the amount of years, months or days exceeds an int |
|
15289 | 251 |
*/ |
16852 | 252 |
public static Period from(TemporalAmount amount) { |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
253 |
if (amount instanceof Period) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
254 |
return (Period) amount; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
255 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
256 |
if (amount instanceof ChronoPeriod) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
257 |
if (IsoChronology.INSTANCE.equals(((ChronoPeriod) amount).getChronology()) == false) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
258 |
throw new DateTimeException("Period requires ISO chronology: " + amount); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
259 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
260 |
} |
16852 | 261 |
Objects.requireNonNull(amount, "amount"); |
262 |
int years = 0; |
|
263 |
int months = 0; |
|
264 |
int days = 0; |
|
265 |
for (TemporalUnit unit : amount.getUnits()) { |
|
266 |
long unitAmount = amount.get(unit); |
|
267 |
if (unit == ChronoUnit.YEARS) { |
|
268 |
years = Math.toIntExact(unitAmount); |
|
269 |
} else if (unit == ChronoUnit.MONTHS) { |
|
270 |
months = Math.toIntExact(unitAmount); |
|
271 |
} else if (unit == ChronoUnit.DAYS) { |
|
272 |
days = Math.toIntExact(unitAmount); |
|
273 |
} else { |
|
274 |
throw new DateTimeException("Unit must be Years, Months or Days, but was " + unit); |
|
275 |
} |
|
276 |
} |
|
277 |
return create(years, months, days); |
|
15289 | 278 |
} |
279 |
||
280 |
//----------------------------------------------------------------------- |
|
281 |
/** |
|
15658 | 282 |
* Obtains a {@code Period} from a text string such as {@code PnYnMnD}. |
15289 | 283 |
* <p> |
284 |
* This will parse the string produced by {@code toString()} which is |
|
19030 | 285 |
* based on the ISO-8601 period formats {@code PnYnMnD} and {@code PnW}. |
15289 | 286 |
* <p> |
15658 | 287 |
* The string starts with an optional sign, denoted by the ASCII negative |
288 |
* or positive symbol. If negative, the whole period is negated. |
|
289 |
* The ASCII letter "P" is next in upper or lower case. |
|
19030 | 290 |
* There are then four sections, each consisting of a number and a suffix. |
291 |
* At least one of the four sections must be present. |
|
292 |
* The sections have suffixes in ASCII of "Y", "M", "W" and "D" for |
|
293 |
* years, months, weeks and days, accepted in upper or lower case. |
|
15658 | 294 |
* The suffixes must occur in order. |
295 |
* The number part of each section must consist of ASCII digits. |
|
296 |
* The number may be prefixed by the ASCII negative or positive symbol. |
|
297 |
* The number must parse to an {@code int}. |
|
298 |
* <p> |
|
299 |
* The leading plus/minus sign, and negative values for other units are |
|
19030 | 300 |
* not part of the ISO-8601 standard. In addition, ISO-8601 does not |
301 |
* permit mixing between the {@code PnYnMnD} and {@code PnW} formats. |
|
302 |
* Any week-based input is multiplied by 7 and treated as a number of days. |
|
303 |
* <p> |
|
304 |
* For example, the following are valid inputs: |
|
305 |
* <pre> |
|
306 |
* "P2Y" -- Period.ofYears(2) |
|
307 |
* "P3M" -- Period.ofMonths(3) |
|
308 |
* "P4W" -- Period.ofWeeks(4) |
|
309 |
* "P5D" -- Period.ofDays(5) |
|
310 |
* "P1Y2M3D" -- Period.of(1, 2, 3) |
|
311 |
* "P1Y2M3W4D" -- Period.of(1, 2, 25) |
|
312 |
* "P-1Y2M" -- Period.of(-1, 2, 0) |
|
313 |
* "-P1Y2M" -- Period.of(-1, -2, 0) |
|
314 |
* </pre> |
|
15289 | 315 |
* |
316 |
* @param text the text to parse, not null |
|
317 |
* @return the parsed period, not null |
|
318 |
* @throws DateTimeParseException if the text cannot be parsed to a period |
|
319 |
*/ |
|
15658 | 320 |
public static Period parse(CharSequence text) { |
15289 | 321 |
Objects.requireNonNull(text, "text"); |
15658 | 322 |
Matcher matcher = PATTERN.matcher(text); |
323 |
if (matcher.matches()) { |
|
324 |
int negate = ("-".equals(matcher.group(1)) ? -1 : 1); |
|
325 |
String yearMatch = matcher.group(2); |
|
326 |
String monthMatch = matcher.group(3); |
|
19030 | 327 |
String weekMatch = matcher.group(4); |
328 |
String dayMatch = matcher.group(5); |
|
329 |
if (yearMatch != null || monthMatch != null || dayMatch != null || weekMatch != null) { |
|
15658 | 330 |
try { |
19030 | 331 |
int years = parseNumber(text, yearMatch, negate); |
332 |
int months = parseNumber(text, monthMatch, negate); |
|
333 |
int weeks = parseNumber(text, weekMatch, negate); |
|
334 |
int days = parseNumber(text, dayMatch, negate); |
|
335 |
days = Math.addExact(days, Math.multiplyExact(weeks, 7)); |
|
336 |
return create(years, months, days); |
|
15658 | 337 |
} catch (NumberFormatException ex) { |
19030 | 338 |
throw new DateTimeParseException("Text cannot be parsed to a Period", text, 0, ex); |
15658 | 339 |
} |
340 |
} |
|
341 |
} |
|
342 |
throw new DateTimeParseException("Text cannot be parsed to a Period", text, 0); |
|
343 |
} |
|
344 |
||
345 |
private static int parseNumber(CharSequence text, String str, int negate) { |
|
346 |
if (str == null) { |
|
347 |
return 0; |
|
348 |
} |
|
349 |
int val = Integer.parseInt(str); |
|
350 |
try { |
|
351 |
return Math.multiplyExact(val, negate); |
|
352 |
} catch (ArithmeticException ex) { |
|
19030 | 353 |
throw new DateTimeParseException("Text cannot be parsed to a Period", text, 0, ex); |
15658 | 354 |
} |
15289 | 355 |
} |
356 |
||
357 |
//----------------------------------------------------------------------- |
|
358 |
/** |
|
16852 | 359 |
* Obtains a {@code Period} consisting of the number of years, months, |
360 |
* and days between two dates. |
|
361 |
* <p> |
|
362 |
* The start date is included, but the end date is not. |
|
363 |
* The period is calculated by removing complete months, then calculating |
|
364 |
* the remaining number of days, adjusting to ensure that both have the same sign. |
|
365 |
* The number of months is then split into years and months based on a 12 month year. |
|
366 |
* A month is considered if the end day-of-month is greater than or equal to the start day-of-month. |
|
367 |
* For example, from {@code 2010-01-15} to {@code 2011-03-18} is one year, two months and three days. |
|
368 |
* <p> |
|
369 |
* The result of this method can be a negative period if the end is before the start. |
|
370 |
* The negative sign will be the same in each of year, month and day. |
|
371 |
* |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
372 |
* @param startDateInclusive the start date, inclusive, not null |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
373 |
* @param endDateExclusive the end date, exclusive, not null |
16852 | 374 |
* @return the period between this date and the end date, not null |
19030 | 375 |
* @see ChronoLocalDate#until(ChronoLocalDate) |
16852 | 376 |
*/ |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
377 |
public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
378 |
return startDateInclusive.until(endDateExclusive); |
16852 | 379 |
} |
380 |
||
381 |
//----------------------------------------------------------------------- |
|
382 |
/** |
|
15289 | 383 |
* Creates an instance. |
384 |
* |
|
385 |
* @param years the amount |
|
386 |
* @param months the amount |
|
387 |
* @param days the amount |
|
388 |
*/ |
|
15658 | 389 |
private static Period create(int years, int months, int days) { |
390 |
if ((years | months | days) == 0) { |
|
15289 | 391 |
return ZERO; |
392 |
} |
|
15658 | 393 |
return new Period(years, months, days); |
15289 | 394 |
} |
395 |
||
396 |
/** |
|
397 |
* Constructor. |
|
398 |
* |
|
399 |
* @param years the amount |
|
400 |
* @param months the amount |
|
401 |
* @param days the amount |
|
402 |
*/ |
|
15658 | 403 |
private Period(int years, int months, int days) { |
15289 | 404 |
this.years = years; |
405 |
this.months = months; |
|
406 |
this.days = days; |
|
407 |
} |
|
408 |
||
409 |
//----------------------------------------------------------------------- |
|
410 |
/** |
|
15658 | 411 |
* Gets the value of the requested unit. |
412 |
* <p> |
|
413 |
* This returns a value for each of the three supported units, |
|
414 |
* {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS} and |
|
415 |
* {@link ChronoUnit#DAYS DAYS}. |
|
416 |
* All other units throw an exception. |
|
417 |
* |
|
418 |
* @param unit the {@code TemporalUnit} for which to return the value |
|
419 |
* @return the long value of the unit |
|
420 |
* @throws DateTimeException if the unit is not supported |
|
16852 | 421 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 422 |
*/ |
423 |
@Override |
|
424 |
public long get(TemporalUnit unit) { |
|
425 |
if (unit == ChronoUnit.YEARS) { |
|
426 |
return getYears(); |
|
427 |
} else if (unit == ChronoUnit.MONTHS) { |
|
428 |
return getMonths(); |
|
429 |
} else if (unit == ChronoUnit.DAYS) { |
|
430 |
return getDays(); |
|
431 |
} else { |
|
19030 | 432 |
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); |
15658 | 433 |
} |
434 |
} |
|
435 |
||
436 |
/** |
|
437 |
* Gets the set of units supported by this period. |
|
438 |
* <p> |
|
439 |
* The supported units are {@link ChronoUnit#YEARS YEARS}, |
|
440 |
* {@link ChronoUnit#MONTHS MONTHS} and {@link ChronoUnit#DAYS DAYS}. |
|
441 |
* They are returned in the order years, months, days. |
|
442 |
* <p> |
|
443 |
* This set can be used in conjunction with {@link #get(TemporalUnit)} |
|
444 |
* to access the entire state of the period. |
|
445 |
* |
|
446 |
* @return a list containing the years, months and days units, not null |
|
447 |
*/ |
|
448 |
@Override |
|
449 |
public List<TemporalUnit> getUnits() { |
|
450 |
return SUPPORTED_UNITS; |
|
451 |
} |
|
452 |
||
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
453 |
/** |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
454 |
* Gets the chronology of this period, which is the ISO calendar system. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
455 |
* <p> |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
456 |
* The {@code Chronology} represents the calendar system in use. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
457 |
* The ISO-8601 calendar system is the modern civil calendar system used today |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
458 |
* in most of the world. It is equivalent to the proleptic Gregorian calendar |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
459 |
* system, in which today's rules for leap years are applied for all time. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
460 |
* |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
461 |
* @return the ISO chronology, not null |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
462 |
*/ |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
463 |
@Override |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
464 |
public IsoChronology getChronology() { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
465 |
return IsoChronology.INSTANCE; |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
466 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
467 |
|
15658 | 468 |
//----------------------------------------------------------------------- |
469 |
/** |
|
470 |
* Checks if all three units of this period are zero. |
|
471 |
* <p> |
|
472 |
* A zero period has the value zero for the years, months and days units. |
|
15289 | 473 |
* |
474 |
* @return true if this period is zero-length |
|
475 |
*/ |
|
476 |
public boolean isZero() { |
|
477 |
return (this == ZERO); |
|
478 |
} |
|
479 |
||
480 |
/** |
|
15658 | 481 |
* Checks if any of the three units of this period are negative. |
15289 | 482 |
* <p> |
15658 | 483 |
* This checks whether the years, months or days units are less than zero. |
15289 | 484 |
* |
15658 | 485 |
* @return true if any unit of this period is negative |
15289 | 486 |
*/ |
15658 | 487 |
public boolean isNegative() { |
488 |
return years < 0 || months < 0 || days < 0; |
|
15289 | 489 |
} |
490 |
||
491 |
//----------------------------------------------------------------------- |
|
492 |
/** |
|
493 |
* Gets the amount of years of this period. |
|
15658 | 494 |
* <p> |
495 |
* This returns the years unit. |
|
496 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
497 |
* The months unit is not automatically normalized with the years unit. |
15658 | 498 |
* This means that a period of "15 months" is different to a period |
499 |
* of "1 year and 3 months". |
|
15289 | 500 |
* |
15658 | 501 |
* @return the amount of years of this period, may be negative |
15289 | 502 |
*/ |
503 |
public int getYears() { |
|
504 |
return years; |
|
505 |
} |
|
506 |
||
507 |
/** |
|
508 |
* Gets the amount of months of this period. |
|
15658 | 509 |
* <p> |
510 |
* This returns the months unit. |
|
511 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
512 |
* The months unit is not automatically normalized with the years unit. |
15658 | 513 |
* This means that a period of "15 months" is different to a period |
514 |
* of "1 year and 3 months". |
|
15289 | 515 |
* |
15658 | 516 |
* @return the amount of months of this period, may be negative |
15289 | 517 |
*/ |
518 |
public int getMonths() { |
|
519 |
return months; |
|
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* Gets the amount of days of this period. |
|
15658 | 524 |
* <p> |
525 |
* This returns the days unit. |
|
15289 | 526 |
* |
15658 | 527 |
* @return the amount of days of this period, may be negative |
15289 | 528 |
*/ |
529 |
public int getDays() { |
|
530 |
return days; |
|
531 |
} |
|
532 |
||
533 |
//----------------------------------------------------------------------- |
|
534 |
/** |
|
535 |
* Returns a copy of this period with the specified amount of years. |
|
536 |
* <p> |
|
15658 | 537 |
* This sets the amount of the years unit in a copy of this period. |
538 |
* The months and days units are unaffected. |
|
539 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
540 |
* The months unit is not automatically normalized with the years unit. |
15658 | 541 |
* This means that a period of "15 months" is different to a period |
542 |
* of "1 year and 3 months". |
|
15289 | 543 |
* <p> |
544 |
* This instance is immutable and unaffected by this method call. |
|
545 |
* |
|
15658 | 546 |
* @param years the years to represent, may be negative |
15289 | 547 |
* @return a {@code Period} based on this period with the requested years, not null |
548 |
*/ |
|
549 |
public Period withYears(int years) { |
|
550 |
if (years == this.years) { |
|
551 |
return this; |
|
552 |
} |
|
15658 | 553 |
return create(years, months, days); |
15289 | 554 |
} |
555 |
||
556 |
/** |
|
557 |
* Returns a copy of this period with the specified amount of months. |
|
558 |
* <p> |
|
15658 | 559 |
* This sets the amount of the months unit in a copy of this period. |
560 |
* The years and days units are unaffected. |
|
561 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
562 |
* The months unit is not automatically normalized with the years unit. |
15658 | 563 |
* This means that a period of "15 months" is different to a period |
564 |
* of "1 year and 3 months". |
|
15289 | 565 |
* <p> |
566 |
* This instance is immutable and unaffected by this method call. |
|
567 |
* |
|
15658 | 568 |
* @param months the months to represent, may be negative |
15289 | 569 |
* @return a {@code Period} based on this period with the requested months, not null |
570 |
*/ |
|
571 |
public Period withMonths(int months) { |
|
572 |
if (months == this.months) { |
|
573 |
return this; |
|
574 |
} |
|
15658 | 575 |
return create(years, months, days); |
15289 | 576 |
} |
577 |
||
578 |
/** |
|
579 |
* Returns a copy of this period with the specified amount of days. |
|
580 |
* <p> |
|
15658 | 581 |
* This sets the amount of the days unit in a copy of this period. |
582 |
* The years and months units are unaffected. |
|
15289 | 583 |
* <p> |
584 |
* This instance is immutable and unaffected by this method call. |
|
585 |
* |
|
15658 | 586 |
* @param days the days to represent, may be negative |
15289 | 587 |
* @return a {@code Period} based on this period with the requested days, not null |
588 |
*/ |
|
589 |
public Period withDays(int days) { |
|
590 |
if (days == this.days) { |
|
591 |
return this; |
|
592 |
} |
|
15658 | 593 |
return create(years, months, days); |
15289 | 594 |
} |
595 |
||
596 |
//----------------------------------------------------------------------- |
|
597 |
/** |
|
598 |
* Returns a copy of this period with the specified period added. |
|
599 |
* <p> |
|
16852 | 600 |
* This operates separately on the years, months and days. |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
601 |
* No normalization is performed. |
15289 | 602 |
* <p> |
15658 | 603 |
* For example, "1 year, 6 months and 3 days" plus "2 years, 2 months and 2 days" |
604 |
* returns "3 years, 8 months and 5 days". |
|
605 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
606 |
* The specified amount is typically an instance of {@code Period}. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
607 |
* Other types are interpreted using {@link Period#from(TemporalAmount)}. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
608 |
* <p> |
15289 | 609 |
* This instance is immutable and unaffected by this method call. |
610 |
* |
|
15658 | 611 |
* @param amountToAdd the period to add, not null |
15289 | 612 |
* @return a {@code Period} based on this period with the requested period added, not null |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
613 |
* @throws DateTimeException if the specified amount has a non-ISO chronology or |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
614 |
* contains an invalid unit |
15289 | 615 |
* @throws ArithmeticException if numeric overflow occurs |
616 |
*/ |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
617 |
public Period plus(TemporalAmount amountToAdd) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
618 |
Period isoAmount = Period.from(amountToAdd); |
15289 | 619 |
return create( |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
620 |
Math.addExact(years, isoAmount.years), |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
621 |
Math.addExact(months, isoAmount.months), |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
622 |
Math.addExact(days, isoAmount.days)); |
15289 | 623 |
} |
624 |
||
625 |
/** |
|
15658 | 626 |
* Returns a copy of this period with the specified years added. |
15289 | 627 |
* <p> |
15658 | 628 |
* This adds the amount to the years unit in a copy of this period. |
629 |
* The months and days units are unaffected. |
|
630 |
* For example, "1 year, 6 months and 3 days" plus 2 years returns "3 years, 6 months and 3 days". |
|
15289 | 631 |
* <p> |
632 |
* This instance is immutable and unaffected by this method call. |
|
633 |
* |
|
15658 | 634 |
* @param yearsToAdd the years to add, positive or negative |
635 |
* @return a {@code Period} based on this period with the specified years added, not null |
|
636 |
* @throws ArithmeticException if numeric overflow occurs |
|
637 |
*/ |
|
638 |
public Period plusYears(long yearsToAdd) { |
|
639 |
if (yearsToAdd == 0) { |
|
640 |
return this; |
|
641 |
} |
|
642 |
return create(Math.toIntExact(Math.addExact(years, yearsToAdd)), months, days); |
|
643 |
} |
|
644 |
||
645 |
/** |
|
646 |
* Returns a copy of this period with the specified months added. |
|
647 |
* <p> |
|
648 |
* This adds the amount to the months unit in a copy of this period. |
|
649 |
* The years and days units are unaffected. |
|
650 |
* For example, "1 year, 6 months and 3 days" plus 2 months returns "1 year, 8 months and 3 days". |
|
651 |
* <p> |
|
652 |
* This instance is immutable and unaffected by this method call. |
|
653 |
* |
|
654 |
* @param monthsToAdd the months to add, positive or negative |
|
655 |
* @return a {@code Period} based on this period with the specified months added, not null |
|
15289 | 656 |
* @throws ArithmeticException if numeric overflow occurs |
657 |
*/ |
|
15658 | 658 |
public Period plusMonths(long monthsToAdd) { |
659 |
if (monthsToAdd == 0) { |
|
660 |
return this; |
|
15289 | 661 |
} |
15658 | 662 |
return create(years, Math.toIntExact(Math.addExact(months, monthsToAdd)), days); |
15289 | 663 |
} |
664 |
||
15658 | 665 |
/** |
666 |
* Returns a copy of this period with the specified days added. |
|
667 |
* <p> |
|
668 |
* This adds the amount to the days unit in a copy of this period. |
|
669 |
* The years and months units are unaffected. |
|
670 |
* For example, "1 year, 6 months and 3 days" plus 2 days returns "1 year, 6 months and 5 days". |
|
671 |
* <p> |
|
672 |
* This instance is immutable and unaffected by this method call. |
|
673 |
* |
|
674 |
* @param daysToAdd the days to add, positive or negative |
|
675 |
* @return a {@code Period} based on this period with the specified days added, not null |
|
676 |
* @throws ArithmeticException if numeric overflow occurs |
|
677 |
*/ |
|
678 |
public Period plusDays(long daysToAdd) { |
|
679 |
if (daysToAdd == 0) { |
|
680 |
return this; |
|
681 |
} |
|
682 |
return create(years, months, Math.toIntExact(Math.addExact(days, daysToAdd))); |
|
15289 | 683 |
} |
684 |
||
685 |
//----------------------------------------------------------------------- |
|
686 |
/** |
|
687 |
* Returns a copy of this period with the specified period subtracted. |
|
688 |
* <p> |
|
16852 | 689 |
* This operates separately on the years, months and days. |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
690 |
* No normalization is performed. |
15289 | 691 |
* <p> |
15658 | 692 |
* For example, "1 year, 6 months and 3 days" minus "2 years, 2 months and 2 days" |
693 |
* returns "-1 years, 4 months and 1 day". |
|
694 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
695 |
* The specified amount is typically an instance of {@code Period}. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
696 |
* Other types are interpreted using {@link Period#from(TemporalAmount)}. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
697 |
* <p> |
15289 | 698 |
* This instance is immutable and unaffected by this method call. |
699 |
* |
|
15658 | 700 |
* @param amountToSubtract the period to subtract, not null |
15289 | 701 |
* @return a {@code Period} based on this period with the requested period subtracted, not null |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
702 |
* @throws DateTimeException if the specified amount has a non-ISO chronology or |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
703 |
* contains an invalid unit |
15289 | 704 |
* @throws ArithmeticException if numeric overflow occurs |
705 |
*/ |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
706 |
public Period minus(TemporalAmount amountToSubtract) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
707 |
Period isoAmount = Period.from(amountToSubtract); |
15289 | 708 |
return create( |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
709 |
Math.subtractExact(years, isoAmount.years), |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
710 |
Math.subtractExact(months, isoAmount.months), |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
711 |
Math.subtractExact(days, isoAmount.days)); |
15289 | 712 |
} |
713 |
||
714 |
/** |
|
15658 | 715 |
* Returns a copy of this period with the specified years subtracted. |
15289 | 716 |
* <p> |
15658 | 717 |
* This subtracts the amount from the years unit in a copy of this period. |
718 |
* The months and days units are unaffected. |
|
719 |
* For example, "1 year, 6 months and 3 days" minus 2 years returns "-1 years, 6 months and 3 days". |
|
15289 | 720 |
* <p> |
721 |
* This instance is immutable and unaffected by this method call. |
|
722 |
* |
|
15658 | 723 |
* @param yearsToSubtract the years to subtract, positive or negative |
724 |
* @return a {@code Period} based on this period with the specified years subtracted, not null |
|
15289 | 725 |
* @throws ArithmeticException if numeric overflow occurs |
726 |
*/ |
|
15658 | 727 |
public Period minusYears(long yearsToSubtract) { |
728 |
return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract)); |
|
15289 | 729 |
} |
730 |
||
15658 | 731 |
/** |
732 |
* Returns a copy of this period with the specified months subtracted. |
|
733 |
* <p> |
|
734 |
* This subtracts the amount from the months unit in a copy of this period. |
|
735 |
* The years and days units are unaffected. |
|
736 |
* For example, "1 year, 6 months and 3 days" minus 2 months returns "1 year, 4 months and 3 days". |
|
737 |
* <p> |
|
738 |
* This instance is immutable and unaffected by this method call. |
|
739 |
* |
|
740 |
* @param monthsToSubtract the years to subtract, positive or negative |
|
741 |
* @return a {@code Period} based on this period with the specified months subtracted, not null |
|
742 |
* @throws ArithmeticException if numeric overflow occurs |
|
743 |
*/ |
|
744 |
public Period minusMonths(long monthsToSubtract) { |
|
745 |
return (monthsToSubtract == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-monthsToSubtract)); |
|
15289 | 746 |
} |
747 |
||
15658 | 748 |
/** |
749 |
* Returns a copy of this period with the specified days subtracted. |
|
750 |
* <p> |
|
751 |
* This subtracts the amount from the days unit in a copy of this period. |
|
752 |
* The years and months units are unaffected. |
|
753 |
* For example, "1 year, 6 months and 3 days" minus 2 days returns "1 year, 6 months and 1 day". |
|
754 |
* <p> |
|
755 |
* This instance is immutable and unaffected by this method call. |
|
756 |
* |
|
757 |
* @param daysToSubtract the months to subtract, positive or negative |
|
758 |
* @return a {@code Period} based on this period with the specified days subtracted, not null |
|
759 |
* @throws ArithmeticException if numeric overflow occurs |
|
760 |
*/ |
|
761 |
public Period minusDays(long daysToSubtract) { |
|
762 |
return (daysToSubtract == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-daysToSubtract)); |
|
15289 | 763 |
} |
764 |
||
765 |
//----------------------------------------------------------------------- |
|
766 |
/** |
|
767 |
* Returns a new instance with each element in this period multiplied |
|
768 |
* by the specified scalar. |
|
769 |
* <p> |
|
15658 | 770 |
* This returns a period with each of the years, months and days units |
771 |
* individually multiplied. |
|
772 |
* For example, a period of "2 years, -3 months and 4 days" multiplied by |
|
773 |
* 3 will return "6 years, -9 months and 12 days". |
|
774 |
* No normalization is performed. |
|
15289 | 775 |
* |
776 |
* @param scalar the scalar to multiply by, not null |
|
777 |
* @return a {@code Period} based on this period with the amounts multiplied by the scalar, not null |
|
778 |
* @throws ArithmeticException if numeric overflow occurs |
|
779 |
*/ |
|
780 |
public Period multipliedBy(int scalar) { |
|
781 |
if (this == ZERO || scalar == 1) { |
|
782 |
return this; |
|
783 |
} |
|
784 |
return create( |
|
785 |
Math.multiplyExact(years, scalar), |
|
786 |
Math.multiplyExact(months, scalar), |
|
15658 | 787 |
Math.multiplyExact(days, scalar)); |
15289 | 788 |
} |
789 |
||
790 |
/** |
|
791 |
* Returns a new instance with each amount in this period negated. |
|
15658 | 792 |
* <p> |
793 |
* This returns a period with each of the years, months and days units |
|
794 |
* individually negated. |
|
795 |
* For example, a period of "2 years, -3 months and 4 days" will be |
|
796 |
* negated to "-2 years, 3 months and -4 days". |
|
797 |
* No normalization is performed. |
|
15289 | 798 |
* |
799 |
* @return a {@code Period} based on this period with the amounts negated, not null |
|
15658 | 800 |
* @throws ArithmeticException if numeric overflow occurs, which only happens if |
801 |
* one of the units has the value {@code Long.MIN_VALUE} |
|
15289 | 802 |
*/ |
803 |
public Period negated() { |
|
804 |
return multipliedBy(-1); |
|
805 |
} |
|
806 |
||
807 |
//----------------------------------------------------------------------- |
|
808 |
/** |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
809 |
* Returns a copy of this period with the years and months normalized. |
15289 | 810 |
* <p> |
15658 | 811 |
* This normalizes the years and months units, leaving the days unit unchanged. |
812 |
* The months unit is adjusted to have an absolute value less than 11, |
|
813 |
* with the years unit being adjusted to compensate. For example, a period of |
|
814 |
* "1 Year and 15 months" will be normalized to "2 years and 3 months". |
|
15289 | 815 |
* <p> |
15658 | 816 |
* The sign of the years and months units will be the same after normalization. |
817 |
* For example, a period of "1 year and -25 months" will be normalized to |
|
818 |
* "-1 year and -1 month". |
|
819 |
* <p> |
|
15289 | 820 |
* This instance is immutable and unaffected by this method call. |
821 |
* |
|
15658 | 822 |
* @return a {@code Period} based on this period with excess months normalized to years, not null |
15289 | 823 |
* @throws ArithmeticException if numeric overflow occurs |
824 |
*/ |
|
15658 | 825 |
public Period normalized() { |
826 |
long totalMonths = toTotalMonths(); |
|
15289 | 827 |
long splitYears = totalMonths / 12; |
828 |
int splitMonths = (int) (totalMonths % 12); // no overflow |
|
829 |
if (splitYears == years && splitMonths == months) { |
|
830 |
return this; |
|
831 |
} |
|
15658 | 832 |
return create(Math.toIntExact(splitYears), splitMonths, days); |
15289 | 833 |
} |
834 |
||
835 |
/** |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
836 |
* Gets the total number of months in this period. |
15289 | 837 |
* <p> |
15658 | 838 |
* This returns the total number of months in the period by multiplying the |
839 |
* number of years by 12 and adding the number of months. |
|
840 |
* <p> |
|
15289 | 841 |
* This instance is immutable and unaffected by this method call. |
842 |
* |
|
15658 | 843 |
* @return the total number of months in the period, may be negative |
15289 | 844 |
*/ |
15658 | 845 |
public long toTotalMonths() { |
846 |
return years * 12L + months; // no overflow |
|
15289 | 847 |
} |
848 |
||
849 |
//------------------------------------------------------------------------- |
|
850 |
/** |
|
851 |
* Adds this period to the specified temporal object. |
|
852 |
* <p> |
|
853 |
* This returns a temporal object of the same observable type as the input |
|
854 |
* with this period added. |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
855 |
* If the temporal has a chronology, it must be the ISO chronology. |
15289 | 856 |
* <p> |
857 |
* In most cases, it is clearer to reverse the calling pattern by using |
|
15658 | 858 |
* {@link Temporal#plus(TemporalAmount)}. |
15289 | 859 |
* <pre> |
860 |
* // these two lines are equivalent, but the second approach is recommended |
|
861 |
* dateTime = thisPeriod.addTo(dateTime); |
|
862 |
* dateTime = dateTime.plus(thisPeriod); |
|
863 |
* </pre> |
|
864 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
865 |
* The calculation operates as follows. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
866 |
* First, the chronology of the temporal is checked to ensure it is ISO chronology or null. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
867 |
* Second, if the months are zero, the years are added if non-zero, otherwise |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
868 |
* the combination of years and months is added if non-zero. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
869 |
* Finally, any days are added. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
870 |
* <p> |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
871 |
* This approach ensures that a partial period can be added to a partial date. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
872 |
* For example, a period of years and/or months can be added to a {@code YearMonth}, |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
873 |
* but a period including days cannot. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
874 |
* The approach also adds years and months together when necessary, which ensures |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
875 |
* correct behaviour at the end of the month. |
15289 | 876 |
* <p> |
877 |
* This instance is immutable and unaffected by this method call. |
|
878 |
* |
|
879 |
* @param temporal the temporal object to adjust, not null |
|
880 |
* @return an object of the same type with the adjustment made, not null |
|
881 |
* @throws DateTimeException if unable to add |
|
882 |
* @throws ArithmeticException if numeric overflow occurs |
|
883 |
*/ |
|
884 |
@Override |
|
885 |
public Temporal addTo(Temporal temporal) { |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
886 |
validateChrono(temporal); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
887 |
if (months == 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
888 |
if (years != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
889 |
temporal = temporal.plus(years, YEARS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
890 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
891 |
} else { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
892 |
long totalMonths = toTotalMonths(); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
893 |
if (totalMonths != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
894 |
temporal = temporal.plus(totalMonths, MONTHS); |
15289 | 895 |
} |
896 |
} |
|
897 |
if (days != 0) { |
|
898 |
temporal = temporal.plus(days, DAYS); |
|
899 |
} |
|
900 |
return temporal; |
|
901 |
} |
|
902 |
||
903 |
/** |
|
904 |
* Subtracts this period from the specified temporal object. |
|
905 |
* <p> |
|
906 |
* This returns a temporal object of the same observable type as the input |
|
907 |
* with this period subtracted. |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
908 |
* If the temporal has a chronology, it must be the ISO chronology. |
15289 | 909 |
* <p> |
910 |
* In most cases, it is clearer to reverse the calling pattern by using |
|
15658 | 911 |
* {@link Temporal#minus(TemporalAmount)}. |
15289 | 912 |
* <pre> |
913 |
* // these two lines are equivalent, but the second approach is recommended |
|
914 |
* dateTime = thisPeriod.subtractFrom(dateTime); |
|
915 |
* dateTime = dateTime.minus(thisPeriod); |
|
916 |
* </pre> |
|
917 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
918 |
* The calculation operates as follows. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
919 |
* First, the chronology of the temporal is checked to ensure it is ISO chronology or null. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
920 |
* Second, if the months are zero, the years are subtracted if non-zero, otherwise |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
921 |
* the combination of years and months is subtracted if non-zero. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
922 |
* Finally, any days are subtracted. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
923 |
* <p> |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
924 |
* This approach ensures that a partial period can be subtracted from a partial date. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
925 |
* For example, a period of years and/or months can be subtracted from a {@code YearMonth}, |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
926 |
* but a period including days cannot. |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
927 |
* The approach also subtracts years and months together when necessary, which ensures |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
928 |
* correct behaviour at the end of the month. |
15289 | 929 |
* <p> |
930 |
* This instance is immutable and unaffected by this method call. |
|
931 |
* |
|
932 |
* @param temporal the temporal object to adjust, not null |
|
933 |
* @return an object of the same type with the adjustment made, not null |
|
934 |
* @throws DateTimeException if unable to subtract |
|
935 |
* @throws ArithmeticException if numeric overflow occurs |
|
936 |
*/ |
|
937 |
@Override |
|
938 |
public Temporal subtractFrom(Temporal temporal) { |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
939 |
validateChrono(temporal); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
940 |
if (months == 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
941 |
if (years != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
942 |
temporal = temporal.minus(years, YEARS); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
943 |
} |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
944 |
} else { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
945 |
long totalMonths = toTotalMonths(); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
946 |
if (totalMonths != 0) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
947 |
temporal = temporal.minus(totalMonths, MONTHS); |
15289 | 948 |
} |
949 |
} |
|
950 |
if (days != 0) { |
|
951 |
temporal = temporal.minus(days, DAYS); |
|
952 |
} |
|
953 |
return temporal; |
|
954 |
} |
|
955 |
||
956 |
/** |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
957 |
* Validates that the temporal has the correct chronology. |
15289 | 958 |
*/ |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
959 |
private void validateChrono(TemporalAccessor temporal) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
960 |
Objects.requireNonNull(temporal, "temporal"); |
20795 | 961 |
Chronology temporalChrono = temporal.query(TemporalQueries.chronology()); |
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
962 |
if (temporalChrono != null && IsoChronology.INSTANCE.equals(temporalChrono) == false) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
963 |
throw new DateTimeException("Chronology mismatch, expected: ISO, actual: " + temporalChrono.getId()); |
15289 | 964 |
} |
965 |
} |
|
966 |
||
967 |
//----------------------------------------------------------------------- |
|
968 |
/** |
|
969 |
* Checks if this period is equal to another period. |
|
970 |
* <p> |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
19841
diff
changeset
|
971 |
* The comparison is based on the type {@code Period} and each of the three amounts. |
15658 | 972 |
* To be equal, the years, months and days units must be individually equal. |
973 |
* Note that this means that a period of "15 Months" is not equal to a period |
|
974 |
* of "1 Year and 3 Months". |
|
15289 | 975 |
* |
976 |
* @param obj the object to check, null returns false |
|
977 |
* @return true if this is equal to the other period |
|
978 |
*/ |
|
979 |
@Override |
|
980 |
public boolean equals(Object obj) { |
|
981 |
if (this == obj) { |
|
982 |
return true; |
|
983 |
} |
|
984 |
if (obj instanceof Period) { |
|
985 |
Period other = (Period) obj; |
|
15658 | 986 |
return years == other.years && |
987 |
months == other.months && |
|
988 |
days == other.days; |
|
15289 | 989 |
} |
990 |
return false; |
|
991 |
} |
|
992 |
||
993 |
/** |
|
994 |
* A hash code for this period. |
|
995 |
* |
|
996 |
* @return a suitable hash code |
|
997 |
*/ |
|
998 |
@Override |
|
999 |
public int hashCode() { |
|
15658 | 1000 |
return years + Integer.rotateLeft(months, 8) + Integer.rotateLeft(days, 16); |
15289 | 1001 |
} |
1002 |
||
1003 |
//----------------------------------------------------------------------- |
|
1004 |
/** |
|
15658 | 1005 |
* Outputs this period as a {@code String}, such as {@code P6Y3M1D}. |
15289 | 1006 |
* <p> |
1007 |
* The output will be in the ISO-8601 period format. |
|
15658 | 1008 |
* A zero period will be represented as zero days, 'P0D'. |
15289 | 1009 |
* |
1010 |
* @return a string representation of this period, not null |
|
1011 |
*/ |
|
1012 |
@Override |
|
1013 |
public String toString() { |
|
1014 |
if (this == ZERO) { |
|
15658 | 1015 |
return "P0D"; |
15289 | 1016 |
} else { |
1017 |
StringBuilder buf = new StringBuilder(); |
|
1018 |
buf.append('P'); |
|
1019 |
if (years != 0) { |
|
1020 |
buf.append(years).append('Y'); |
|
1021 |
} |
|
1022 |
if (months != 0) { |
|
1023 |
buf.append(months).append('M'); |
|
1024 |
} |
|
1025 |
if (days != 0) { |
|
1026 |
buf.append(days).append('D'); |
|
1027 |
} |
|
1028 |
return buf.toString(); |
|
1029 |
} |
|
1030 |
} |
|
1031 |
||
15658 | 1032 |
//----------------------------------------------------------------------- |
1033 |
/** |
|
1034 |
* Writes the object using a |
|
1035 |
* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>. |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1036 |
* @serialData |
15658 | 1037 |
* <pre> |
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1038 |
* out.writeByte(14); // identifies a Period |
15658 | 1039 |
* out.writeInt(years); |
1040 |
* out.writeInt(months); |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1041 |
* out.writeInt(days); |
15658 | 1042 |
* </pre> |
1043 |
* |
|
1044 |
* @return the instance of {@code Ser}, not null |
|
1045 |
*/ |
|
1046 |
private Object writeReplace() { |
|
1047 |
return new Ser(Ser.PERIOD_TYPE, this); |
|
1048 |
} |
|
1049 |
||
1050 |
/** |
|
1051 |
* Defend against malicious streams. |
|
1052 |
* @return never |
|
1053 |
* @throws java.io.InvalidObjectException always |
|
1054 |
*/ |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1055 |
private Object readResolve() throws InvalidObjectException { |
15658 | 1056 |
throw new InvalidObjectException("Deserialization via serialization delegate"); |
1057 |
} |
|
1058 |
||
1059 |
void writeExternal(DataOutput out) throws IOException { |
|
1060 |
out.writeInt(years); |
|
1061 |
out.writeInt(months); |
|
1062 |
out.writeInt(days); |
|
1063 |
} |
|
1064 |
||
1065 |
static Period readExternal(DataInput in) throws IOException { |
|
1066 |
int years = in.readInt(); |
|
1067 |
int months = in.readInt(); |
|
1068 |
int days = in.readInt(); |
|
1069 |
return Period.of(years, months, days); |
|
1070 |
} |
|
1071 |
||
15289 | 1072 |
} |