The LocalTime
class in Java is designed to handle times without the complexities of dates and time zones. Whether you're scheduling events, setting alarms, or logging time-stamps, LocalTime
provides a simple and effective way to manage time in your applications.
Key Points
- Immutable and Thread-Safe:
LocalTime
objects are immutable, meaning once created, they cannot be changed. This makes them thread-safe and reliable in concurrent applications. - Easy to Create: You can easily create instances of
LocalTime
using various factory methods, such asnow()
,of()
, andparse()
. - Time Manipulation:
LocalTime
provides methods to add or subtract hours, minutes, seconds, and nanoseconds, allowing for easy time arithmetic. - Accessing Time Components: Methods are available to retrieve specific parts of the time, such as the hour, minute, second, and nanosecond.
- Comparison and Validation: You can compare times using methods like
isBefore()
,isAfter()
, andequals()
, and validate times to ensure they meet certain criteria. - Formatting and Parsing:
LocalTime
supports formatting to and parsing from strings, making it easy to convert betweenLocalTime
objects and string representations.
Java LocalTime Methods
The java.time.LocalTime
class represents a time without a time-zone in the ISO-8601 calendar system. Here are some of the commonly used methods:
Method | Description |
---|---|
compareTo() | Compares this time to another time. |
equals() | Checks if this time is equal to another time. |
from() | Obtains an instance of LocalTime from a temporal object. |
get() | Gets the value of the specified field from this time as an int . |
getHour() | Gets the hour-of-day field. |
getMinute() | Gets the minute-of-hour field. |
getNano() | Gets the nano-of-second field. |
getSecond() | Gets the second-of-minute field. |
isAfter() | Checks if this time is after the specified time. |
isBefore() | Checks if this time is before the specified time. |
minusHours() | Returns a copy of this time with the specified number of hours subtracted. |
minusMinutes() | Returns a copy of this time with the specified number of minutes subtracted. |
minusNanos() | Returns a copy of this time with the specified number of nanoseconds subtracted. |
minusSeconds() | Returns a copy of this time with the specified number of seconds subtracted. |
now() | Obtains the current time from the system clock in the default time-zone. |
of() | Obtains an instance of LocalTime from an hour, minute, second and nanosecond. |
ofInstant() | Obtains an instance of LocalTime from an Instant and time-zone. |
ofSecondOfDay() | Obtains an instance of LocalTime from the second-of-day value. |
plus() | Returns a copy of this time with the specified amount added. |
plusHours() | Returns a copy of this time with the specified number of hours added. |
plusSeconds() | Returns a copy of this time with the specified number of seconds added. |
withHour() | Returns a copy of this time with the hour-of-day altered. |
withMinute() | Returns a copy of this time with the minute-of-hour altered. |
withSecond() | Returns a copy of this time with the second-of-minute altered. |
In conclusion, the java.time.LocalTime
class provides various methods to work with times, allowing operations such as comparing, getting, manipulating, and formatting time fields. For more detailed information, refer to the official Java Documentation.