The Class LocalTime
In this lesson, we will explore a standard class that enables us to get the current time of day at our location.
The Java Class Library contains the package java.time
, which contains classes related to dates and times. Among them is the class LocalTime
. An object in this class represents the actual time of day that it was created. The time is recorded in 24-hour notation to the nearest nanosecond as hh:mm:ss.ddddddddd.
The import
statement
As we saw in an earlier lesson, Simple Input from a Keyboard, if our program uses a class from a package, we write an import
statement that appears before the rest of our program. To import the class LocalTime
from the package java.time
, we write
import java.time.LocalTime;
If we use more than one class in a package, we can import the entire package by writing an asterisk instead of a class name, as in
import java.time.*;
While importing an entire package is common among some programmers, we will import each class separately to help us associate classes with their packages.
Creating a LocalTime
object
To get the current time of day, we use LocalTime
’s static method now
, as we see in the next program:
Get hands-on with 1200+ tech skills courses.