Open In App

java.time.LocalTime Class in Java

Last Updated : 09 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Java is the most popular programming language and widely used programming language. Java is used in all kinds of applications like mobile applications, desktop applications, web applications. As in Java, java.time.LocalTime class represents time, which is viewed as hour-minute-second. This class is immutable and also thread-safe. There are various methods under java.time.LocalTime class which is explained below and implementation is also performed below. java.time is a package used to work with the current date and time API.

  • Import classes such as java.time.LocalTime.
  • Now use LocalTime.now() to store current time.
  • Display the current time which is stored in a variable.

 First most all the methods of this class is demonstrated below in tabular format as shown:

Method Description
adjustInto(Temporal temporal)

All MethodsStatic MethodsInstance MethodsConcrete Methods

Modifier and Type Method and Description

Temporal

atDate(LocalDate date) Combines this time with a date to create a LocalDateTime.
atOffset(ZoneOffset offset) Combines this time with an offset to create an OffsetTime.
compareTo(LocalTime other) Compares this time to another time.
equals(Object obj) Checks if this time is equal to another time.
format(DateTimeFormatter formatter) Formats this time using the specified formatter.
get(TemporalField field) Gets the value of the specified field from this time as an int.
getHour() Gets the hour-of-day field.
getMinute() Gets the minute-of-hour field.
getNano() Gets the nano-of-second field.
getSecond() Gets the second-of-minute field.
hashCode() A hash code for this time.
isAfter(LocalTime other) Checks if this time is after the specified time.
isBefore(LocalTime other) Checks if this time is before the specified time.
isSupported(TemporalField field) Checks if the specified field is supported.
minus(long amountToSubtract, TemporalUnit unit) Returns a copy of this time with the specified amount subtracted.
minusHours(long hoursToSubtract) Returns a copy of this LocalTime with the specified number of hours subtracted.
minusMinutes(long minutesToSubtract) Returns a copy of this LocalTime with the specified number of minutes subtracted.
minusNanos(long nanosToSubtract) Returns a copy of this LocalTime with the specified number of nanoseconds subtracted.
minusSeconds(long secondsToSubtract) Returns a copy of this LocalTime with the specified number of seconds subtracted.
 now() Obtains the current time from the system clock in the default time-zone.
of(int hour, int minute) Obtains an instance of LocalTime from an hour and minute.
ofNanoOfDay(long nanoOfDay) Obtains an instance of LocalTime from a nanos-of-day value.
ofSecondOfDay(long secondOfDay) Obtains an instance of LocalTime from a second-of-day value.
parse(CharSequence text) Obtains an instance of LocalTime from a text string such as 10:15.
plus(TemporalAmount amountToAdd) Returns a copy of this time with the specified amount added.
plusHours(long hoursToAdd) Returns a copy of this LocalTime with the specified number of hours added.
plusMinutes(long minutesToAdd) Returns a copy of this LocalTime with the specified number of minutes added.
plusNanos(long nanosToAdd) Returns a copy of this LocalTime with the specified number of nanoseconds added.
plusSeconds(long secondstoAdd) Returns a copy of this LocalTime with the specified number of seconds added.
query(TemporalQuery<R> query) Queries this time using the specified query.
range(TemporalField field) Gets the range of valid values for the specified field.
toNanoOfDay() Extracts the time as nanos of day, from 0 to 24 * 60 * 60 * 1,000,000,000 – 1.
truncatedTo(TemporalUnit unit) Returns a copy of this LocalTime with the time truncated.
with(TemporalAdjuster adjuster) Returns an adjusted copy of this time.
withHour(int hour) Returns a copy of this LocalTime with the hour-of-day altered.
withMinute(int minute) Returns a copy of this LocalTime with the minute-of-hour altered.
withNano(int nanoOfSecond) Returns a copy of this LocalTime with the nano-of-second altered.
withSecond(int second) Returns a copy of this LocalTime with the second-of-minute altered.

Implementation:

Example 1

Java




// Java Program to illustrate LocalTime Class
 
// Importing LocalDateTime class from
// java.time package
import java.time.LocalTime;
 
// Main Class
public class  GFG {
   
    // Main driver method
    public static void main(String args[]) {
       
      // Creating an object of LocalTime class by
      // declaring a variable to store LocalTime
        LocalTime ltime = LocalTime.now();
       
      // Print and display the LocalTime value
        System.out.println("Local time value : "+ltime);       
    }
}


Output

Local time value : 06:20:52.986897

Example 2

Java




// Java Program to illustrate LocalTime
// where plusMethod() is illustrated
 
// Importing LocalTime class from
// java.time package
import java.time.LocalTime;
 
// Main Class
public class GFG {
   
    // main driver method
    public static void main(String args[])
    {
        // Creating an object of LocalTime Class by
        // declaring a variable to store LocalTime
        LocalTime ltime = LocalTime.now();
         
      // Now plusMinute() will add up minutes to the LocalTime
        LocalTime time3 = ltime.plusMinutes(120);
         
        // Display the time using object of class
        System.out.println("Time : "+time3);
    }
}


Output

Time : 08:34:59.135137

Example 3 

Java




// Java Program to illustrate LocalTime
// where plusMethod() is illustrated
 
// Importing LocalTime class from
// java.time package
import java.time.LocalTime;
 
// Main Class
public class GFG {
   
   // Main class
    public static void main(String args[])
    {
        // Creating an object of LocalTime class by
        // declare a variable to store LocalTime
        LocalTime ltime = LocalTime.now();
       
        // plusHours() method will add up Hours to
        // the LocalTime
        LocalTime time3 = ltime.plusHours(05);
         
        // Display the time
        System.out.println(time3);
    }
}


Output

17:48:53.003478

Example 4 

Java




// Java Program to illustrate LocalTime
// where plusMethod() is illustrated
 
// Importing LocalTime class from
// java.time package
import java.time.LocalTime;
 
// Main Class
public class GFG {
   
    // Main driver method
    public static void main(String args[])
    {
        // creating an object of. LocalTime Class by
        // declare a variable to store LocalTime
        LocalTime ltime = LocalTime.now();
       
        // minusMinute() will subtract minutes from
        // the LocalTime
        LocalTime time = ltime.minusMinutes(120);
       
        // Display the time
        System.out.println(time);
    }
}


Output

10:54:49.649675

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads