🎓 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
The minusMinutes() method in Java, part of the java.time.ZonedDateTime class, returns a copy of this ZonedDateTime with the specified number of minutes subtracted. This method is useful for performing date-time arithmetic, such as calculating a date-time a certain number of minutes in the past.
Table of Contents
- Introduction
minusMinutes()Method Syntax- Understanding
minusMinutes() - Examples
- Basic Usage
- Using
minusMinutes()in Conditional Statements
- Real-World Use Case
- Conclusion
Introduction
The minusMinutes() method allows you to subtract a specified number of minutes from a ZonedDateTime instance, resulting in a new ZonedDateTime object. This is particularly useful for date-time calculations and scheduling tasks.
minusMinutes() Method Syntax
The syntax for the minusMinutes() method is as follows:
public ZonedDateTime minusMinutes(long minutes) Parameters:
minutes: The number of minutes to subtract, may be negative.
Returns:
- A
ZonedDateTimebased on this date-time with the specified number of minutes subtracted, not null.
Throws:
DateTimeExceptionif the result exceeds the supported date range.
Understanding minusMinutes()
The minusMinutes() method subtracts the specified number of minutes from the current ZonedDateTime instance and returns a new ZonedDateTime object with the updated date-time. This method does not modify the original instance, as ZonedDateTime is immutable.
Examples
Basic Usage
To demonstrate the basic usage of minusMinutes(), we will subtract a specified number of minutes from a ZonedDateTime instance.
Example
import java.time.ZonedDateTime; import java.time.ZoneId; public class ZonedDateTimeMinusMinutesExample { public static void main(String[] args) { ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 15, 10, 30, 45, 0, ZoneId.of("America/New_York")); ZonedDateTime newZonedDateTime = zonedDateTime.minusMinutes(45); System.out.println("Original ZonedDateTime: " + zonedDateTime); System.out.println("New ZonedDateTime after subtracting 45 minutes: " + newZonedDateTime); } } Output:
Original ZonedDateTime: 2023-06-15T10:30:45-04:00[America/New_York] New ZonedDateTime after subtracting 45 minutes: 2023-06-15T09:45:45-04:00[America/New_York] Using minusMinutes() in Conditional Statements
This example shows how to use the minusMinutes() method in conditional statements to perform actions based on the new date-time.
Example
import java.time.ZonedDateTime; import java.time.ZoneId; public class ZonedDateTimeConditionalExample { public static void main(String[] args) { ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC")); ZonedDateTime pastDateTime = now.minusMinutes(90); if (pastDateTime.getHour() == now.getHour() - 1) { System.out.println("The date-time 90 minutes ago was in the previous hour."); } else { System.out.println("The date-time 90 minutes ago was not in the previous hour."); } } } Output:
The date-time 90 minutes ago was not in the previous hour. Real-World Use Case
Adjusting Time Based on Past Minutes
In real-world applications, the minusMinutes() method can be used to adjust times based on minutes in the past, such as calculating deadlines or scheduling events.
Example
import java.time.ZonedDateTime; import java.time.ZoneId; public class ReminderScheduler { public static void main(String[] args) { ZonedDateTime currentDateTime = ZonedDateTime.now(ZoneId.of("America/Los_Angeles")); ZonedDateTime reminderDateTime = currentDateTime.minusMinutes(30); // 30 minutes before now System.out.println("Current Date and Time: " + currentDateTime); System.out.println("Reminder Date and Time: " + reminderDateTime); } } Output:
Current Date and Time: 2024-07-06T22:14:09.090848300-07:00[America/Los_Angeles] Reminder Date and Time: 2024-07-06T21:44:09.090848300-07:00[America/Los_Angeles] Conclusion
The ZonedDateTime.minusMinutes() method is used to subtract a specified number of minutes from a ZonedDateTime instance. This method is particularly useful for date-time arithmetic and scheduling tasks. By understanding and using the minusMinutes() method, you can effectively manage and manipulate date-time data in your Java applications.
Comments
Post a Comment
Leave Comment