1. LocalTime class

The LocalTime class was created for cases where you need to work with time but without a date. For example, suppose you're writing an alarm clock application. You care about the time, but not the date.

The LocalTime class is very similar to the LocalDate class — its objects similarly cannot be changed after creation.

Getting the current time

To create a new LocalTime object, you need to use the static now() method. Example:

LocalTime time = LocalTime.now();

Where time is a LocalTime variable, and LocalTime.now() is a call to the static now() method of the LocalTime class.


Code Console output
LocalTime time = LocalTime.now();
System.out.println("Now = " + time);

Now = 09:13:13.642881600

The dot is followed by the current number of nanoseconds.

2. Getting a specific time

To get a specific time, you need to use the static of() method. Example:

LocalTime time = LocalTime.of(hours, minutes, seconds, nanoseconds);

You pass in the hours, minutes, seconds and nanoseconds.


Code Console output
LocalTime time = LocalTime.of(12, 15, 0, 100);
System.out.println("Now = " + time);
Now = 12:15:00.000000100

By the way, there are two more variations of this method:

LocalTime time = LocalTime.of(hours, minutes, seconds);


LocalTime time = LocalTime.of(hours, minutes);

So you can use whichever is more convenient for you.

Getting a time based on the index of a second

You can also get the time by the index of a second in a day. To do this, we have the static ofSecondOfDay() method:

LocalTime time = LocalTime.ofSecondOfDay(seconds);

Where seconds is the number of seconds since the beginning of the day.


Code Console output
LocalTime time = LocalTime.ofSecondOfDay(10000);


Yes, 10,000 seconds is a little less than three hours. It's all correct.

3. Getting elements of time

To get the value of a specific element of time from a LocalTime object, we have these methods:

Method Description
int getHour()
Returns the hours
int getMinute()
Returns the minutes
int getSecond()
Returns the seconds
int getNano()
Returns the nanoseconds


Code Console output
LocalTime now = LocalTime.now();


4. Changing the time in a LocalTime object

The LocalTime class also has methods that let you work with time. The implementation of these methods is analogous to the methods of the LocalDate class: they do not change the existing LocalTime object, but instead return a new one with the desired data.

Here are the methods of the LocalTime class:

Method Description
plusHours(int hours)
Adds hours
plusMinutes(int minutes)
Adds minutes
plusSeconds(int seconds)
Adds seconds
plusNanos(int nanos)
Adds nanoseconds
minusHours(int hours)
Subtracts hours
minusMinutes(int minutes)
Subtracts minutes
minusSeconds(int seconds)
Subtracts seconds
minusNanos(int nanos)
Subtracts nanoseconds


Code Console output
LocalTime time = LocalTime.now();
LocalTime time2 = time.plusHours(2);
LocalTime time3 = time.minusMinutes(40);
LocalTime time4 = time.plusSeconds(3600);



Note that in each case we get a new time that is relative to the original time object. If you add 3600 seconds to a time, then you add exactly 1hour.