The LocalTime class, available in the java.time package, represents a time without date and timezone. Introduced in Java 8, it is part of the modern Date-Time API and is used to store and manipulate time values such as meeting schedules, alarms, business hours, and event timings.
- Represents only hour, minute, second, and nanosecond.
- Provides methods for formatting, parsing, and comparing time values.
- Immutable and thread-safe.
Declaration Of java.time.LocalTime Class
public final class LocalTime
extends Object
implements Temporal, TemporalAdjuster,
Comparable<LocalTime>, Serializable
Approach
- Import the java.time.LocalTime class.
- Create a LocalTime object using methods such as now(), of(), or parse().
- Perform operations like formatting, comparison, addition, or subtraction.
- Display the result using System.out.println().
Example: Program to illustrate LocalTime Class
// Importing LocalDateTime class from
// java.time package
import java.time.LocalTime;
// Main Class
public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an object of LocalTime class by
// declaring a variable to store LocalTime
LocalTime ltime = LocalTime.now();
// Print and display the LocalTime value
System.out.println("Local time value : " + ltime);
}
}
Output
Local time value : 06:20:52.986897
Explanation: The LocalTime.now() method returns the current system time. The program creates a LocalTime object and prints the current time, including hours, minutes, seconds, and nanoseconds.
Example: Program to illustrate LocalTime where plusMethod() is illustrated
// Importing LocalTime class from
// java.time package
import java.time.LocalTime;
// Main Class
public class GFG {
// main driver method
public static void main(String args[])
{
// Creating an object of LocalTime Class by
// declaring a variable to store LocalTime
LocalTime ltime = LocalTime.now();
// Now plusMinute() will add up minutes to the
// LocalTime
LocalTime time3 = ltime.plusMinutes(120);
// Display the time using object of class
System.out.println("Time : " + time3);
}
}
Output
Time : 08:34:59.135137
Explanation: The plusMinutes(120) method adds 120 minutes (2 hours) to the current time and returns a new LocalTime object. The original time remains unchanged because LocalTime is immutable.
Advantages of LocalTime
The LocalTime class provides a simple and reliable way to work with time without including date or time zone information.
- Provides methods for performing time-based calculations and comparisons.
- Creates new LocalTime objects instead of modifying existing instances, ensuring immutability.
- Supports different time formats using DateTimeFormatter.
- Useful for applications that require time handling without date or time zone information.
Commonly Used Methods of LocalTime
| Method | Description |
|---|---|
| now() | Returns the current time using the system clock and default time zone. |
| of(int hour, int minute) | Creates a LocalTime with the specified hour and minute. |
parse(CharSequence text) | Creates a LocalTime by parsing a time string. |
getHour() | Returns the hour of the day. |
getMinute() | Returns the minute of the hour. |
getSecond() | Returns the second of the minute. |
plusHours(long hours) | Returns a copy with the specified number of hours added. |
plusMinutes(long minutes) | Returns a copy with the specified number of minutes added. |
| minusHours(long hours) | Returns a copy with the specified number of hours subtracted. |
| minusMinutes(long minutes) | Returns a copy with the specified number of minutes subtracted. |
| isBefore(LocalTime other) | Checks whether this time is before another time. |
| isAfter(LocalTime other) | Checks whether this time is after another time. |
| format(DateTimeFormatter formatter) | Formats the time using the specified formatter. |