🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
In this guide, you will learn about the LocalDateTime minusHours() method in Java programming and how to use it with an example.
1. LocalDateTime minusHours() Method Overview
Definition:
The LocalDateTime.minusHours() method returns a copy of the LocalDateTime with the specified number of hours subtracted.
Syntax:
LocalDateTime minusHours(long hours)
Parameters:
- hours: The number of hours to subtract, may be negative.
Key Points:
- The LocalDateTime.minusHours() method is useful for date and time arithmetic where you want to manipulate the hour part of a LocalDateTime.
- The returned LocalDateTime instance is immutable, which means the original LocalDateTime instance remains unchanged.
- If subtracting the hours results in a date-time before the minimum date (LocalDateTime.MIN), a DateTimeException will be thrown.
- Subtracting a negative value is the same as adding the absolute value of that number of hours.
2. LocalDateTime minusHours() Method Example
import java.time.LocalDateTime;
public class LocalDateTimeMinusHoursExample {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.of(2023, 1, 1, 0, 0);
// Subtract 1 hour from the LocalDateTime
LocalDateTime result1 = dateTime.minusHours(1);
// Add 3 hours by subtracting a negative value
LocalDateTime result2 = dateTime.minusHours(-3);
System.out.println("Original DateTime: " + dateTime);
System.out.println("After subtracting 1 hour: " + result1);
System.out.println("After adding 3 hours: " + result2);
}
}
Output:
Original DateTime: 2023-01-01T00:00 After subtracting 1 hour: 2022-12-31T23:00 After adding 3 hours: 2023-01-01T03:00
Explanation:
In the example, we have a LocalDateTime set to January 1, 2023, at midnight (00:00).
- When we subtract 1 hour using minusHours(1), the time becomes 23:00 on December 31, 2022, demonstrating the method's capability to handle day, month, and year boundaries.
- When we add 3 hours using minusHours(-3), the time becomes 03:00 on January 1, 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