-->
Skip to main content

Java LocalTime Class | Date and Time API

Java LocalTime Class 

Java LocalTime class is an immutable and thread-safe class introduced in new Date and Time API in Java8 and time is represented in format Hour:Minute:Seconds.Nanoseconds. For example. 04:15:15.555

All classes of Java8 Date/Time API are located in java.time package and today we are going to look at one of the classes from this package that is LocalTime.

Java LocalTime Class

LocalTime Structure

public final class LocalTime
extends Object
implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable

As we can see LocalTime is a final class and also immutable so we cannot inherit it and it implements interfaces Temporal, TemporalAdjuster, LocalTime, Serializable.

Introduction about the Java LocalTime class

  1. A time without a time-zone, such as 10:15:30 often viewed as hour-minute-second
  2. This class does not store or represent a date or time-zone.
  3. This is a value-based class, The equals method should be used for comparisons.
  4. LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second
  5. LocalTime is in java.time package
  6. This class is immutable and thread-safe.
For example, the value "13:45.30.123456789" can be stored in a LocalTime.

Now we will see how we can use LocalTime

Importing LocalTime Class

Java LocalTime class is in java.time package. Here what it looks like : 

import java.time.LocalTime;

Creating New LocalTime Instance

  1. Using from(TemporalAccessor temporal) to get an instance of LocalTime from a temporal object.
  2. // Obtains an instance of LocalTime from a temporal object.
    LocalTime time2 = LocalTime.from(LocalDateTime.now());
    System.out.println(time2); // only prints time from datetime in format 21:39:02.405
    

  3. Using now() to get the current time from the system clock in the default time-zone.
  4. // Obtains the current time from the system clock in the default time-zone.
    LocalTime currentTime = LocalTime.now();
    System.out.println(currentTime);// prints time in format 21:39:02.405
    

  5. Using now(Clock clock) to get the current time from the specified clock.
  6. // Obtains the current time from the specified clock.
    // Prints time in hour-minute-secondnanoseconds format
    LocalTime time = LocalTime.now(Clock.systemDefaultZone());
    System.out.println(time); // prints time in format 21:39:02.405
    

  7. Using now(ZoneId zone) to get the current time from the system clock in the specified time-zone.
  8. // Obtains the current time from the system clock in the specified time-zone.
    LocalTime time1 = LocalTime.now(ZoneId.systemDefault());
    System.out.println(time1); // prints time in format 21:39:02.405
    

  9. Using of(int hour, int minute) to get an instance of LocalTime from an hour and minute.
  10. // Obtains an instance of LocalTime from an hour and minute.
    LocalTime usingOf1 = LocalTime.of(12, 23);
    System.out.println(usingOf1); // 12:23
    

  11. Using of(int hour, int minute, int second) to get an instance of LocalTime from an hour, minute, and second.
  12. // Obtains an instance of LocalTime from an hour, minute and second.
    LocalTime usingOf2 = LocalTime.of(06, 17, 47);
    System.out.println(usingOf2); // 06:17:47
    

  13. Using of(int hour, int minute, int second, int nanoOfSecond) to get an instance of LocalTime from an hour, minute, second and nanosecond.
  14. // Obtains an instance of LocalTime from an hour, minute, second and nanosecond.
    LocalTime usingOf3 = LocalTime.of(06, 17, 47, 769999999);
    System.out.println(usingOf3); // 06:17:47.769999999
    

  15. Using ofNanoOfDay(long nanoOfDay) to get an instance of LocalTime from a nanos-of-day value.
  16. // Obtains an instance of LocalTime from a nanos-of-day value.
    LocalTime usingOfNanoDay = LocalTime.ofNanoOfDay(Integer.MAX_VALUE);
    System.out.println(usingOfNanoDay);// 00:00:02.147483647
    

  17. Using ofSecondOfDay(long secondOfDay) to get an instance of LocalTime from a second-of-day value.
  18. // Obtains an instance of LocalTime from a second-of-day value.
    LocalTime usingOfSecondOfDay = LocalTime.ofSecondOfDay(24 * 60 * 60 - 1);
    System.out.println(usingOfSecondOfDay); // 23:59:59
    

  19. Using parse(CharSequence text) to get an instance of LocalTime from a text string such as 10:15.
  20. // Obtains an instance of LocalTime from a text string such as 10:15.
    LocalTime parseTime = LocalTime.parse("13:34:10");
    System.out.println(parseTime); // 13:34:10
    

  21. Using parse(CharSequence text, DateTimeFormatter formatter) to get an instance of LocalTime from a text string using a specific formatter.
  22. // Obtains an instance of LocalTime from a text string using a specific formatter.
    LocalTime parseTimeWithFormatter = LocalTime.parse("12:32:55", DateTimeFormatter.ISO_LOCAL_TIME);
    System.out.println(parseTimeWithFormatter); // 12:32:55
    

