🎓 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 Instant minusSeconds() method in Java programming and how to use it with an example.
1. Instant minusSeconds() Method Overview
Definition:
The minusSeconds() method of the Instant class in Java is used to subtract the specified number of seconds from this Instant. This method returns a new Instant object representing the resultant time.
Syntax:
public Instant minusSeconds(long secondsToSubtract)
Parameters:
- secondsToSubtract: The number of seconds to subtract, may be negative.
Key Points:
- The minusSeconds() method can be used to modify the time represented by an Instant object by subtracting a certain number of seconds.
- If the number is negative, it effectively adds the corresponding seconds to the Instant.
- The method is non-static and should be called on an instance of the Instant class.
- The method does not modify the original Instant object but rather returns a new object representing the modified time.
2. Instant minusSeconds() Method Example
import java.time.Instant;
public class InstantMinusSecondsExample {
public static void main(String[] args) {
// Creating the current Instant
Instant instant = Instant.now();
System.out.println("Current Instant: " + instant);
// Subtracting 3600 seconds (1 hour) from the current Instant
Instant subtractedInstant = instant.minusSeconds(3600);
System.out.println("Instant after subtracting 3600 seconds: " + subtractedInstant);
// Adding 3600 seconds (1 hour) to the current Instant by subtracting negative seconds
Instant addedInstant = instant.minusSeconds(-3600);
System.out.println("Instant after adding 3600 seconds: " + addedInstant);
}
}
Output:
Current Instant: 2023-09-20T10:00:00Z Instant after subtracting 3600 seconds: 2023-09-20T09:00:00Z Instant after adding 3600 seconds: 2023-09-20T11:00:00Z
Explanation:
In this example, we created the current Instant and displayed it. We then used the minusSeconds() method to subtract 3600 seconds (1 hour) from the current Instant and displayed the resultant Instant. Similarly, we added 3600 seconds (1 hour) to the current Instant by passing a negative parameter to the minusSeconds() method and displayed the resultant Instant.
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