Java localdate add day

Java 8 – Adding Days to the LocalDate

In this tutorial we will see how to add days to the LocalDate.

Java LocalDate Example 1: Adding Days to the current Date

In this example, we are adding one day to the current date. We are using now() method of LocalDate class to get the current date and then using the plusDays() method to add the specified number of days to the LocalDate.

import java.time.LocalDate; public class Example < public static void main(String[] args) < //current date LocalDate today = LocalDate.now(); //adding one day to the localdate LocalDate tomorrow = today.plusDays(1); System.out.println("Tomorrow's Date: "+tomorrow); >>

Example 2: Adding Days to the given LocalDate

In this example, we have the given date, which we are converting to LocalDate instance using the of() method and then we are adding the days to it using plusDays() method, similar to what we have seen in the above example.

import java.time.LocalDate; public class Example < public static void main(String[] args) < //given date LocalDate localDate = LocalDate.of(2017, 06, 22); //adding 5 days to the given localdate LocalDate newDate = localDate.plusDays(5); System.out.println("Date after addition: "+newDate); >>
Date after addition: 2017-06-27

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

Читайте также:  Php error checking online
Оцените статью