Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

LocalTime

A time of day without a date — for example, a shop's opening time.

Available since: Java 8

Syntax

public final class LocalTime implements Temporal, Comparable<LocalTime>

Examples

A time, wrapping past midnight, and comparison:

void main() {
    LocalTime opens = LocalTime.of(9, 0);
    LocalTime closes = LocalTime.parse("21:30");
    IO.println(opens);
    IO.println(closes.plusHours(3));
 
    LocalTime now = LocalTime.of(22, 5);
    IO.println(now.isAfter(opens) && now.isBefore(closes));
}

Output:

09:00
00:30
false

Notes

Adding time wraps around: 21:30 plus 3 hours is 00:30, with no date change (LocalTime has no date). Seconds are not printed when they are zero. The current time is LocalTime.now().

See also

  • LocalDateTime — A date and time without a time zone — for example, a meeting in a calendar.
  • Duration — An amount of time in seconds and nanoseconds — hours, minutes, seconds.
  • DateTimeFormatter.ofPattern() — Creates a formatter for printing and parsing dates with a pattern such as dd.MM.yyyy.

In the tutorial

  • Dates and times — The java.time package — LocalDate, LocalTime, LocalDateTime, date arithmetic, Period, Duration, ChronoUnit, time zones and Instant.

Thanks for reading!

Donate to the site — we work hard for you.