The class LocalTime represents the time without time zone information such as 11:20:45. LocalTime can be used to represent time upto nanosecond precision. For example, 15:40.25.123456789 can be represented by an instance of LocalTime. This class is immutable and thread-safe.
Java LocalTime class
public final class LocalTime extends Object implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable
Java LocalTime class – Method Summary
Method | Description |
---|---|
static LocalTime now() | It returns the current system time in default time zone. |
static LocalTime of(int hour, int minute) | It returns the instance of LocalTime from an hour and minute |
int get(TemporalField field) | It returns the value of the specified field from this time as an int. |
String format(DateTimeFormatter formatter) | This method is used to format the time using the specified formatter. |
int compareTo(LocalTime other) | It is used for comparison, it compares this time with another time. It returns positive value if this time is greater than other time, negative if this time is less than the other time. |
LocalDateTime atDate(LocalDate date) | It attaches the date to the LocalTime and returns the LocalDateTime instance that has this time along with the attached date. |
LocalTime plusHours(long hoursToAdd) | It returns a copy of this LocalTime after adding the specified hours to it. |
LocalTime minusHours(long hoursToSubtract) | It returns a copy of this LocalTime after subtracting the specified hours from it. |
boolean isAfter(LocalTime other) | It checks if this time is after the specified time. |
boolean isBefore(LocalTime other) | It checks if this time is before the specified time. |
Example 1: A Simple Example of LocalTime in Java
In this example we are creating the instances of LocalTime by calling now()
and of()
methods. The now()
method returns the current system time while the of()
method returns the instance of LocalTime that represents the time for the given hour, minutes, seconds and nanoseconds values.
import java.time.LocalTime; public class Example { public static void main(String[] args) { //now() method is used for getting the current time LocalTime currentTime = LocalTime.now(); System.out.println(currentTime); /*of() method is used for creating an instance of LocalTime * by using the given hour, minutes and seconds values */ LocalTime localTime = LocalTime.of(15,40,55); System.out.println(localTime); } }
Output:
21:22:57.555 15:40:55
Example 2: Getting hour, minutes, seconds and nanoseconds
In this example, we are demonstrating the use of getHour()
, getMinute()
, getSecond()
and getNano()
methods of LocalTime class.
import java.time.LocalTime; public class Example { public static void main(String[] args) { LocalTime localTime = LocalTime.of(15,40,55, 1234); //getting hour from the LocalTime System.out.println("Hour: "+localTime.getHour()); //getting minutes from the LocalTime System.out.println("Minute: "+localTime.getMinute()); //getting seconds from the LocalTime System.out.println("Second: "+localTime.getSecond()); //getting nano seconds from the LocalTime System.out.println("Nano Second: "+localTime.getNano()); } }
Output:
Hour: 15 Minute: 40 Second: 55 Nano Second: 1234
Example 3: plusHours(), plusMinutes(), plusSeconds() and plusNanos()
These methods are used for adding the hours, minutes, seconds and nanoseconds to the local time.
import java.time.LocalTime; public class Example { public static void main(String[] args) { //time 16:45:55:222222146 LocalTime localTime = LocalTime.of(16, 45, 55, 222222146); System.out.println("Given Time: "+localTime); //plusHours() method adds the specified hours to the given local time LocalTime localTime2 = localTime.plusHours(5); System.out.println("Adding 5 hours to the given time: "+ localTime2); //plusMinutes() method adds the specified minutes to the given local time LocalTime localTime3 = localTime.plusMinutes(7); System.out.println("Adding 7 minutes to the given time: "+localTime3); //plusSeconds() method adds the specified seconds to the given local time LocalTime localTime4 = localTime.plusSeconds(2); System.out.println("Adding 2 seconds to the given time : "+localTime4); //plusNanos() method adds the specified nanoseconds to the given local time LocalTime localTime5 = localTime.plusNanos(14); System.out.println("Adding 14 nanoseconds to the given time: "+localTime5); } }
Output:
Given Time: 16:45:55.222222146 Adding 5 hours to the given time: 21:45:55.222222146 Adding 7 minutes to the given time: 16:52:55.222222146 Adding 2 seconds to the given time : 16:45:57.222222146 Adding 14 nanoseconds to the given time: 16:45:55.222222160
Example 4: minusHours(), minusMinutes(), minusSeconds() and minusNanos()
These methods are used to subtract the specified time from the local time.
import java.time.LocalTime; public class Example { public static void main(String[] args) { //time 16:45:55:222222146 LocalTime localTime = LocalTime.of(16, 45, 55, 222222146); System.out.println("Given Time: "+localTime); //minusHours() method subtracts the specified hours from the given time LocalTime localTime2 = localTime.minusHours(3); System.out.println("Subtracting 3 hours from the given time: "+ localTime2); //minusMinutes() method subtracts the specified minutes from the given time LocalTime localTime3 = localTime.minusMinutes(5); System.out.println("Subtracting 5 minutes from the given time: "+localTime3); //minusSeconds() method subtracts the specified seconds from the given time LocalTime localTime4 = localTime.minusSeconds(10); System.out.println("Subtracting 10 seconds from the given time : "+localTime4); //minusNanos() method subtracts the specified nanoseconds from the given time LocalTime localTime5 = localTime.minusNanos(16); System.out.println("Subtracting 16 nanoseconds from the given time: "+localTime5); } }
Output:
Given Time: 16:45:55.222222146 Subtracting 3 hours from the given time: 13:45:55.222222146 Subtracting 5 minutes from the given time: 16:40:55.222222146 Subtracting 10 seconds from the given time : 16:45:45.222222146 Subtracting 16 nanoseconds from the given time: 16:45:55.222222130
Example 5: LocalTime methods – isBefore() and isAfter()
These methods check whether the time is before or after the specified time.
import java.time.LocalTime; public class Example { public static void main(String[] args) { //time1 16:45:55:222222146 LocalTime localTime = LocalTime.of(16, 45, 55, 222222146); System.out.println("Given Time: "+localTime); //time2 current time LocalTime localTime2 = LocalTime.now(); System.out.println("Current Time: "+localTime2); /* public boolean isBefore(LocalTime other): Checks if * this time is before the specified time. */ System.out.println("current time before given time? "+ localTime2.isBefore(localTime)); /* public boolean isAfter(LocalTime other): Checks if * this time is after the specified time. */ System.out.println("current time after given time? "+ localTime2.isAfter(localTime)); } }
Output:
Given Time: 16:45:55.222222146 Current Time: 22:15:48.842 current time before given time? false current time after given time? true
Example 6: withHour(), withMinute(), withSecond() and withNano()
These methods are used for altering the hours, minutes, seconds and nanoseconds values in the returned copy of the given local time.
import java.time.LocalTime; public class Example { public static void main(String[] args) { //time 19:25:44:111122223 LocalTime localTime = LocalTime.of(19, 25, 44, 111122223); System.out.println("Given Time: "+localTime); /* public LocalTime withHour(int hour): This method returns the copy * of the local time with the hour altered */ LocalTime localTime2 = localTime.withHour(11); System.out.println("LocalTime with hours altered: "+localTime2); /* public LocalTime withMinute(int minute): This method returns the copy * of the local time with the minutes altered */ LocalTime localTime3 = localTime.withMinute(30); System.out.println("LocalTime with minutes altered: "+localTime3); /* public LocalTime withSecond(int second): This method returns the * copy of the local time with the seconds altered */ LocalTime localTime4 = localTime.withSecond(55); System.out.println("LocalTime with seconds altered: "+localTime4); /* public LocalTime withNano(int nanosecond): This method returns the * copy of the local time with the nanoSeconds altered */ LocalTime localTime5 = localTime.withNano(999); System.out.println("LocalTime with nanoseconds altered: "+localTime5); } }
Output:
Given Time: 19:25:44.111122223 LocalTime with hours altered: 11:25:44.111122223 LocalTime with minutes altered: 19:30:44.111122223 LocalTime with seconds altered: 19:25:55.111122223 LocalTime with nanoseconds altered: 19:25:44.000000999
Leave a Reply