Sub topic 4
Times of Day with LocalTime
Some values only need a clock time, with no date attached. A shop opens at 9:30. An alarm rings at 7:00. A daily report runs at 18:00. For values like these, Java gives you LocalTime.
What LocalTime Is
LocalTime is a class that holds a time of day: hours, minutes, seconds, and nanoseconds. A nanosecond is one billionth of a second. A LocalTime has no date and no time zone. It means "9:30 on the clock" and nothing more.
Like the other java.time types, LocalTime is immutable. An immutable object never changes after you create it. Methods such as plusHours give you back a new object, so you must save the result in a variable.
Creating and Reading Times
You can build a time from numbers with LocalTime.of, or from text with LocalTime.parse. To parse means to read text and turn it into an object. The text must use the 24-hour HH:mm or HH:mm:ss form.
import java.time.LocalTime;
public class Times {
public static void main(String[] args) {
LocalTime opening = LocalTime.of(9, 30);
LocalTime closing = LocalTime.parse("17:45");
LocalTime exact = LocalTime.of(9, 30, 15);
System.out.println(opening); // 09:30
System.out.println(exact); // 09:30:15
System.out.println(closing.getHour()); // 17
System.out.println(closing.getMinute()); // 45
LocalTime lunch = opening.plusHours(3).minusMinutes(15);
System.out.println(lunch); // 12:15
System.out.println(lunch.isBefore(closing)); // true
LocalTime late = LocalTime.of(23, 30).plusHours(1);
System.out.println(late); // 00:30
}
}When printed, a time shows its seconds only if they are not zero. That is why opening prints as 09:30 and exact prints as 09:30:15.
Useful Methods
| Method | What it does | Example result |
|---|---|---|
| `getHour()` | Reads the hour (0–23) | `17` |
| `getMinute()` | Reads the minute (0–59) | `45` |
| `plusMinutes(n)` | Returns a time n minutes later | `09:45` from `09:30` |
| `minusHours(n)` | Returns a time n hours earlier | `08:30` from `09:30` |
| `isBefore(other)` | Checks if this time comes first | `true` or `false` |
| `withHour(h)` | Returns a copy with a new hour | `14:30` from `09:30` |
Java also gives you ready-made constants: LocalTime.MIDNIGHT (00:00), LocalTime.NOON (12:00), LocalTime.MIN, and LocalTime.MAX (23:59:59.999999999).
Times Wrap Around Midnight
A LocalTime has no date, so it cannot move to "tomorrow." If you add time past midnight, the clock wraps around and starts again from 00:00, the way a wall clock does.
When to Use LocalTime
Use LocalTime for times that repeat every day and do not depend on a place: opening hours, alarm times, or a class schedule. If you need a specific moment, such as when an order was placed, use a type that includes a date and possibly a time zone.
Exercise
What does this line print? System.out.println(LocalTime.of(22, 0).plusMinutes(150));
Show answer
It prints 00:30. 150 minutes is 2 hours and 30 minutes. 22:00 plus 2 hours is 00:00, and 30 more minutes gives 00:30. The time wraps past midnight because LocalTime has no date.
Exercise
A student writes LocalTime t = LocalTime.of(8, 0); t.plusHours(1); System.out.println(t); and expects 09:00. What prints, and how do you fix it?
Show answer
It prints 08:00, because LocalTime is immutable and the result of plusHours was thrown away. Fix it by saving the result: t = t.plusHours(1);.