You can add days/weeks/months/years to the Java Date Time API LocalDate class object using the below methods,
- plus(TemporalAmount amountToAdd): returns LocalDate
- plus(long amountToAdd, TemporalUnit unit): returns LocalDate
- plusDays(long daysToAdd): returns LocalDate
- plusWeeks(Long weeksToAdd): returns LocalDate
- plusMonths(long monthsToAdd): returns LocalDate
- plusYears(long yearsToAdd): returns LocalDate
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
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- Export aborted because fatal lint error were found - Android
- How to Split a String by Space in Rust - Rust
- How to Split a String in Python? - Python
- StringTokenizer in Java with Examples - Java
- fill_parent vs match_parent vs wrap_content - Android
- Java 8 Streams map() with examples - Java
- How to hide or display Wifi icon in macOS Bug Sur Menu Bar - MacOS
- How to Monitor Network Traffic on Mac Terminal - MacOS