Java LocalTime Tutorial with Examples
LocalTime class represent a time without a time-zone in the ISO-8601 calendar system, such as 15:10:40, often viewed as often viewed as hour-minute-second. Time is represented to nanosecond precision. This class is immutable and thread-safe.
Creating a LocalTime
We can create a LocalTime in several ways:
- static LocalTime now(): Obtains the current time from the system clock in the default time-zone.
- static LocalTime now(Clock clock): Obtains the current time from the specified clock.
- static LocalTime now(ZoneId zone): Obtains the current time from the system clock in the specified time-zone.
- static LocalTime of(int hour, int minute): Obtains an instance of LocalTime from an hour and minute.
- static LocalTime of(int hour, int minute, int second): Obtains an instance of LocalTime from an hour, minute and second.
- static LocalTime of(int hour, int minute, int second, int nanoOfSecond): Obtains an instance of LocalTime from an hour, minute, second and nanosecond.
- static LocalTime ofNanoOfDay(long nanoOfDay): Obtains an instance of LocalTime from a nanos-of-day value.
- static LocalTime ofSecondOfDay(long secondOfDay): Obtains an instance of LocalTime from a second-of-day value.
- static LocalTime parse(CharSequence text): Obtains an instance of LocalTime from a text string such as 20:45:30.
- static LocalTime parse(CharSequence text, DateTimeFormatter formatter): Obtains an instance of LocalTime from a text string using a specific formatter.
Added since Java 9:
- static LocalTime ofInstant(Instant instant, ZoneId zone): Obtains an instance of LocalTime from an Instant and zone ID.
import java.time.Clock;
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class LocalTimeInitExample {
public static void main(String[] args) {
LocalTime localTime1 = LocalTime.now();
System.out.println("LocalTime1 : " + localTime1);
LocalTime localTime2 = LocalTime.now(Clock.systemUTC());
System.out.println("LocalTime2 : " + localTime2);
LocalTime localTime3 = LocalTime.now(ZoneId.systemDefault());
System.out.println("LocalTime3 : " + localTime3);
LocalTime localTime4 = LocalTime.of(3, 5, 15);
System.out.println("LocalTime4 : " + localTime4);
LocalTime localTime5 = LocalTime.of(15, 20, 45, 123456789);
System.out.println("LocalTime5 : " + localTime5);
LocalTime localTime6 = LocalTime.ofNanoOfDay(86399123456789L);
System.out.println("LocalTime6 : " + localTime6);
LocalTime localTime7 = LocalTime.ofSecondOfDay(86399);
System.out.println("LocalTime7 : " + localTime7);
LocalTime localTime8 = LocalTime.parse("20:45:30.956702500");
System.out.println("LocalTime8 : " + localTime8);
LocalTime localTime9 = LocalTime.parse("10:15:20", DateTimeFormatter.ISO_LOCAL_TIME);
System.out.println("LocalTime9 : " + localTime9);
LocalTime localTime10 = LocalTime.ofInstant(Instant.now(), ZoneId.systemDefault());
System.out.println("LocalTime10: " + localTime10);
}
}
LocalTime1 : 14:53:49.920827100 LocalTime2 : 06:53:49.958803400 LocalTime3 : 14:53:49.959802500 LocalTime4 : 03:05:15 LocalTime5 : 15:20:45.123456789 LocalTime6 : 23:59:59.123456789 LocalTime7 : 23:59:59 LocalTime8 : 20:45:30.956702500 LocalTime9 : 10:15:20 LocalTime10: 14:53:49.990784200
Getting Information from a LocalTime
Following methods can be used to access Time information from a LocalTime:
- int get(TemporalField field): Gets the value of the specified field from this time as an int.
- int getHour(): Gets the hour-of-day field.
- long getLong(TemporalField field): Gets the value of the specified field from this time as a long.
- int getMinute(): Gets the minute-of-hour field.
- int getNano(): Gets the nano-of-second field.
- int getSecond(): Gets the second-of-minute field.
- long toNanoOfDay(): Extracts the time as nanos of day, from 0 to 24 * 60 * 60 * 1,000,000,000 - 1.
- int toSecondOfDay(): Extracts the time as seconds of day, from 0 to 24 * 60 * 60 - 1.
Since Java 9:
- long toEpochSecond(LocalDate date, ZoneOffset offset): Converts this LocalTime to the number of seconds since the epoch of 1970-01-01T00:00:00Z.
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;
public class LocalTimeInfoExample {
public static void main(String[] args) {
LocalTime localTime = LocalTime.now();
System.out.println("LocalTime : " + localTime);
System.out.println("Hour : " + localTime.getHour());
System.out.println("Minute : " + localTime.getMinute());
System.out.println("Second : " + localTime.getSecond());
System.out.println("Nano : " + localTime.getNano());
System.out.println("HOUR_OF_DAY : " + localTime.get(ChronoField.HOUR_OF_DAY));
System.out.println("MINUTE_OF_HOUR : " + localTime.get(ChronoField.MINUTE_OF_HOUR));
System.out.println("SECOND_OF_MINUTE: " + localTime.get(ChronoField.SECOND_OF_MINUTE));
System.out.println("MINUTE_OF_DAY : " + localTime.getLong(ChronoField.MINUTE_OF_DAY));
System.out.println("SECOND_OF_DAY : " + localTime.getLong(ChronoField.SECOND_OF_DAY));
System.out.println("#toSecondOfDay : " + localTime.toSecondOfDay());
System.out.println("#toNanoOfDay : " + localTime.toNanoOfDay());
System.out.println("#toEpochSecond : " + localTime.toEpochSecond(LocalDate.now(), ZoneOffset.UTC));
}
}
LocalTime : 12:27:17.893373200 Hour : 12 Minute : 27 Second : 17 Nano : 893373200 HOUR_OF_DAY : 12 MINUTE_OF_HOUR : 27 SECOND_OF_MINUTE: 17 MINUTE_OF_DAY : 747 SECOND_OF_DAY : 44837 #toSecondOfDay : 44837 #toNanoOfDay : 44837893373200 #toEpochSecond : 1566649637
Add/Subtract Operations on LocalTime
Following methods used for add/subtract operation in a LocalTime:
- LocalTime minus(long amountToSubtract, TemporalUnit unit): Returns a copy of this time with the specified amount subtracted.
- LocalTime minus(TemporalAmount amountToSubtract): Returns a copy of this time with the specified amount subtracted.
- LocalTime minusHours(long hoursToSubtract): Returns a copy of this LocalTime with the specified number of hours subtracted.
- LocalTime minusMinutes(long minutesToSubtract): Returns a copy of this LocalTime with the specified number of minutes subtracted.
- LocalTime minusNanos(long nanosToSubtract): Returns a copy of this LocalTime with the specified number of nanoseconds subtracted.
- LocalTime minusSeconds(long secondsToSubtract): Returns a copy of this LocalTime with the specified number of seconds subtracted.
- LocalTime plus(long amountToAdd, TemporalUnit unit): Returns a copy of this time with the specified amount added.
- LocalTime plus(TemporalAmount amountToAdd): Returns a copy of this time with the specified amount added.
- LocalTime plusHours(long hoursToAdd): Returns a copy of this LocalTime with the specified number of hours added.
- LocalTime plusMinutes(long minutesToAdd): Returns a copy of this LocalTime with the specified number of minutes added.
- LocalTime plusNanos(long nanosToAdd): Returns a copy of this LocalTime with the specified number of nanoseconds added.
- LocalTime plusSeconds(long secondstoAdd): Returns a copy of this LocalTime with the specified number of seconds added.
import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class LocalTimeAddSubstractExample {
public static void main(String[] args) {
LocalTime localTime = LocalTime.parse("20:15:30");
System.out.println("LocalTime : " + localTime);
// Adding/subtracting hours
System.out.println("12 hours before : " + localTime.minusHours(12));
System.out.println("6 hours later : " + localTime.plusHours(6));
// Adding/subtracting minutes
System.out.println("Minus 40 minutes : " + localTime.minusMinutes(40));
System.out.println("Plus 15 minutes : " + localTime.plusMinutes(15));
// Adding/subtracting seconds
System.out.println("Minus 30 seconds : " + localTime.minusSeconds(30));
System.out.println("Plus 20 seconds : " + localTime.plusSeconds(20));
// Adding/subtracting Nanos
System.out.println("Minus 20000 nanos : " + localTime.minusNanos(20000));
System.out.println("Plus 340000 nanos : " + localTime.plusNanos(340000));
// Using HOURS
System.out.println("8 hours before : " + localTime.minus(8, ChronoUnit.HOURS));
// Using MINUTES
System.out.println("35 minutes before : " + localTime.minus(35, ChronoUnit.MINUTES));
// Using SECONDS
System.out.println("125 seconds later : " + localTime.plus(125, ChronoUnit.SECONDS));
// Using NANOS
System.out.println("42357500 nanos later: " + localTime.plus(42357500, ChronoUnit.NANOS));
// Using TemporalAmount - Duration
System.out.println("160 minutes before : " + localTime.minus(Duration.ofMinutes(160)));
System.out.println("2 hours later : " + localTime.plus(Duration.ofHours(2)));
}
}
LocalTime : 20:15:30 12 hours before : 08:15:30 6 hours later : 02:15:30 Minus 40 minutes : 19:35:30 Plus 15 minutes : 20:30:30 Minus 30 seconds : 20:15 Plus 20 seconds : 20:15:50 Minus 20000 nanos : 20:15:29.999980 Plus 340000 nanos : 20:15:30.000340 8 hours before : 12:15:30 35 minutes before : 19:40:30 125 seconds later : 20:17:35 42357500 nanos later: 20:15:30.042357500 160 minutes before : 17:35:30 2 hours later : 22:15:30
Comparing LocalTimes
Following methods can be use to compare two LocalTimes:
- int compareTo(LocalTime other): Compares this time to another time.
- boolean isAfter(LocalTime other): Checks if this time is after the specified time.
- boolean isBefore(LocalTime other): Checks if this time is before the specified time.
import java.time.LocalTime;
public class LocalTimeCompareExample {
public static void main(String[] args) {
LocalTime localTime1 = LocalTime.parse("20:15:30");
LocalTime localTime2 = LocalTime.parse("08:15:30");
System.out.println("LocalTime1 : " + localTime1);
System.out.println("LocalTime2 : " + localTime2);
System.out.println("LocalTime1 after LocalTime2 : " + localTime1.isAfter(localTime2));
System.out.println("LocalTime1 before LocalTime2 : " + localTime1.isBefore(localTime2));
System.out.println("LocalTime1 compareTo LocalTime2: " + localTime1.compareTo(localTime2));
System.out.println("LocalTime2 compareTo LocalTime1: " + localTime2.compareTo(localTime1));
}
}
LocalTime1 : 20:15:30 LocalTime2 : 08:15:30 LocalTime1 after LocalTime2 : true LocalTime1 before LocalTime2 : false LocalTime1 compareTo LocalTime2: 1 LocalTime2 compareTo LocalTime1: -1
Supported Field and Unit of a LocalTime
Use isSupported(...) to check if a particular field/unit is supported in a LocalTime:
- boolean isSupported(TemporalField field): Checks if the specified field is supported.
- boolean isSupported(TemporalUnit unit): Checks if the specified unit is supported.
import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
public class LocalTimeIsSupportedExample {
public static void main(String[] args) {
LocalTime localTime = LocalTime.now();
System.out.println("*** ChronoField ***");
for(ChronoField chronoField : ChronoField.values()){
System.out.println(chronoField + " is supported:" + localTime.isSupported(chronoField));
}
System.out.println("\n*** ChronoUnit ***");
for(ChronoUnit chronoUnit : ChronoUnit.values()){
System.out.println(chronoUnit + " is supported:" + localTime.isSupported(chronoUnit));
}
}
}
*** ChronoField *** NanoOfSecond is supported:true NanoOfDay is supported:true MicroOfSecond is supported:true MicroOfDay is supported:true MilliOfSecond is supported:true MilliOfDay is supported:true SecondOfMinute is supported:true SecondOfDay is supported:true MinuteOfHour is supported:true MinuteOfDay is supported:true HourOfAmPm is supported:true ClockHourOfAmPm is supported:true HourOfDay is supported:true ClockHourOfDay is supported:true AmPmOfDay is supported:true DayOfWeek is supported:false AlignedDayOfWeekInMonth is supported:false AlignedDayOfWeekInYear is supported:false DayOfMonth is supported:false DayOfYear is supported:false EpochDay is supported:false AlignedWeekOfMonth is supported:false AlignedWeekOfYear is supported:false MonthOfYear is supported:false ProlepticMonth is supported:false YearOfEra is supported:false Year is supported:false Era is supported:false InstantSeconds is supported:false OffsetSeconds is supported:false *** ChronoUnit *** Nanos is supported:true Micros is supported:true Millis is supported:true Seconds is supported:true Minutes is supported:true Hours is supported:true HalfDays is supported:true Days is supported:false Weeks is supported:false Months is supported:false Years is supported:false Decades is supported:false Centuries is supported:false Millennia is supported:false Eras is supported:false Forever is supported:false
Getting LocalDateTime or OffsetTime from a LocalTime
Following method is used to create LocalDateTime from a LocalTime:
- LocalDateTime atDate(LocalDate date): Combines this time with a date to create a LocalDateTime.
And this method is to create OffsetTime from a LocalTime:
- OffsetTime atOffset(ZoneOffset offset): Combines this time with an offset to create an OffsetTime.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
public class LocalTimeToLocalOffsetTimeExample {
public static void main(String[] args) {
LocalTime localTime = LocalTime.parse("10:15:20.45675");
LocalDateTime localDateTime = localTime.atDate(LocalDate.now());
System.out.println("LocalDateTime: " + localDateTime);
OffsetTime offsetTime = localTime.atOffset(ZoneOffset.UTC);
System.out.println("OffsetTime : " + offsetTime);
}
}
LocalDateTime: 2019-08-24T10:15:20.456750 OffsetTime : 10:15:20.456750Z
Conclusion
The LocalTime class represents a specific time of day without any Date information. If you need both Date and Time information, you should use class LocalDateTime instead.