Compare LocalTime objects in Java

  1. Using compareTo(LocalTime other) to compare this time to another time.
  2. // Comparing two localtime Objects
    LocalTime localtime1 = LocalTime.parse("12:45:10");
    LocalTime localtime2 = LocalTime.parse("12:45:10");
    System.out.println(localtime1.compareTo(localtime2)); // 0 means equal
    
    LocalTime localtime3 = LocalTime.parse("12:46:10");
    System.out.println(localtime1.compareTo(localtime3)); // -1 as time1 is less than time3.
    
    LocalTime localtime4 = LocalTime.parse("12:41:10");
    System.out.println(localtime1.compareTo(localtime4)); // 1 as time1 is greater than time4
    

  3. Using equals(Object obj) to check if this time is equal to another time.
  4. // Checks if this time is equal to another time.
    LocalTime t1 = LocalTime.parse("17:00:56");
    LocalTime t2 = LocalTime.parse("17:00:55");
    System.out.println(t1.equals(t2)); // false
    
    LocalTime t3 = LocalTime.parse("17:00:56");
    System.out.println(t1.equals(t3)); // true
    

  5. Using isAfter(LocalTime other) to check if this time is after the specified time.
  6. // Checks if this time is after the specified time.
    LocalTime t4 = LocalTime.parse("13:00:56");
    LocalTime t5 = LocalTime.parse("06:00:15");
    System.out.println(t4.isAfter(t5)); // true
    
    LocalTime t6 = LocalTime.parse("17:10:45");
    System.out.println(t4.isAfter(t6)); // false
    

  7. Using isBefore(LocalTime other) to check if this time is before the specified time.
  8. // Checks if this time is before the specified time.
    LocalTime t7 = LocalTime.parse("13:00:56");
    LocalTime t8 = LocalTime.parse("06:00:15");
    System.out.println(t7.isBefore(t8)); // false
    
    LocalTime t9 = LocalTime.parse("17:10:45");
    System.out.println(t7.isBefore(t9)); // true
    

LocalTime Format Time Example

  1. Using format(DateTimeFormatter formatter) formats this time using the specified formatter.
  2. // Formats this time using the specified formatter.
    LocalTime formatTime = LocalTime.parse("22:12:06.366");
    String formatedTime = formatTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
    System.out.println(formatedTime);// Prints formated time as 22:12:06 format
    

Truncating Time Example

  1. Using truncatedTo(TemporalUnit unit) returns a copy of this LocalTime with the time truncated.
  2. // Returns a copy of this LocalTime with the time truncated.
    LocalTime timeNow = LocalTime.parse("22:15:08.366");
    System.out.println("Truncate Examples");
    LocalTime newTruncatedTime1 = timeNow.truncatedTo(ChronoUnit.HOURS);
    System.out.println(newTruncatedTime1); // 22:00 with showing only Hours
    
    LocalTime newTruncatedTime2 = timeNow.truncatedTo(ChronoUnit.MINUTES);
    System.out.println(newTruncatedTime2); // 22:15 with showing hour Minutes
    
    LocalTime newTruncatedTime3 = timeNow.truncatedTo(ChronoUnit.SECONDS);
    System.out.println(newTruncatedTime3); // 22:15:08 with showing hour minutes Seconds
    
    LocalTime newTruncatedTime4 = timeNow.truncatedTo(ChronoUnit.NANOS);
    System.out.println(newTruncatedTime4); // 22:16:21.366 with showing hour minutes Seconds nanoseconds
    

