java.time.LocalTime class, introduced in Java 8, represents a local time object without the date or timezone information as hour-minute-second parts. It represents time to nanosecond precision e.g. 09:25:59.123456789
We can use the LocalTime
instances where we need to represent a time without any need of the date or timezone reference as seen in a wall clock. For example, we can use LocalTime
to represent LocalTime class to capture at which time an employee entered the office building and at which time left.
Note that LocalTime
instances are immutable and thread-safe.
1. LocalTime class declaration
public final class LocalTime extends Object implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable { //class body }
2. How to create LocalTime in Java
Generally, we will be creating local time instances in two conditions i.e. get current time or create local time for a specified timestamp.
2.1. Get current local time
Use following method to get the current local time.
LocalTime now = LocalTime.now();
2.2. Create specified local time
To create a local time with specific hour, minute and seconds – use below methods.
LocalTime ltObject1 = LocalTime.of(08, 20, 45); LocalTime ltObject2 = LocalTime.of(08, 20, 45, 123456789); LocalTime ltObject3 = LocalTime.parse("08:20"); LocalTime ltObject4 = LocalTime.parse("08:20:45.123456789");
3. How to parse string to LocalTime
The LocalTime
class has two overloaded parse() methods to convert time in string to local time instance.
parse(CharSequence text) //1 parse(CharSequence text, DateTimeFormatter formatter) //2
- Use first method if the string contains time in
ISO_LOCAL_TIME
pattern i.e. [HH]:[mm]:[ss]. This is default pattern of local time in Java. - For any other time pattern, we need to use second method where we pass the time as string as well as a formatter which represents the pattern of that time string.
//1 - default time pattern String time = "08:20:45.123456789"; LocalTime localTimeObj = LocalTime.parse(time); //2 - specific time pattern DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH.mm.ss.nnn"); String time = "08.20.45.123456789"; LocalTime localTimeObj = LocalTime.parse(time, formatter);
4. How to format LocalTime to string
Use LocalTime.format(DateTimeFormatter)
method to format a local time to a desired string representation.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH.mm"); LocalTime today = LocalTime.now(); String timeString = today.format(formatter); //12.38
5. How to modify local time
LocalTime
provides below methods which can be used to get to new local time instance relative to available local time instance.
plusHours()
plusMinutes()
plusSeconds()
plusNanos()
minusHours()
minusMinutes()
minusSeconds()
minusNanos()
LocalTime now = LocalTime.now(); //3 hours later LocalTime localTime1 = now.plusHours(3); //3 minutes earliar LocalTime localTime2 = now.minusMinutes(3);
6. More examples
Convert between LocalTime to java.sql.Time
Happy Learning !!
Ref : LocalTime Java Doc