Java LocalDate Calculate plusDays(LocalDate date, int daysToAdd)

Here you can find the source of plusDays(LocalDate date, int daysToAdd)

Description

Adds a number of days to the date.

License

Open Source License

Parameter

Parameter Description
date the date to add to

Return

the new date

Declaration

static LocalDate plusDays(LocalDate date, int daysToAdd) 

Method Source Code

//package com.java2s;
/**//from w  ww  . j a v  a 2  s.co  m
 * Copyright (C) 2015 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */

import java.time.LocalDate;

public class Main {
    /**
     * Adds a number of days to the date.
     * <p>
     * Faster than the JDK method.
     * 
     * @param date  the date to add to
     * @return the new date
     */
    static LocalDate plusDays(LocalDate date, int daysToAdd) {
        if (daysToAdd == 0) {
            return date;
        }
        // add the days to the current day-of-month
        // if it is guaranteed to be in this month or the next month then fast path it
        // (59th Jan is 28th Feb, 59th Feb is 31st Mar)
        long dom = date.getDayOfMonth() + daysToAdd;
        if (dom > 0 && dom <= 59) {
            int monthLen = date.lengthOfMonth();
            int month = date.getMonthValue();
            int year = date.getYear();
            if (dom <= monthLen) {
                return LocalDate.of(year, month, (int) dom);
            } else if (month < 12) {
                return LocalDate.of(year, month + 1, (int) (dom - monthLen));
            } else {
                return LocalDate.of(year + 1, 1, (int) (dom - monthLen));
            }
        }
        long mjDay = Math.addExact(date.toEpochDay(), daysToAdd);
        return LocalDate.ofEpochDay(mjDay);
    }
}

Related

  1. normalizedYear(LocalDate d)
  2. obtenirLocalDateAPartirDUneDate(Date date)
  3. oneDayAfterNotificationRequired(LocalDate requestDate, LocalDate vehicleDetailsMotExpiryDate)
  4. parseDate(LocalDate date)
  5. period(LocalDate startDate, LocalDate endDate)
  6. rebucketingArray(List targetDates, double[] rebucketedSensitivityAmounts, double sensitivityAmount, LocalDate sensitivityDate)
  7. roundDown(final LocalDate dateTime, final TemporalUnit temporalUnit, final int size)
  8. stream(LocalDate startInclusive, LocalDate endExclusive)
  9. stringDateToLocalDate(String day, String month, String year)