ZetCode

Java LocalTime Class

Last modified: April 16, 2025

The java.time.LocalTime class represents a time without a date or time-zone. It stores hours, minutes, seconds, and nanoseconds. LocalTime is immutable and thread-safe, making it ideal for time-only representations.

LocalTime is commonly used for storing business hours, event times, or any scenario where only the time portion matters. It provides precision up to nanoseconds and follows the 24-hour clock format. The class cannot represent time zones or dates.

LocalTime Class Overview

LocalTime provides methods to create, parse, and manipulate times. Key operations include getting time components, comparing times, and performing arithmetic. The class handles time in hours, minutes, seconds, and nanoseconds.

public final class LocalTime implements Temporal, TemporalAdjuster, 
    Comparable<LocalTime>, Serializable {
    public static LocalTime now();
    public static LocalTime of(int hour, int minute);
    public static LocalTime of(int hour, int minute, int second);
    public static LocalTime of(int hour, int minute, int second, int nanoOfSecond);
    public static LocalTime parse(CharSequence text);
    public int getHour();
    public int getMinute();
    public int getSecond();
    public int getNano();
    public boolean isAfter(LocalTime other);
    public boolean isBefore(LocalTime other);
    public LocalTime plusHours(long hours);
    public LocalTime plusMinutes(long minutes);
    public LocalTime minusHours(long hours);
    public LocalTime minusMinutes(long minutes);
}

The code above shows key methods provided by LocalTime. These methods allow creating, comparing, and manipulating times. The class provides precision up to nanoseconds while maintaining simple time-only representation.

Creating LocalTime Objects

LocalTime objects can be created in several ways. The most common methods are now for current time and factory methods for specific times. Parsing from strings is also supported.

Main.java
package com.zetcode; 

import java.time.LocalTime;

public class Main {

    public static void main(String[] args) {
        
        // Current time
        LocalTime now = LocalTime.now();
        System.out.println("Current time: " + now);
        
        // Specific time
        LocalTime lunchTime = LocalTime.of(12, 30);
        System.out.println("Lunch time: " + lunchTime);
        
        // With seconds
        LocalTime preciseTime = LocalTime.of(14, 15, 30);
        System.out.println("Precise time: " + preciseTime);
        
        // With nanoseconds
        LocalTime nanoTime = LocalTime.of(16, 45, 30, 123456789);
        System.out.println("Nanosecond time: " + nanoTime);
        
        // From string
        LocalTime parsed = LocalTime.parse("23:59:59");
        System.out.println("Parsed from string: " + parsed);
    }
}

This example demonstrates different ways to create LocalTime objects. The output shows times in ISO-8601 format (HH:mm:ss.nnn). The now method captures the current system time with available precision.

Getting Time Components

A LocalTime can be decomposed into its hour, minute, second, and nanosecond components. These methods allow accessing individual parts of the time for display or calculations.

Main.java
package com.zetcode; 

import java.time.LocalTime;

public class Main {

    public static void main(String[] args) {

        LocalTime time = LocalTime.of(15, 45, 30, 123456789);
        
        // Get components
        int hour = time.getHour();
        int minute = time.getMinute();
        int second = time.getSecond();
        int nano = time.getNano();
        
        System.out.println("Hour: " + hour);
        System.out.println("Minute: " + minute);
        System.out.println("Second: " + second);
        System.out.println("Nanosecond: " + nano);
        
        // Convert to seconds of day
        int secondsOfDay = time.toSecondOfDay();
        System.out.println("Seconds since midnight: " + secondsOfDay);
    }
}

This example shows how to extract components from a LocalTime. The toSecondOfDay method converts the time to seconds since midnight. This is useful for time-based calculations and comparisons.

Comparing Times

LocalTime objects can be compared to determine chronological order. The class provides isBefore, isAfter, and compareTo methods. These comparisons are essential for time-based logic.

Main.java
package com.zetcode; 

import java.time.LocalTime;

public class Main {

