LocalTime


LocalTime

Method Output Description
LocalTime.of(13, 12, 11) 13:12:11 Creates a LocalTime instance with the specified hour, minute, and second.
LocalTime.MIDNIGHT 00:00 Represents midnight (start of the day).
LocalTime.NOON 12:00 Represents noon (middle of the day).
LocalTime.now() Current time from system clock Obtains the current system time as a LocalTime.
LocalTime.MAX 23:59:59.999999999 The maximum supported local time.
LocalTime.MIN 00:00 The minimum supported local time.
LocalTime.ofSecondOfDay(84399) 23:59:59 Obtains a LocalTime from the second-of-day value.
LocalTime.ofNanoOfDay(2000000000) 00:00:02 Obtains a LocalTime from the nanos-of-day value.

Amount of time between two LocalTime

There are two equivalent ways to calculate the amount of time unit between two LocalTime: (1) through until(Temporal, TemporalUnit) method and through (2) TemporalUnit.between(Temporal, Temporal).

import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
 
public class CustomAmountOfTime {
    public static void main(String[] args) {
        LocalTime initialTime = LocalTime.of(3, 0, 0); // hour, minute, second
        LocalTime finalTime = LocalTime.of(5, 15, 40); // hour, minute, second
 
        long halfDays1 = initialTime.until(finalTime, ChronoUnit.HALF_DAYS); // 0
        long halfDays2 = ChronoUnit.HALF_DAYS.between(initialTime, finalTime); // 0
 
        long hours1 = initialTime.until(finalTime, ChronoUnit.HOURS); // 2
        long hours2 = ChronoUnit.HOURS.between(initialTime, finalTime); // 2
 
        long minutes1 = initialTime.until(finalTime, ChronoUnit.MINUTES); // 135
        long minutes2 = ChronoUnit.MINUTES.between(initialTime, finalTime); // 135
 
        long seconds1 = initialTime.until(finalTime, ChronoUnit.SECONDS); // 81240
        long seconds2 = ChronoUnit.SECONDS.between(initialTime, finalTime); // 81240
 
        long millisecs1 = initialTime.until(finalTime, ChronoUnit.MILLIS); // 81240000
        long millisecs2 = ChronoUnit.MILLIS.between(initialTime, finalTime); // 81240000
 
        long microsecs1 = initialTime.until(finalTime, ChronoUnit.MICROS); // 81240000000
        long microsecs2 = ChronoUnit.MICROS.between(initialTime, finalTime); // 81240000000
 
        long nanosecs1 = initialTime.until(finalTime, ChronoUnit.NANOS); // 81240000000000
        long nanosecs2 = ChronoUnit.NANOS.between(initialTime, finalTime); // 81240000000000
 
        // Using other ChronoUnit will throw an UnsupportedTemporalTypeException.
        // For instance:
        long days1 = initialTime.until(finalTime, ChronoUnit.DAYS);
        long days2 = ChronoUnit.DAYS.between(initialTime, finalTime);
    }
}

Intro

LocalTime is an immutable class and thread-safe, used to represent time, often viewed as hour-min-sec. Time is represented to nanosecond precision. For example, the value "13:45.30.123456789" can be stored in a LocalTime.

This class does not store or represent a date or time-zone. Instead, it is a description of the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone. This is a value based class, equals method should be used for comparisons.

Fields

MAX - The maximum supported LocalTime, '23:59:59.999999999'. MIDNIGHT, MIN, NOON

Important Static Methods

now(), now(Clock clock), now(ZoneId zone), parse(CharSequence text)

Important Instance Methods

isAfter(LocalTime other), isBefore(LocalTime other), minus(TemporalAmount amountToSubtract), minus(long amountToSubtract, TemporalUnit unit), plus(TemporalAmount amountToAdd), plus(long amountToAdd, TemporalUnit unit)

import java.time.ZoneId;
import java.time.LocalTime;
 
public class CustomZoneTime {
    public static void main(String[] args) {
        ZoneId customZone = ZoneId.of("America/New_York");
        LocalTime currentTime = LocalTime.now();
        LocalTime customZoneTime = LocalTime.now(customZone);
        LocalTime specificTime = LocalTime.parse("11:30:15");
 
        // Adjusted variable names and time zones for demonstration
    }
}
 

Difference in time can be calculated in any of following way

long timeDifference = Duration.between(customZoneTime, currentTime).toMinutes();
long timeDifference1 = java.time.temporal.ChronoUnit.MINUTES.between(then, customZoneTime);

You can also add/subtract hours, minutes or seconds from any object of LocalTime.

minusHours(long hoursToSubtract), minusMinutes(long hoursToMinutes), minusNanos(long nanosToSubtract), minusSeconds(long secondsToSubtract), plusHours(long hoursToSubtract), plusMinutes(long hoursToMinutes), plusNanos(long nanosToSubtract), plusSeconds(long secondsToSubtract)

now.plusHours(1L);
now1.minusMinutes(20L);

Time Modification

You can add hours, minutes, seconds and nanoseconds:

LocalTime currentTime = LocalTime.now();
LocalTime addedHours = currentTime.plusHours(3); // Add 3 hours
LocalTime addedMinutes = currentTime.plusMinutes(20); // Add 20 minutes
LocalTime addedSeconds = currentTime.plusSeconds(45); // Add 45 seconds
LocalTime addedNanoseconds = currentTime.plusNanos(250_000_000); // Add 250.000.000ns (250ms)

Time Zones and their time difference

import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
 
public class TimeZoneComparison {
    public static void main(String[] args) {
        ZoneId zoneA = ZoneId.of("Europe/Paris");
        ZoneId zoneB = ZoneId.of("America/New_York");
        ZoneId zoneC = ZoneId.of("Asia/Tokyo");
 
        LocalTime timeNow = LocalTime.now();
        LocalTime timeA = LocalTime.now(zoneA);
        LocalTime timeB = LocalTime.now(zoneB);
        LocalTime timeC = LocalTime.now(zoneC);
 
        System.out.println("Current Time : " + timeNow);
        System.out.println("Paris Time : " + timeA);
        System.out.println("New York Time : " + timeB);
        System.out.println("Tokyo Time : " + timeC);
 
        long minutesAB = ChronoUnit.MINUTES.between(timeA, timeB);
        long minutesAC = ChronoUnit.MINUTES.between(timeA, timeC);
        long minutesBC = ChronoUnit.MINUTES.between(timeB, timeC);
 
        System.out.println("Minutes Between Paris and New York : " + minutesAB + " mins");
        System.out.println("Minutes Between Paris and Tokyo : " + minutesAC + " mins");
        System.out.println("Minutes Between New York and Tokyo : " + minutesBC + " mins");
    }
}

Basic Programs