🎓 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 of() method in Java, part of the java.time.LocalTime class, is used to obtain an instance of LocalTime from specified hour, minute, second, and nanosecond parameters. This method provides several overloaded versions to offer flexibility in creating LocalTime instances based on different levels of precision.
Table of Contents
- Introduction
of()Method Syntax- Overloaded
of()Methods - Understanding
of() - Examples
- Basic Usage with Hour and Minute
- Using
of()with Hour, Minute, and Second - Using
of()with Hour, Minute, Second, and Nanosecond
- Real-World Use Case
- Conclusion
Introduction
The of() method allows you to create a LocalTime instance with specified hour, minute, second, and nanosecond values. This is particularly useful when you need to create specific times programmatically.
of() Method Syntax
The LocalTime class provides several overloaded of() methods to create instances of LocalTime:
- Using hour and minute:
public static LocalTime of(int hour, int minute)
- Using hour, minute, and second:
public static LocalTime of(int hour, int minute, int second)
- Using hour, minute, second, and nanosecond:
public static LocalTime of(int hour, int minute, int second, int nanoOfSecond)
Parameters:
hour: The hour-of-day to represent, from 0 to 23.minute: The minute-of-hour to represent, from 0 to 59.second: The second-of-minute to represent, from 0 to 59.nanoOfSecond: The nano-of-second to represent, from 0 to 999,999,999.
Returns:
- A
LocalTimerepresenting the specified time.
Throws:
DateTimeExceptionif the value of any field is out of range.
Overloaded of() Methods
1. of(int hour, int minute)
This method creates an instance of LocalTime using the specified hour and minute.
Example
import java.time.LocalTime;
public class LocalTimeOfExample {
public static void main(String[] args) {
LocalTime time = LocalTime.of(14, 30); // 2:30 PM
System.out.println("Time: " + time);
}
}
Output:
Time: 14:30
2. of(int hour, int minute, int second)
This method creates an instance of LocalTime using the specified hour, minute, and second.
Example
import java.time.LocalTime;
public class LocalTimeOfWithSecondExample {
public static void main(String[] args) {
LocalTime time = LocalTime.of(14, 30, 45); // 2:30:45 PM
System.out.println("Time: " + time);
}
}
Output:
Time: 14:30:45
3. of(int hour, int minute, int second, int nanoOfSecond)
This method creates an instance of LocalTime using the specified hour, minute, second, and nanosecond.
Example
import java.time.LocalTime;
public class LocalTimeOfWithNanoExample {
public static void main(String[] args) {
LocalTime time = LocalTime.of(14, 30, 45, 123456789); // 2:30:45.123456789 PM
System.out.println("Time: " + time);
}
}
Output:
Time: 14:30:45.123456789
Understanding of()
The of() method creates a LocalTime instance by specifying the hour, minute, second, and optionally the nanosecond. The method ensures that the values are valid and within the correct range for the time specified.
Examples
Basic Usage with Hour and Minute
To demonstrate the basic usage of of(int hour, int minute), we will create a LocalTime instance by specifying the hour and minute.
Example
import java.time.LocalTime;
public class LocalTimeOfExample {
public static void main(String[] args) {
LocalTime time = LocalTime.of(9, 15); // 9:15 AM
System.out.println("Time: " + time);
}
}
Output:
Time: 09:15
Using of() with Hour, Minute, and Second
This example shows how to use the of(int hour, int minute, int second) method to create a LocalTime instance.
Example
import java.time.LocalTime;
public class LocalTimeOfWithSecondExample {
public static void main(String[] args) {
LocalTime time = LocalTime.of(22, 45, 30); // 10:45:30 PM
System.out.println("Time: " + time);
}
}
Output:
Time: 22:45:30
Using of() with Hour, Minute, Second, and Nanosecond
This example shows how to use the of(int hour, int minute, int second, int nanoOfSecond) method to create a LocalTime instance.
Example
import java.time.LocalTime;
public class LocalTimeOfWithNanoExample {
public static void main(String[] args) {
LocalTime time = LocalTime.of(7, 30, 15, 987654321); // 7:30:15.987654321 AM
System.out.println("Time: " + time);
}
}
Output:
Time: 07:30:15.987654321
Real-World Use Case
Scheduling Specific Times for Events
In real-world applications, the of() method can be used to schedule specific times for events, such as setting start and end times for meetings or appointments.
Example
import java.time.LocalTime;
public class EventSchedulingExample {
public static void main(String[] args) {
LocalTime meetingStartTime = LocalTime.of(9, 0); // 9:00 AM
LocalTime meetingEndTime = LocalTime.of(10, 30); // 10:30 AM
System.out.println("Meeting Start Time: " + meetingStartTime);
System.out.println("Meeting End Time: " + meetingEndTime);
}
}
Output:
Meeting Start Time: 09:00
Meeting End Time: 10:30
Conclusion
The LocalTime.of() method is used to create an instance of LocalTime with specified hour, minute, second, and nanosecond values. The method offers flexibility by allowing the specification of various levels of precision. By understanding and using the overloaded of() methods, you can effectively create and manage 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