🎓 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 withMinute() method in Java, part of the java.time.LocalTime class, is used to create a copy of the current LocalTime instance with the specified minute-of-hour value. This method is useful when you need to adjust the minute component of a LocalTime instance while keeping the other components (hour, second, and nanosecond) unchanged.
Table of Contents
- Introduction
withMinute()Method Syntax- Understanding
withMinute() - Examples
- Basic Usage
- Using
withMinute()in Conditional Statements
- Real-World Use Case
- Conclusion
Introduction
The withMinute() method allows you to create a new LocalTime instance with a specified minute-of-hour value. This is particularly useful when you need to adjust the minute component of a time while keeping the hour, second, and nanosecond components unchanged.
withMinute() Method Syntax
The syntax for the withMinute() method is as follows:
public LocalTime withMinute(int minute)
Parameters:
minute: The minute-of-hour to set in the resultingLocalTime, from 0 to 59.
Returns:
- A
LocalTimebased on the current time with the requested minute, not null.
Throws:
DateTimeExceptionif the minute value is invalid.
Understanding withMinute()
The withMinute() method returns a copy of the current LocalTime instance with the specified minute value. The hour, second, and nanosecond values remain unchanged. This method is immutable and does not modify the original LocalTime instance.
Examples
Basic Usage
To demonstrate the basic usage of withMinute(), we will create a new LocalTime instance with a specified minute value.
Example
import java.time.LocalTime;
public class LocalTimeWithMinuteExample {
public static void main(String[] args) {
LocalTime time = LocalTime.of(10, 30, 45); // 10:30:45 AM
LocalTime newTime = time.withMinute(15); // Set minute to 15
System.out.println("Original Time: " + time);
System.out.println("New Time: " + newTime);
}
}
Output:
Original Time: 10:30:45
New Time: 10:15:45
Using withMinute() in Conditional Statements
This example shows how to use the withMinute() method in conditional statements to perform actions based on the adjusted time.
Example
import java.time.LocalTime;
public class LocalTimeConditionalExample {
public static void main(String[] args) {
LocalTime currentTime = LocalTime.now();
LocalTime adjustedTime = currentTime.withMinute(0); // Set minute to 0
if (adjustedTime.isBefore(currentTime)) {
System.out.println("The adjusted time is before the current time.");
} else {
System.out.println("The adjusted time is after or equal to the current time.");
}
}
}
Output:
The adjusted time is before the current time.
Real-World Use Case
Setting Specific Minutes for Events
In real-world applications, the withMinute() method can be used to set specific minutes for events or tasks while keeping the other components of the time unchanged.
Example
import java.time.LocalTime;
public class EventSchedulerExample {
public static void main(String[] args) {
LocalTime eventTime = LocalTime.of(14, 30); // 2:30 PM
LocalTime newEventTime = eventTime.withMinute(45); // Change event time to 2:45 PM
System.out.println("Original Event Time: " + eventTime);
System.out.println("New Event Time: " + newEventTime);
}
}
Output:
Original Event Time: 14:30
New Event Time: 14:45
Conclusion
The LocalTime.withMinute() method is used to create a copy of the current LocalTime instance with a specified minute value. This method is particularly useful for adjusting the minute component of a time while keeping the other components unchanged. By understanding and using the withMinute() method, you can effectively manage and manipulate time-based data in your Java applications.
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