1 /*
2 * Copyright (c) 2012, 2015, 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
1352
1353 /**
1354 * Returns a copy of this {@code LocalDate} with the specified number of days added.
1355 * <p>
1356 * This method adds the specified amount to the days field incrementing the
1357 * month and year fields as necessary to ensure the result remains valid.
1358 * The result is only invalid if the maximum/minimum year is exceeded.
1359 * <p>
1360 * For example, 2008-12-31 plus one day would result in 2009-01-01.
1361 * <p>
1362 * This instance is immutable and unaffected by this method call.
1363 *
1364 * @param daysToAdd the days to add, may be negative
1365 * @return a {@code LocalDate} based on this date with the days added, not null
1366 * @throws DateTimeException if the result exceeds the supported date range
1367 */
1368 public LocalDate plusDays(long daysToAdd) {
1369 if (daysToAdd == 0) {
1370 return this;
1371 }
1372 long mjDay = Math.addExact(toEpochDay(), daysToAdd);
1373 return LocalDate.ofEpochDay(mjDay);
1374 }
1375
1376 //-----------------------------------------------------------------------
1377 /**
1378 * Returns a copy of this date with the specified amount subtracted.
1379 * <p>
1380 * This returns a {@code LocalDate}, based on this one, with the specified amount subtracted.
1381 * The amount is typically {@link Period} but may be any other type implementing
1382 * the {@link TemporalAmount} interface.
1383 * <p>
1384 * The calculation is delegated to the amount object by calling
1385 * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
1386 * to implement the subtraction in any way it wishes, however it typically
1387 * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
1388 * of the amount implementation to determine if it can be successfully subtracted.
1389 * <p>
1390 * This instance is immutable and unaffected by this method call.
1391 *
|
1 /*
2 * Copyright (c) 2012, 2016, 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
1352
1353 /**
1354 * Returns a copy of this {@code LocalDate} with the specified number of days added.
1355 * <p>
1356 * This method adds the specified amount to the days field incrementing the
1357 * month and year fields as necessary to ensure the result remains valid.
1358 * The result is only invalid if the maximum/minimum year is exceeded.
1359 * <p>
1360 * For example, 2008-12-31 plus one day would result in 2009-01-01.
1361 * <p>
1362 * This instance is immutable and unaffected by this method call.
1363 *
1364 * @param daysToAdd the days to add, may be negative
1365 * @return a {@code LocalDate} based on this date with the days added, not null
1366 * @throws DateTimeException if the result exceeds the supported date range
1367 */
1368 public LocalDate plusDays(long daysToAdd) {
1369 if (daysToAdd == 0) {
1370 return this;
1371 }
1372 long dom = day + daysToAdd;
1373 if (dom > 0 && dom <= 28) {
1374 return LocalDate.of(year, month, (int) dom);
1375 } else if (dom > 28 && dom <= 59) { // 59th Jan is 28th Feb, 59th Feb is 31st Mar
1376 long monthLen = lengthOfMonth();
1377 if (dom <= monthLen) {
1378 return LocalDate.of(year, month, (int) dom);
1379 } else if (month < 12) {
1380 return LocalDate.of(year, month + 1, (int) (dom - monthLen));
1381 } else {
1382 return LocalDate.of(year + 1, 1, (int) (dom - monthLen));
1383 }
1384 }
1385
1386 long mjDay = Math.addExact(toEpochDay(), daysToAdd);
1387 return LocalDate.ofEpochDay(mjDay);
1388 }
1389
1390 //-----------------------------------------------------------------------
1391 /**
1392 * Returns a copy of this date with the specified amount subtracted.
1393 * <p>
1394 * This returns a {@code LocalDate}, based on this one, with the specified amount subtracted.
1395 * The amount is typically {@link Period} but may be any other type implementing
1396 * the {@link TemporalAmount} interface.
1397 * <p>
1398 * The calculation is delegated to the amount object by calling
1399 * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
1400 * to implement the subtraction in any way it wishes, however it typically
1401 * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
1402 * of the amount implementation to determine if it can be successfully subtracted.
1403 * <p>
1404 * This instance is immutable and unaffected by this method call.
1405 *
|