Difference between two times 

  1. Using until(Temporal endExclusive, TemporalUnit unit) calculates the amount of time until another time in terms of the specified unit.

  2. // localtime difference between two localtime objects
    // Calculates the amount of time until another time in terms of the specified unit.
    
    LocalTime startTime = LocalTime.parse("19:23:42");
    LocalTime endTime = LocalTime.parse("23:45:34");
    
    // Time difference in hours
    System.out.println(startTime.until(endTime, ChronoUnit.HOURS)); //4
    
    // Time difference in minutes
    System.out.println(startTime.until(endTime, ChronoUnit.MINUTES)); // 261
     
    // Time difference in seconds
    System.out.println(startTime.until(endTime, ChronoUnit.SECONDS)); // 15712
    
    // Time difference in nanoseconds
    System.out.println(startTime.until(endTime, ChronoUnit.NANOS)); // 15712000000000
    

 Getting Time Information

  1. Using get(TemporalField field) gets the value of the specified field from this time as an int.
  2. //Gets the value of the specified field from this time as an int.
    LocalTime infoTime = LocalTime.parse("15:34:54.043");
    int hourOfDay = infoTime.get(ChronoField.HOUR_OF_DAY);
    System.out.println(hourOfDay); // 15
    		
    int minuteOfDay = infoTime.get(ChronoField.MINUTE_OF_DAY);
    System.out.println(minuteOfDay); // 934
    		
    int minuteOfHour = infoTime.get(ChronoField.MINUTE_OF_HOUR);
    System.out.println(minuteOfHour); // 34
    		
    int secondsOfDay = infoTime.get(ChronoField.SECOND_OF_DAY);
    System.out.println(secondsOfDay); // 56094
    		
    int secondOfMinute = infoTime.get(ChronoField.SECOND_OF_MINUTE);
    System.out.println(secondOfMinute); // 54
    		
    int nanosecondsOfDay = infoTime.get(ChronoField.NANO_OF_SECOND);
    System.out.println(nanosecondsOfDay); // 43000000
    

  3. Using getHour() gets the hour-of-day field.
  4. //Gets the hour-of-day field.
    LocalTime dayTime = LocalTime.parse("14:24:10.432");
    int hour = dayTime.getHour();
    System.out.println(hour);//14
    

  5. Using getLong(TemporalField field) gets the value of the specified field from this time as a long.
  6. //Gets the value of the specified field from this time as a long.
    
    LocalTime dayTime = LocalTime.parse("14:24:10.432");
    long
    hour1 = dayTime.getLong(ChronoField.HOUR_OF_DAY); System.out.println(hour1);//14 long hour2 = dayTime.getLong(ChronoField.HOUR_OF_AMPM); System.out.println(hour2);//2 long minute1 = dayTime.getLong(ChronoField.MINUTE_OF_DAY); System.out.println(minute1);//864 long minute2 = dayTime.getLong(ChronoField.MINUTE_OF_HOUR); System.out.println(minute2);//24 long second1= dayTime.getLong(ChronoField.SECOND_OF_DAY); System.out.println(second1); // 51850 long second2= dayTime.getLong(ChronoField.SECOND_OF_MINUTE); System.out.println(second2); // 10

  7. Using getMinute() gets the minute-of-hour field.
  8. //Gets the minute-of-hour field.
    
    LocalTime dayTime = LocalTime.parse("14:24:10.432");
    int
    minute = dayTime.getMinute(); System.out.println(minute); //24

  9. Using getNano() gets the nano-of-second field.
  10. //Gets the nano-of-second field.
    
    LocalTime dayTime = LocalTime.parse("14:24:10.432");
    int
    nanosec = dayTime.getNano(); System.out.println(nanosec); // 432000000

  11. Using getSecond() gets the second-of-minute field.
  12. //Gets the second-of-minute field.
    
    LocalTime dayTime = LocalTime.parse("14:24:10.432");
    int
    seconds = dayTime.getSecond(); System.out.println(seconds); // 10

LocalTime Minus Methods

LocalTime Minus Methods are used to get a past time using minus hours, minutes, seconds, nanos methods.
  1. Using minus(long amountToSubtract, TemporalUnit unit) returns a copy of this time with the specified amount subtracted.
  2. //Returns a copy of this time with the specified amount subtracted.
    LocalTime dayTime1 = LocalTime.parse("23:40:40.100");
    LocalTime minusTime1 = dayTime1.minus(1, ChronoUnit.HOURS);
    System.out.println(minusTime1); //22:40:40.100
    		
    LocalTime minusTime2 = dayTime1.minus(15, ChronoUnit.MINUTES);
    System.out.println(minusTime2); //23:25:40.100
    		
    LocalTime minusTime3 = dayTime1.minus(15, ChronoUnit.SECONDS);
    System.out.println(minusTime3); //23:40:25.100
    		
    LocalTime minusTime4 = dayTime1.minus(500000, ChronoUnit.NANOS);
    System.out.println(minusTime4); //23:40:40.099500
    

  3. Using minus(TemporalAmount amountToSubtract) returns a copy of this time with the specified amount subtracted.
  4. //Returns a copy of this time with the specified amount subtracted.
    LocalTime dayTime2 = LocalTime.parse("23:40:40.100");
    LocalTime minusTime5 = dayTime2.minus(Duration.ofHours(1));
    System.out.println(minusTime5); // 22:40:40.100
    		
    LocalTime minusTime6 = dayTime2.minus(Duration.ofMinutes(15));
    System.out.println(minusTime6); // 23:25:40.100
    		
    LocalTime minusTime7 = dayTime2.minus(Duration.ofSeconds(15));
    System.out.println(minusTime7); // 23:40:25.100
    		
    LocalTime minusTime8 = dayTime2.minus(Duration.ofNanos(500000));
    System.out.println(minusTime8); // 23:40:40.099500
    

  5. Using minusHours(long hoursToSubtract) returns a copy of this LocalTime with the specified number of hours subtracted.
  6. //Returns a copy of this LocalTime with the specified number of hours subtracted.
    LocalTime dayTime3 = LocalTime.parse("13:50:10.100");
    LocalTime minusHours = dayTime3.minusHours(4);
    System.out.println(minusHours); // 09:50:10.100
    

  7. Using minusMinutes(long minutesToSubtract) returns a copy of this LocalTime with the specified number of minutes subtracted.
  8. //Returns a copy of this LocalTime with the specified number of minutes subtracted.
    
    LocalTime dayTime3 = LocalTime.parse("13:50:10.100");
    LocalTime minusMinutes = dayTime3.minusMinutes(40); System.out.println(minusMinutes); // 13:10:10.100

  9. Using minusNanos(long nanosToSubtract) returns a copy of this LocalTime with the specified number of nanoseconds subtracted.
  10. //Returns a copy of this LocalTime with the specified number of nanoseconds subtracted.
    
    LocalTime dayTime3 = LocalTime.parse("13:50:10.100");
    LocalTime minusNanos = dayTime3.minusNanos(400000); System.out.println(minusNanos); // 13:50:10.099600

  11. Using minusSeconds(long secondsToSubtract) returns a copy of this LocalTime with the specified number of seconds subtracted.
  12. //Returns a copy of this LocalTime with the specified number of seconds subtracted.
    
    LocalTime dayTime3 = LocalTime.parse("13:50:10.100");
    LocalTime minusSeconds = dayTime3.minusSeconds(4); System.out.println(minusSeconds); // 13:50:06.100

LocalTime Plus methods

LocalTime Plus methods are used to get future time using plus hours, minutes, seconds, nanos methods.
  1. Using plus(long amountToAdd, TemporalUnit unit) returns a copy of this time with the specified amount added.
  2. //Returns a copy of this time with the specified amount added.
    LocalTime dayTime4 = LocalTime.parse("23:40:40.100");
    LocalTime plusTime1 = dayTime4.plus(1, ChronoUnit.HOURS);
    System.out.println(plusTime1);//00:40:40.100
    
    LocalTime plusTime2 = dayTime4.plus(15, ChronoUnit.MINUTES);
    System.out.println(plusTime2); //23:55:40.100
    		
    LocalTime plusTime3 = dayTime4.plus(15, ChronoUnit.SECONDS);
    System.out.println(plusTime3); //23:40:55.100
    	
    LocalTime plusTime4 = dayTime4.plus(500000, ChronoUnit.NANOS);
    System.out.println(plusTime4); //23:40:40.100500
    

  3. Using plus(TemporalAmount amountToAdd) returns a copy of this time with the specified amount added.
  4. //Returns a copy of this time with the specified amount added.
    LocalTime dayTime5 = LocalTime.parse("23:40:40.100");
    LocalTime plusTime5 = dayTime5.plus(Duration.ofHours(1));
    System.out.println(plusTime5); // 00:40:40.100
    		
    LocalTime plusTime6 = dayTime5.plus(Duration.ofMinutes(15));
    System.out.println(plusTime6); // 23:55:40.100
    		
    LocalTime plusTime7 = dayTime5.plus(Duration.ofSeconds(15));
    System.out.println(plusTime7); // 23:40:55.100
    		
    LocalTime plusTime8 = dayTime5.plus(Duration.ofNanos(500000));
    System.out.println(plusTime8); // 23:40:40.100500
    

  5. Using plusHours(long hoursToAdd) returns a copy of this LocalTime with the specified number of hours added.
  6. //Returns a copy of this LocalTime with the specified number of hours added.
    LocalTime dayTime6 = LocalTime.parse("13:50:10.100");
    LocalTime plusHours = dayTime6.plusHours(4);
    System.out.println(plusHours); // 17:50:10.100
    

  7. Using plusMinutes(long minutesToAdd) returns a copy of this LocalTime with the specified number of minutes added.
  8. //Returns a copy of this LocalTime with the specified number of minutes added.
    LocalTime dayTime6 = LocalTime.parse("13:50:10.100");
    LocalTime plusMinute11 = dayTime6.plusMinutes(40);
    System.out.println(plusMinute11); // 14:30:10.100
    

  9. Using plusNanos(long nanosToAdd) returns a copy of this LocalTime with the specified number of nanoseconds added.
  10. //Returns a copy of this LocalTime with the specified number of nanoseconds added.
    LocalTime dayTime6 = LocalTime.parse("13:50:10.100");
    LocalTime plusNanos = dayTime6.plusNanos(400000);
    System.out.println(plusNanos); // 13:50:10.100400
    

  11. Using plusSeconds(long secondstoAdd) returns a copy of this LocalTime with the specified number of seconds added.
  12. //Returns a copy of this LocalTime with the specified number of seconds added.
    LocalTime dayTime6 = LocalTime.parse("13:50:10.100");
    LocalTime plusSeconds12 = dayTime6.plusSeconds(4);
    System.out.println(plusSeconds12); // 13:50:14.100
    

LocalTime with Methods

with methods are method provided by LocalTime class to alter time according to hour, minute, seconds, nanos and get LocalTime altered value.
  1. Using with(TemporalAdjuster adjuster) returns an adjusted copy of this time.
  2. //Returns an adjusted copy of this time.
    LocalTime dayTime7 = LocalTime.parse("11:11:11.111");
    LocalTime adjustedTime1 = dayTime7.with(LocalTime.now());
    System.out.println(adjustedTime1);//23:14:23.843
    		
    

  3. Using with(TemporalField field, long newValue) returns a copy of this time with the specified field set to a new value.
  4. //Returns a copy of this time with the specified field set to a new value.
    LocalTime dayTime7 = LocalTime.parse("11:11:11.111");
    LocalTime adjustedTime2 = dayTime7.with(ChronoField.HOUR_OF_DAY, 21);
    System.out.println(adjustedTime2); // 21:11:11.111
    		
    LocalTime adjustedTime3 = dayTime7.with(ChronoField.MINUTE_OF_HOUR, 45);
    System.out.println(adjustedTime3); // 11:45:11.111
    
    LocalTime adjustedTime4 = dayTime7.with(ChronoField.SECOND_OF_MINUTE, 33);
    System.out.println(adjustedTime4); // 11:11:33.111
    		
    LocalTime adjustedTime5 = dayTime7.with(ChronoField.NANO_OF_SECOND, 3322232);
    System.out.println(adjustedTime5); // 11:11:11.003322232
    

  5. Using withHour(int hour) returns a copy of this LocalTime with the hour-of-day altered.
  6. // Returns a copy of this LocalTime with the hour-of-day altered.
    LocalTime dayTime8 = LocalTime.parse("15:15:15.555");
    LocalTime withHourAdjusted = dayTime8.withHour(04);
    System.out.println(withHourAdjusted); // 04:15:15.555
    

  7. Using withMinute(int minute) returns a copy of this LocalTime with the minute-of-hour altered.
  8. // Returns a copy of this LocalTime with the minute-of-hour altered.
    LocalTime dayTime8 = LocalTime.parse("15:15:15.555");
    LocalTime withMinuteAdjusted = dayTime8.withMinute(55);
    System.out.println(withMinuteAdjusted); // 15:55:15.555
    

  9. Using withNano(int nanoOfSecond) returns a copy of this LocalTime with the nano-of-second altered.
  10. // Returns a copy of this LocalTime with the nano-of-second altered.
    LocalTime dayTime8 = LocalTime.parse("15:15:15.555");
    LocalTime withNanoAdjusted = dayTime8.withNano(55555);
    System.out.println(withNanoAdjusted); // 15:15:15.000055555
    

  11. Using withSecond(int second) returns a copy of this LocalTime with the second-of-minute altered.
  12. // Returns a copy of this LocalTime with the second-of-minute altered.
    LocalTime dayTime8 = LocalTime.parse("15:15:15.555");
    LocalTime withSecondAdjusted = dayTime8.withSecond(04);
    System.out.println(withSecondAdjusted); // 15:15:04.555
    

String to LocalTime Example

String time can be converted into LocalTime using parse methods
  1. parse(CharSequence text): to get an instance of LocalTime from a text string such as 10:15:43.
  2. parse(CharSequence text, DateTimeFormatter formatter): to get an instance of LocalTime from a text string using a specific formatter. 
Below is the sample code using both methods

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

public class StringToLocalTimeExample {

	public static void main(String[] args) {
		String time1 = "13:34:10.987";
		LocalTime parseTime = LocalTime.parse(time1);
		System.out.println(parseTime); // 13:34:10.987

		String time2 = "23:43:33";
		LocalTime parseTimeWithFormatter = LocalTime.parse(time2, DateTimeFormatter.ofPattern("HH:mm:ss"));
		System.out.println(parseTimeWithFormatter); // 23:43:33

	}

}

Conclusion

In this post, we have seen 
  • How we can import LocalTime Class.
  • how to create a LocalTime instance.
  • how to compare LocalTime objects in Java.
  • LocalTime Format Time example.
  • How we can truncate Time with example.
  • Difference between two times with example.
  • Getting Time Information.
  • how to get past time using LocalTime Minus methods.
  • how to get future time using LocalTime Plus methods.
  • how to use with methods to get a specific time.
  • how to convert String to LocalTime example.
That's it for the LocalTime Java class. As we can see LocalTime helps in doing operations like plus hours, plus minutes, minus hours, minus minutes, compare time, format time, truncate time and many others with respect to current time with easy methods which are very helpful and handy thus one must use LocalTime java class features in the current project.

With Java 8 we get a very rich API for working with date and time. The API can completely replace old classes like java.util.Date or java.util.Calendar with newer, more flexible classes. Due to mostly immutable classes, the new API helps in building thread-safe systems. Java8 has come up with many classes like LocalDate, LocalTime, LocalDateTime, Instant, Clock, etc. We will look at more classes in further posts.

Happy Learning!!!

References: LocalTime Java Docs


Comments