How to Use LocalTime in Java: Syntax and Examples
Use
LocalTime from java.time package to represent time without date in Java. You can create instances using methods like LocalTime.now() or LocalTime.of(), and manipulate time with built-in methods.Syntax
LocalTime is a class in the java.time package that represents a time without a date or timezone.
Common ways to create a LocalTime object:
LocalTime.now()- gets the current time.LocalTime.of(hour, minute)- creates a time with specified hour and minute.LocalTime.of(hour, minute, second)- creates a time with hour, minute, and second.
You can also parse a string to get a LocalTime using LocalTime.parse().
java
import java.time.LocalTime; public class LocalTimeSyntax { public static void main(String[] args) { LocalTime currentTime = LocalTime.now(); LocalTime specificTime = LocalTime.of(14, 30, 15); LocalTime parsedTime = LocalTime.parse("09:45:00"); System.out.println("Current Time: " + currentTime); System.out.println("Specific Time: " + specificTime); System.out.println("Parsed Time: " + parsedTime); } }
Example
This example shows how to create, manipulate, and display LocalTime objects. It demonstrates getting the current time, creating a specific time, adding hours and minutes, and formatting the output.
java
import java.time.LocalTime; public class LocalTimeExample { public static void main(String[] args) { // Get current time LocalTime now = LocalTime.now(); // Create a specific time: 10:15:30 LocalTime time = LocalTime.of(10, 15, 30); // Add 2 hours and 45 minutes LocalTime newTime = time.plusHours(2).plusMinutes(45); // Display times System.out.println("Current time: " + now); System.out.println("Original time: " + time); System.out.println("New time after adding 2h 45m: " + newTime); } }
Output
Current time: 14:23:45.123456789
Original time: 10:15:30
New time after adding 2h 45m: 13:00:30
Common Pitfalls
Common mistakes when using LocalTime include:
- Confusing
LocalTimewithLocalDateTimeorZonedDateTime.LocalTimeonly stores time without date or timezone. - Parsing strings with wrong format causes exceptions. The default format is
HH:mm:ss. - Using 24 for hour in
LocalTime.of()causes error; valid hour range is 0-23.
Example of wrong and right usage:
java
import java.time.LocalTime; public class LocalTimePitfalls { public static void main(String[] args) { // Wrong: hour 24 is invalid // LocalTime invalidTime = LocalTime.of(24, 0); // Throws DateTimeException // Right: use 0 for midnight LocalTime midnight = LocalTime.of(0, 0); System.out.println("Midnight time: " + midnight); // Wrong: parsing wrong format // LocalTime wrongParse = LocalTime.parse("10-15-30"); // Throws DateTimeParseException // Right: parse correct format LocalTime correctParse = LocalTime.parse("10:15:30"); System.out.println("Parsed time: " + correctParse); } }
Output
Midnight time: 00:00
Parsed time: 10:15:30
Quick Reference
Here is a quick summary of useful LocalTime methods:
| Method | Description |
|---|---|
| now() | Gets the current time from the system clock. |
| of(int hour, int minute) | Creates a time with specified hour and minute. |
| of(int hour, int minute, int second) | Creates a time with hour, minute, and second. |
| parse(CharSequence text) | Parses a string like "HH:mm:ss" to LocalTime. |
| plusHours(long hours) | Adds hours to the time. |
| plusMinutes(long minutes) | Adds minutes to the time. |
| minusHours(long hours) | Subtracts hours from the time. |
| minusMinutes(long minutes) | Subtracts minutes from the time. |
| toString() | Returns the time as a string in "HH:mm:ss" format. |
Key Takeaways
Use LocalTime to represent time without date or timezone in Java.
Create LocalTime instances with now(), of(), or parse() methods.
Be careful with hour values (0-23) and string formats when parsing.
Use plusHours(), plusMinutes(), minusHours(), and minusMinutes() to adjust time.
LocalTime.toString() outputs time in HH:mm:ss format by default.