    public static void main(String[] args) {

        LocalTime morning = LocalTime.of(9, 0);
        LocalTime afternoon = LocalTime.of(14, 30);
        LocalTime evening = LocalTime.of(20, 0);
        
        System.out.println("Morning before afternoon: " + morning.isBefore(afternoon));
        System.out.println("Evening after afternoon: " + evening.isAfter(afternoon));
        System.out.println("Comparison result: " + morning.compareTo(evening));
        
        // Equality check
        LocalTime sameAsMorning = LocalTime.of(9, 0);
        System.out.println("Morning equals same time: " + morning.equals(sameAsMorning));
    }
}

This example demonstrates various ways to compare LocalTime objects. The comparison methods consider all time components. Note that equality requires all components (including nanoseconds) to match exactly.

Adding and Subtracting Time

LocalTime supports temporal arithmetic through plus and minus methods. These operations are useful for calculating future or past times. The class handles overflow automatically (24-hour wrap-around).

Main.java
package com.zetcode; 

import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class Main {

    public static void main(String[] args) {

        LocalTime now = LocalTime.of(10, 30);
        
        // Add hours
        LocalTime later = now.plusHours(2);
        System.out.println("Two hours later: " + later);
        
        // Subtract minutes
        LocalTime earlier = now.minusMinutes(45);
        System.out.println("Forty-five minutes earlier: " + earlier);
        
        // Add using ChronoUnit
        LocalTime nextHour = now.plus(1, ChronoUnit.HOURS);
        System.out.println("Next hour: " + nextHour);
        
        // Wrap-around midnight
        LocalTime lateNight = LocalTime.of(23, 0).plusHours(2);
        System.out.println("After midnight: " + lateNight);
    }
}

This example shows various ways to perform temporal arithmetic with LocalTime. Operations can use specific unit methods or ChronoUnit constants. All calculations handle midnight wrap-around automatically.

Formatting and Parsing

LocalTime can be formatted and parsed using DateTimeFormatter. This allows custom display formats and flexible input parsing. The ISO-8601 format is used by default.

Main.java
package com.zetcode; 

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {

    public static void main(String[] args) {

        LocalTime time = LocalTime.of(16, 45, 30);
        
        // Default format
        String defaultFormat = time.toString();
        System.out.println("Default format: " + defaultFormat);
        
        // Custom formatting
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");
        String customFormat = time.format(formatter);
        System.out.println("Custom format: " + customFormat);
        
        // Parsing custom format
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("H.mm");
        LocalTime parsedTime = LocalTime.parse("14.30", parser);
        System.out.println("Parsed time: " + parsedTime);
    }
}

This example demonstrates formatting and parsing LocalTime objects. The DateTimeFormatter provides flexible pattern-based formatting. Parsing supports both standard and custom formats.

Time Range Operations

LocalTime provides methods to check if a time falls within certain ranges or meets specific conditions. These operations are useful for time-based business rules and validations.

Main.java
package com.zetcode; 

import java.time.LocalTime;

public class Main {

    public static void main(String[] args) {

        LocalTime businessStart = LocalTime.of(9, 0);
        LocalTime businessEnd = LocalTime.of(17, 0);
        LocalTime currentTime = LocalTime.now();
        
        // Check if within business hours
        boolean isBusinessHours = !currentTime.isBefore(businessStart) 
                               &&  currentTime.isBefore(businessEnd);
        System.out.println("Is business hours: " + isBusinessHours);
        
        // Check if midnight
        boolean isMidnight = currentTime.equals(LocalTime.MIDNIGHT);
        System.out.println("Is midnight: " + isMidnight);
        
        // Check if AM or PM
        boolean isAM = currentTime.getHour() < 12;
        System.out.println("Is AM: " + isAM);
        
        // Get min/max of two times
        LocalTime earliest = LocalTime.min(businessStart, currentTime);
        System.out.println("Earliest time: " + earliest);
    }
}

This example shows various range and condition checks with LocalTime. The isBefore and isAfter methods are particularly useful for range checks. Constants like MIDNIGHT provide common reference points.

Source

Java LocalTime Class Documentation

In this article, we've covered the essential methods and features of the Java LocalTime class. Understanding these concepts is crucial for accurate time-only representations in Java applications.

Author

My name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts.

List all Java tutorials.