Java Add Number of Days to Current LocalDateTime
Tags: Java LocalDateTime
In this Java tutorial, we learn how to add number of days into current LocalDateTime value in Java programming language.
How to add number of days to current LocalDateTime in Java
In Java, we can add the number of days into current LocalDateTime value with the following steps:
- Instantiate current LocalDateTime value using the LocalDateTime.now() static method.
- Using the LocalDateTime.plusDays(long days) method to create new LocalDateTime value with number of days added.
AddDaysToCurrentLocalDateTimeExample.java
import java.time.LocalDateTime;
public class AddDaysToCurrentLocalDateTimeExample {
public static void main(String... args) {
int numberOfDays = 20;
// Add Number of Days to Current LocalDateTime
LocalDateTime currentLocalDateTime = LocalDateTime.now();
LocalDateTime newLocalDateTime = currentLocalDateTime.plusDays(numberOfDays);
System.out.println("Number of days: " + numberOfDays);
System.out.println("Current LocalDateTime: " + currentLocalDateTime);
System.out.println("New LocalDateTime: " + newLocalDateTime);
}
}
Number of days: 20
Current LocalDateTime: 2022-09-06T22:26:58.601242800
New LocalDateTime: 2022-09-26T22:26:58.601242800
Happy Coding 😊
Related Articles
Java Add Number of Days to Current Date
Java Add Number of Days to Current LocalDate
Java Add Number of Days to Current OffsetDateTime
Java Add Number of Days to Current ZonedDateTime