🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the LocalDateTime plusDays() method in Java programming and how to use it with an example.
1. LocalDateTime plusDays() Method Overview
Definition:
The LocalDateTime.plusDays() method returns a copy of the LocalDateTime with the specified number of days added.
Syntax:
LocalDateTime plusDays(long days)
Parameters:
- days: The number of days to add, may be negative.
Key Points:
- The LocalDateTime.plusDays() method is useful for date arithmetic where you want to manipulate the day part of a LocalDateTime.
- The returned LocalDateTime instance is immutable, which means the original LocalDateTime instance remains unchanged.
- The method takes care of month and year boundaries. For example, adding 1 day to the last day of a month will return the first day of the next month.
- If adding the days results in an overflow of the maximum date (LocalDateTime.MAX), a DateTimeException will be thrown.
2. LocalDateTime plusDays() Method Example
import java.time.LocalDateTime;
public class LocalDateTimePlusDaysExample {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.of(2023, 12, 31, 16, 30);
// Add 1 day to the LocalDateTime
LocalDateTime result1 = dateTime.plusDays(1);
// Subtract 5 days from the LocalDateTime
LocalDateTime result2 = dateTime.plusDays(-5);
System.out.println("Original DateTime: " + dateTime);
System.out.println("After adding 1 day: " + result1);
System.out.println("After subtracting 5 days: " + result2);
}
}
Output:
Original DateTime: 2023-12-31T16:30 After adding 1 day: 2024-01-01T16:30 After subtracting 5 days: 2023-12-26T16:30
Explanation:
In the example, we have a LocalDateTime set to December 31, 2023.
- When we add 1 day using plusDays(1), the date becomes January 1, 2024, which demonstrates the method's capability to handle month and year boundaries.
- When we subtract 5 days using plusDays(-5), the date becomes December 26, 2023.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment