🎓 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 LocalTime minusMinutes() method in Java programming and how to use it with an example.
1. LocalTime minusMinutes() Method Overview
Definition:
The minusMinutes() method of the LocalTime class in Java is used to subtract the specified number of minutes from the LocalTime instance, resulting in a new LocalTime object representing the updated time.
Syntax:
public LocalTime minusMinutes(long minutesToSubtract)
Parameters:
- minutesToSubtract: the minutes to subtract, may be negative.
Key Points:
- The minusMinutes() method returns a new LocalTime instance with the specified minutes subtracted from the original time.
- The original LocalTime object remains unmodified, as LocalTime instances are immutable.
- If the subtraction results in a time before midnight, the time value wraps around.
- The method does not affect the hour, second, or nanosecond values of the LocalTime.
- If minutesToSubtract is negative, the method effectively adds the absolute value of the minutes to the current time.
2. LocalTime minusMinutes() Method Example
import java.time.LocalTime;
public class LocalTimeMinusMinutesExample {
public static void main(String[] args) {
// Get the current LocalTime
LocalTime currentTime = LocalTime.now();
System.out.println("Current Local Time: " + currentTime);
// Subtract 30 minutes from the current LocalTime
LocalTime timeAfterSubtractingMinutes = currentTime.minusMinutes(30);
System.out.println("Local Time After Subtracting 30 Minutes: " + timeAfterSubtractingMinutes);
// Add 15 minutes to the current LocalTime using negative parameter
LocalTime timeAfterAddingMinutes = currentTime.minusMinutes(-15);
System.out.println("Local Time After Adding 15 Minutes: " + timeAfterAddingMinutes);
}
}
Output:
Current Local Time: 10:15:30.123456789 (example time, actual output will vary) Local Time After Subtracting 30 Minutes: 09:45:30.123456789 Local Time After Adding 15 Minutes: 10:30:30.123456789
Explanation:
In this example, we first retrieve the current LocalTime and print it to the console.
We then use the minusMinutes() method to subtract 30 minutes from the current time, resulting in a new LocalTime instance which is printed to the console.
Subsequently, we demonstrate the addition of minutes by passing a negative value to minusMinutes(), adding 15 minutes to the current time, and printing the resulting LocalTime to the console.
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