Java Reference ยท Date and Time
The LocalTime class
Reference for java.time.LocalTime: wall-clock times without a date, arithmetic that wraps around midnight, truncation, comparison and parsing.
java.time.LocalTime is a time of day without a date or a time zone, such as 09:30 or 23:59:59.999999999. It stores hour, minute, second and nanosecond, with a range from 00:00 to 23:59:59.999999999. Use it for opening hours, alarm times and recurring daily schedules.
A LocalTime is immutable and has no idea about days, so arithmetic wraps around midnight: 23:30 plus one hour is 00:30. If you need to know that the day changed, combine it with a date using LocalDateTime.
LocalTime open = LocalTime.of(9, 30); LocalTime close = open.plusHours(8);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public final class LocalTime implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable
| Member | Description |
|---|---|
static LocalTime now() / now(ZoneId) / now(Clock) |
Current time from the system clock or a given clock. |
of(int h, int m), of(h, m, s), of(h, m, s, nano) |
Create from fields. |
ofSecondOfDay(long), ofNanoOfDay(long) |
From the amount since midnight. |
parse(CharSequence) |
ISO text such as 10:15, 10:15:30, 10:15:30.123. |
getHour(), getMinute(), getSecond(), getNano() |
Field accessors. |
plusHours/Minutes/Seconds/Nanos(long), minus... |
Arithmetic that wraps within the day. |
withHour(int), withMinute(int), withSecond(int), withNano(int) |
Replace one field. |
truncatedTo(TemporalUnit) |
Zero all smaller units. |
isBefore, isAfter, compareTo, equals |
Comparison. |
atDate(LocalDate) |
Makes a LocalDateTime. |
toSecondOfDay(), toNanoOfDay() |
Amount since midnight. |
| Constants | MIN, MAX, MIDNIGHT, NOON. |
Throws: DateTimeException for out-of-range fields, DateTimeParseException for bad text.
Usage notes
- No date, no zone, no wrap detection. A pure
LocalTimecan't tell you that 23:30 plus 1 hour crossed into the next day. UseLocalDateTimewhen that matters. toString()is compact: it drops seconds when both seconds and nanos are zero, and prints only as many fractional digits as needed (3, 6 or 9).LocalTime.of(9, 0)prints09:00, whichparsereads back.Duration.betweenis signed and does not wrap. For overnight spans add a day when the result is negative, or use date-times.- Daylight saving does not exist here. On a day when clocks change,
LocalTimehappily holds a time (such as 02:30) that does not exist in some zones. - Hours are 0 to 23.
24:00is rejected. - Opening-hours checks are usually "at or after open and before close"; the end is exclusive so that adjacent intervals do not overlap.
- Precision: nanoseconds are stored, but
now()precision depends on the platform's clock, typically microseconds.
Related
LocalDate,LocalDateTime,Duration,DateTimeFormatter- Tutorial: Dates and Times