Java LocalTime Class
LocalTime is the Date and Time API introduced in Java 8 (java.time package), an important class used to represent time without time-zone information. It specifically handles the time of day, precise to the nanosecond level.
Features of the LocalTime class
1. Immutability
LocalTime is an immutable class; all modification operations return a new instance, and the original object remains unchanged.
2. No time-zone information
LocalTime does not contain time-zone information and only represents local time.
3. Time precision
Supports time precision from hours to nanoseconds (HH:mm:ss.nnnnnnnnn).
Creating a LocalTime object
1. Get the current time
LocalTime currentTime = LocalTime.now();
System.out.println("当前时间: " + currentTime);
2. Create using the of() method
Example
// Create 14:30:00
LocalTime time1
=
LocalTime.
of
(
14
,
30
)
;
// Create 09:15:30
LocalTime time2
=
LocalTime.
of
(
9
,
15
,
30
)
;
// Create 10:20:30.500 (500 milliseconds)
LocalTime time3
=
LocalTime.
of
(
10
,
20
,
30
,
500000000
)
;
3. Parse a string
LocalTime parsedTime = LocalTime.parse("08:45");
System.out.println("解析的时间: " + parsedTime);
// 2022-01-01 00:00:00 GMT
1. Get time components
Example
LocalTime time
=
LocalTime.
of
(
15
,
45
,
20
,
123456789
)
;
<
p
int
hour
=time.
getHour
(
)
;
// 15
int
minute
=
time.
getMinute
(
)
;
// 45
int
second
=
time.
getSecond
(
)
;
// 20
int
nano
=
time.
getNano
(
)
;
// 123456789
2. Time comparison
Example
LocalTime time1
=
LocalTime.
of
(
10
,
30
)
;
LocalTime time2
=
LocalTime.
of
(
11
,
15
)
;
boolean
isBefore
=
time1.
isBefore
(
time2
)
;
// true
boolean
isAfter
=
time1.
isAfter
(
time2
)
;
// false
boolean
isEqual
=
time1.
equals
(
time2
)
;
// false
3. Time arithmetic
Example
LocalTime time
=
LocalTime.
of
(
9
,
0
)
;
// Add 2 hours
LocalTime plusHours
=
time.
plusHours
(
2
)
;
// 11:00
// Subtract 30 minutes
LocalTime minusMinutes
=
time.
minusMinutes
(
30
)
;
// 08:30
// Add 1 hour 15 minutes
LocalTime plusDuration
=
time.
plus
(
Duration.
ofMinutes
(
75
)
)
;
// 10:15
4. Time adjustment
Example
LocalTime time
=
LocalTime.
of
(
14
,
30
)
;
// Set the hour to 10
LocalTime withHour
=
time.
withHour
(
10
)
;
// 10:30
// Set the minute to 45
LocalTime withMinute
=
time.
withMinute
(
45
)
;
// 14:45
Formatting and parsing
1. Format the time
Example
java LocalTime time
=
LocalTime.
of
(
16
,
45
,
30
)
;
DateTimeFormatter formatter
=
DateTimeFormatter.
ofPattern
(
"HH:mm:ss"
)
;
String
formattedTime
=
time.
format
(
formatter
)
;
// "16:45:30"
2. Parse a time string
Example
String
timeString
=
"08:15:45 AM"
;
DateTimeFormatter formatter
=
DateTimeFormatter.
ofPattern
(
"hh:mm:ss a"
)
;
LocalTime parsedTime
=
LocalTime.
parse
(
timeString, formatter
)
;
Practical Application Examples
1. Calculate the interval between two time points
Example
LocalTime start
=
LocalTime.
of
(
9
,
0
)
;
LocalTime end
=
LocalTime.
of
(
17
,
30
)
;
Duration duration
=
Duration.
between
(
start, end
)
;
System
.
out
.
println
(
"Working hours: "
duration.
toHours
(
)
hours
)
;
// 8 hours
2. Check whether within business hours
Example
LocalTime current
=
LocalTime.
now
(
)
;
LocalTime opening
=
LocalTime.
of
(
8
,
0
)
;
LocalTime closing
=
LocalTime.
of
(
22
,
0
)
;
if
(
current.
isAfter
(
opening
)
&&
current.
isBefore
(
closing
)
)
{
System
.
out
.
println
(
"Store is open"
)
;
}
else
{
System
.
out
.
println
(
"Store is closed"
)
;
}
3. Create a time range
Example
LocalTime start
=
LocalTime.
of
(
8
,
0
)
;
LocalTime end
=
start.
plusHours
(
8
)
;
// 16:00
System
.
out
.
println
(
"Working hours: "
start
to
end
)
;
Summary
LocalTime class is the core class for handling local time in the Java 8 Date and Time API. It provides rich methods to create, operate on, and format time. Due to its immutability and thread safety,LocalTime it is very suitable for use in multi-threaded environments. Mastering LocalTime the use of the class can greatly simplify time-related programming tasks.