< prev index next >

src/java.base/share/classes/java/time/LocalDate.java

Print this page


   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) {
1374             if (dom <= 28) {
1375                 return new LocalDate(year, month, (int) dom);
1376             } else if (dom <= 59) { // 59th Jan is 28th Feb, 59th Feb is 31st Mar
1377                 long monthLen = lengthOfMonth();
1378                 if (dom <= monthLen) {
1379                     return new LocalDate(year, month, (int) dom);
1380                 } else if (month < 12) {
1381                     return new LocalDate(year, month + 1, (int) (dom - monthLen));
1382                 } else {
1383                     YEAR.checkValidValue(year + 1);
1384                     return new LocalDate(year + 1, 1, (int) (dom - monthLen));
1385                 }
1386             }
1387         }
1388 
1389         long mjDay = Math.addExact(toEpochDay(), daysToAdd);
1390         return LocalDate.ofEpochDay(mjDay);
1391     }
1392 
1393     //-----------------------------------------------------------------------
1394     /**
1395      * Returns a copy of this date with the specified amount subtracted.
1396      * <p>
1397      * This returns a {@code LocalDate}, based on this one, with the specified amount subtracted.
1398      * The amount is typically {@link Period} but may be any other type implementing
1399      * the {@link TemporalAmount} interface.
1400      * <p>
1401      * The calculation is delegated to the amount object by calling
1402      * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
1403      * to implement the subtraction in any way it wishes, however it typically
1404      * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
1405      * of the amount implementation to determine if it can be successfully subtracted.
1406      * <p>
1407      * This instance is immutable and unaffected by this method call.
1408      *


< prev index next >