LocalTime class

The java.time.LocalTime class represents a time without a date and without time zone information, using hour, minute, second, and nanosecond.

Commonly Used Methods

Simple Program

import java.time.LocalTime;

public class SimpleLocalTimeExample {
    public static void main(String[] args) {
        LocalTime now = LocalTime.now();
        LocalTime lunchTime = LocalTime.of(13, 0);

        System.out.println("Current time: " + now);
        System.out.println("Lunch is scheduled at: " + lunchTime);
        System.out.println("Is it lunch time now? " + now.equals(lunchTime));
    }
}

Problem Statement

LotusJavaPrince and LotusPythonPrince are creating a conference room scheduler for the TechMind Event Center. Each room has defined opening and closing times. They need to:

  • Accept user input for desired start and end time of a booking.
  • Check if the requested time slot is within the room’s operational hours.
  • Deny or accept the booking request based on the time check.
import java.time.LocalTime;
import java.util.Scanner;

public class ConferenceRoomScheduler {
    public static void main(String[] args) {
        // Operational hours
        LocalTime openingTime = LocalTime.of(9, 0);
        LocalTime closingTime = LocalTime.of(18, 0);

        Scanner scanner = new Scanner(System.in);

        // Input start time
        System.out.print("Enter meeting start time (HH:mm): ");
        String startInput = scanner.nextLine();
        LocalTime startTime = LocalTime.parse(startInput);

        // Input end time
        System.out.print("Enter meeting end time (HH:mm): ");
        String endInput = scanner.nextLine();
        LocalTime endTime = LocalTime.parse(endInput);

        System.out.println("Room Available: " + openingTime + " to " + closingTime);
        System.out.println("Requested: " + startTime + " to " + endTime);

        // Check booking validity
        if (startTime.isBefore(openingTime) || endTime.isAfter(closingTime) || endTime.isBefore(startTime)) {
            System.out.println("Invalid booking. Please choose a time within operational hours.");
        } else {
            System.out.println("Booking confirmed.");
        }

        scanner.close();
    }
}
/*
Enter meeting start time (HH:mm): 10:30
Enter meeting end time (HH:mm): 12:00
Room Available: 09:00 to 18:00
Requested: 10:30 to 12:00
Booking confirmed.
*/

The LocalTime class is ideal for applications dealing with:

  • Daily schedules, such as work shifts, medication reminders, or appointments.
  • Time comparisons and operations, like checking if a store is open or calculating how long a session should last.
  • Use it alongside LocalDate for full date-time processing, or with Duration to compute time differences.

Its precision and immutability make it reliable in event scheduling systems, alarms, reminders, and banking hours.

Scroll to Top