Add days/weeks/months/years to LocalDate in Java 8 and above examples


You can add days/weeks/months/years to the Java Date Time API LocalDate class object using the below methods,

  1. plus(TemporalAmount amountToAdd): returns LocalDate
  2. plus(long amountToAdd, TemporalUnit unit): returns LocalDate
  3. plusDays(long daysToAdd): returns LocalDate
  4. plusWeeks(Long weeksToAdd): returns LocalDate
  5. plusMonths(long monthsToAdd): returns LocalDate
  6. plusYears(long yearsToAdd): returns LocalDate
Add days weeks months years to LocalDateTime Java

Let's take a look at the code example,

package org.code2care;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class Java8AddDaysToDate {

    public static void main(String[] args) {

        LocalDate localDateToday = LocalDate.now();
        System.out.println("Today: " + localDateToday);

        //Adding one day to today
        System.out.println("Today + 1 day = "+ localDateToday.plusDays(1));

        //Adding one week to today
        System.out.println("Today + 1 week = "+ localDateToday.plusWeeks(1));

        //Adding one month to today
        System.out.println("Today + 1 month = "+ localDateToday.plusMonths(1));

        //Adding one year to today
        System.out.println("Today + 1 year = "+ localDateToday.plusYears(1));

        //Adding days/weeks/months/years using plus(long amountToAdd, TemporalUnit unit)
        System.out.println("Today + 10 days   = "+ localDateToday.plus(10, ChronoUnit.DAYS));
        System.out.println("Today + 10 weeks  = "+ localDateToday.plus(10, ChronoUnit.WEEKS));
        System.out.println("Today + 10 months = "+ localDateToday.plus(10, ChronoUnit.MONTHS));
        System.out.println("Today + 10 years  = "+ localDateToday.plus(10, ChronoUnit.YEARS));

    }
}
Output:
Today: 2022-05-14
Today + 1 day = 2022-05-15
Today + 1 week = 2022-05-21
Today + 1 month = 2022-06-14
Today + 1 year = 2023-05-14
Today + 10 days   = 2022-05-24
Today + 10 weeks  = 2022-07-23
Today + 10 months = 2023-03-14
Today + 10 years  = 2032-05-14
Advertisements
Try Out Code2care Dev Tools:

Advertisements



#Android #Java #Linux #Microsoft #Google #Python #MacOS #Notepad++ #Microsoft Teams #CSS #PHP #SharePoint #Html #Linux #C-Program #Bootstrap #jQuery #Sublime #Android-Studio #Facebook #Eclipse #WhatsApp #News #MySQL #Json #HowTos #JavaScript #FTP #S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency