Java 8 LocalTime class

The java.time.LocalTime class is an immutable class that represents a time, often viewed as hour-minute-second. 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.

Java LocalTime class methods

Method Description
LocalDateTime atDate(LocalDate date) It is used to combine this time with a date to create a LocalDateTime.
int compareTo(LocalTime other) It is used to compare this time to another time.
String format(DateTimeFormatter formatter) It is used to format this time using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this time as an int.
LocalTime minusHours(long hoursToSubtract) It is used to return a copy of this LocalTime with the specified number of hours subtracted.
LocalTime minusMinutes(long minutesToSubtract) It is used to return a copy of this LocalTime with the specified number of minutes subtracted.
static LocalTime now() It is used to obtain the current time from the system clock in the default time-zone.
static LocalTime of(int hour, int minute, int second) It is used to obtain an instance of LocalTime from an hour, minute and second.
LocalTime plusHours(long hoursToAdd) It is used to return a copy of this LocalTime with the specified number of hours added.
LocalTime plusMinutes(long minutesToAdd) It is used to return a copy of this LocalTime with the specified number of minutes added.

Example

package com.w3schools;
 
import java.time.LocalTime;
 
public class TestExample {
	public static void main(String args[]){
		 LocalTime time1 = LocalTime.now();  
		 System.out.println(time1);  
		 LocalTime time2 = LocalTime.of(11,23,32);  
		 System.out.println(time2);  
		 LocalTime time3 = LocalTime.of(10,23,32);  
		 System.out.println(time3);  
		 LocalTime time4=time3.minusHours(2);  
		 LocalTime time5=time4.minusMinutes(34);  
		 System.out.println(time5);  
	}  
}

Output

18:40:48.976
11:23:32
10:23:32
07:49:32