Java Add Number of Days to Current OffsetDateTime
Tags: Java OffsetDateTime
In this Java tutorial, we learn how to add number of days to current OffsetDateTime value in Java programming language.
How to add number of days to current OffsetDateTime in Java
In Java, we can add number of days to current OffsetDateTime value with the following steps:
- Instantiate current OffsetDateTime value using the OffsetDateTime.now() static method.
- Using the OffsetDateTime.plusDays(long days) method to create a new OffsetDateTime value with number of days added.
AddDaysToCurrentOffsetDateTimeExample.java
import java.time.OffsetDateTime;
public class AddDaysToCurrentOffsetDateTimeExample {
public static void main(String... args) {
int numberOfDays = 15;
// Add Number of Days to Current OffsetDateTime
OffsetDateTime currentOffsetDateTime = OffsetDateTime.now();
OffsetDateTime newOffsetDateTime = currentOffsetDateTime.plusDays(numberOfDays);
System.out.println("Number of days: " + numberOfDays);
System.out.println("Current OffsetDateTime: " + currentOffsetDateTime);
System.out.println("New OffsetDateTime: " + newOffsetDateTime);
}
}
Number of days: 15
Current OffsetDateTime: 2022-09-06T22:31:55.219631200+07:00
New OffsetDateTime: 2022-09-21T22:31:55.219631200+07:00
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 LocalDateTime
Java Add Number of Days to Current ZonedDateTime