Java LocalTime
- LocalTime provides time without any time zone information. It is very much similar to observing the time from a wall clock which just shown the time and not the time zone information.
- The assumption from this API is that all the calendar system uses the same way of representing the time.
- It is a value-based class, so use of reference equality (==), identity hash code or synchronization on instances of LocalTime may have unexpected results and is highly advised to be avoided. The equals method should be used for comparisons.
- The LocalTime class is immutable that mean any operation on the object would result in a new instance of LocalTime reference.
How to Create LocalTime Object
LocalTime objects can be created using the below mentioned ways.
- LocalTime instance can be created using the
now()
method of LocalTime class. There are two other overloaded now() methods that takes argument asClock
andZoneId
.LocalTime lt = LocalTime.now(); System.out.println(lt); //15:43:43.212787 LocalTime lt1 = LocalTime.now(Clock.systemDefaultZone()); System.out.println(lt1); //15:43:43.213454 LocalTime lt2 = LocalTime.now(ZoneId.systemDefault()); System.out.println(lt2); //15:43:43.213542
- We can create LocalTime instance using
of()
method. There are multiple overloaded methods for different arguments for hour, minute, second and nanosecond.LocalTime lt = LocalTime.of(10, 30); System.out.println(lt); //10:30 LocalTime lt1 = LocalTime.of(10, 30, 45); System.out.println(lt1); //10:30:45 LocalTime lt2 = LocalTime.of(10, 30, 45, 12345); System.out.println(lt2); //10:30:45.000012345 notice the left padding LocalTime lt3 = LocalTime.ofInstant(Instant.now(), ZoneId.systemDefault()); System.out.println(lt3); //15:48:58.358195
- We can use LocalTime parse() method to convert String to instance of LocalTime.
LocalTime lt = LocalTime.parse("10:30"); System.out.println(lt); //10:30 LocalTime lt1 = LocalTime.parse("10:30:45.1234"); System.out.println(lt1); //10:30:45.123400 LocalTime lt2 = LocalTime.parse("10.3.45.1234", DateTimeFormatter.ofPattern("H.m.s.n")); System.out.println(lt2); //10:03:45.000001234
Java LocalTime Methods
There are methods in LocalTime class for different purposes. We have divided it into following categories.
- Getting time from LocalTime –
getHour()
,getMinute()
,getSecond()
andgetNano()
. - Time manipulation –
plusHours()
,minusHours()
,plusMinutes()
,minusMinutes()
,plusSeconds()
,minusSeconds()
,plusNanos()
andminusNanos()
. - Comparison in LocalTime –
isAfter()
andisBefore()
to check if this LocalTime is after/before the specified time. We can use these methods to compare two local times.
Java LocalTime Example
Let’s look at java LocalTime example program to explore all the above mentioned methods.
package com.journaldev.java;
import java.time.LocalTime;
public class LocalTimeExample {
public static void main(String[] args) {
LocalTime lt = LocalTime.now();
System.out.println("Current Time: " + lt);
System.out.println("nHour: " + lt.getHour());
System.out.println("Minute: " + lt.getMinute());
System.out.println("Second: " + lt.getSecond());
System.out.println("Nanosecond: " + lt.getNano());
// add 2 hours, 10 minutes, 30 seconds and 1000 nanoseconds
lt = lt.plusHours(2);
lt = lt.plusMinutes(10);
lt = lt.plusSeconds(30);
lt = lt.plusNanos(1000);
System.out.println("nUpdated Time: " + lt);
// minus 2 hours, 10 minutes, 30 seconds and 1000 nanoseconds
lt = lt.minusHours(2);
lt = lt.minusMinutes(10);
lt = lt.minusSeconds(30);
lt = lt.minusNanos(1000);
System.out.println("nUpdated Time: " + lt);
LocalTime ct = LocalTime.now();
System.out.println("nlt before ct ? " + lt.isBefore(ct));
System.out.println("lt after ct ? " + lt.isAfter(ct));
}
}
That’s all for Java LocalTime example programs.
Reference: API Doc