Java LocalTime
last modified January 27, 2024
Java LocalTime tutorial shows how to work with LocalTime in Java. We compute the current local time, parse local time, format local time, compare local time, and do time arithmetics.
Java LocalTime
LocalTime is a time without a time-zone in the ISO-8601
calendar system. LocalTime
is an immutable date-time object.
LocalTime
does not store or represent a date or time-zone.
It is a description of the local time as seen on a wall clock.
Wall time, also called real-world time or wall-clock time,
refers to elapsed time as determined by a chronometer such as
a wristwatch or wall clock.
The equals
method should be used for comparisons.
Java LocalTime current time
The current time is retrived with LocalTime.now
.
package com.zetcode; import java.time.LocalTime; public class JavaLocalTimeNow { public static void main(String[] args) { LocalTime now = LocalTime.now(); System.out.println(now); } }
The example prints the local current time.
18:12:05.172
Java LocalTime create
The are several ways to create LocalTime
in Java.
package com.zetcode; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class JavaLocalTimeCreate { public static void main(String[] args) { // Current Time LocalTime time1 = LocalTime.now(); System.out.println(time1); // Specific Time LocalTime time2 = LocalTime.of(7, 20, 45, 342123342); System.out.println(time2); // Specific Time LocalTime time3 = LocalTime.parse("12:32:22", DateTimeFormatter.ISO_TIME); System.out.println(time3); // Retrieving from LocalDateTime LocalTime time4 = LocalDateTime.now().toLocalTime(); System.out.println(time4); } }
The example presents four methods
LocalTime time1 = LocalTime.now();
The LocalTime.now
creates a current local time.
LocalTime time2 = LocalTime.of(7, 20, 45, 342123342);
With LocalTime.of
, we can create a specific local time
from an hour, minute, second and nanosecond.
LocalTime time3 = LocalTime.parse("12:32:22", DateTimeFormatter.ISO_TIME);
With LocalTime.parse
, we parse LocalTime
from a string.
LocalTime time4 = LocalDateTime.now().toLocalTime();
It is also possible to get LocalTime
from
a LocalDateTime
object.
18:18:12.135 07:20:45.342123342 12:32:22 18:18:12.186
Java LocalTime hour, minute, second
The following example splits a local time into hour, minute, and second parts.
package com.zetcode; import java.time.LocalTime; public class JavaLocalTimeParts { public static void main(String[] args) { LocalTime time = LocalTime.now(); System.out.printf("Hour: %s%n", time.getHour()); System.out.printf("Minute: %s%n", time.getMinute()); System.out.printf("Second: %s%n", time.getSecond()); } }
The getHour
gets the hour part, the getMinute
gets the minute part, and the getSecond
the second part
of the LocalTime
.
Hour: 18 Minute: 25 Second: 55
Java LocalTime zones
We can compute a local time for a specific time zone. LocalTime
, however,
does not store time zone information.
package com.zetcode; import java.time.LocalTime; import java.time.ZoneId; import java.time.temporal.ChronoUnit; public class JavaLocalTimeZone { public static void main(String[] args) { ZoneId zone1 = ZoneId.of("Europe/Bratislava"); ZoneId zone2 = ZoneId.of("Europe/Moscow"); LocalTime now1 = LocalTime.now(zone1); LocalTime now2 = LocalTime.now(zone2); System.out.printf("Bratislava time: %s%n", now1); System.out.printf("Moscow time: %s%n", now2); long hoursBetween = ChronoUnit.HOURS.between(now1, now2); long minutesBetween = ChronoUnit.MINUTES.between(now1, now2); System.out.println(hoursBetween); System.out.println(minutesBetween); } }
The example finds out current local time for Moscow and Bratislava. We also compute the time difference between the two cities.
ZoneId zone1 = ZoneId.of("Europe/Bratislava"); ZoneId zone2 = ZoneId.of("Europe/Moscow");
We specify the time zones with ZoneId.of
method.
LocalTime now1 = LocalTime.now(zone1); LocalTime now2 = LocalTime.now(zone2);
To create local times, we pass the zones to the LocalTime.now
.
long hoursBetween = ChronoUnit.HOURS.between(now1, now2); long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
We compute the difference between the two cities in hours and minutes.
Bratislava time: 11:00:42.704 Moscow time: 13:00:42.732 2 120
Java LocalTime format
The time is formatted differently in various countries.
DateTimeFormatter
helps us format the time.
package com.zetcode; import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class JavaLocalTimeFormat { public static void main(String[] args) { LocalTime now = LocalTime.now(); DateTimeFormatter dtf = DateTimeFormatter.ISO_TIME; System.out.println(now.format(dtf)); DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("hh:mm:ss"); System.out.println(now.format(dtf2)); DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("hh:mm:ss a"); System.out.println(now.format(dtf3)); } }
The example uses DateTimeFormatter
to format time.
DateTimeFormatter dtf = DateTimeFormatter.ISO_TIME; System.out.println(now.format(dtf));
We format the time to the ISO format time standart.
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("hh:mm:ss");
We can choose a specific time format with DateTimeFormatter.ofPattern
.
The documentation to DateTimeFormatter
contains the description
of various formatting characters that we can use.
11:08:56.483 11:08:56 11:08:56 AM
Java LocalTime arithmetic
Java LocalTime
has methods for doing time arithmetics.
package com.zetcode; import java.time.LocalTime; public class JavaLocalTimeArithmetic { public static void main(String[] args) { LocalTime now = LocalTime.now(); System.out.println("Current Time: " + now); // LocalTime addition System.out.println("Adding 3 hours: " + now.plusHours(3)); System.out.println("Adding 30 minutes: " + now.plusMinutes(30)); System.out.println("Adding 45 seconds: " + now.plusSeconds(45)); System.out.println("Adding 40000 nanoseconds: " + now.plusNanos(40000)); // LocalTime subtraction System.out.println("Subtracting 3 hours: " + now.minusHours(3)); System.out.println("Subtracting 30 minutes: " + now.minusMinutes(30)); System.out.println("Subtracting 45 seconds: " + now.minusSeconds(45)); System.out.println("Subtracting 40000 nanoseconds: " + now.minusNanos(40000)); } }
The example presents method for adding and subtracting time units.
System.out.println("Adding 3 hours: " + localTime.plusHours(3));
The plusHours
adds three hours to the current local time.
System.out.println("Subtracting 3 hours: " + now.minusHours(3));
Likewise, the minusHours
subtracts three hours from the
current local time.
Current Time: 11:12:51.155 Adding 3 hours: 14:12:51.155 Adding 30 minutes: 11:42:51.155 Adding 45 seconds: 11:13:36.155 Adding 40000 nanoseconds: 11:12:51.155040 Subtracting 3 hours: 08:12:51.155 Subtracting 30 minutes: 10:42:51.155 Subtracting 45 seconds: 11:12:06.155 Subtracting 40000 nanoseconds: 11:12:51.154960
Java LocalTime until
With the until
method, we can compute the time until
another time in terms of the specified unit.
package com.zetcode; import java.time.LocalTime; import java.time.temporal.ChronoUnit; public class JavaLocalTimeUntil { public static void main(String[] args) { LocalTime now = LocalTime.now(); LocalTime time = LocalTime.parse("22:15:30"); System.out.printf("%s hours%n", now.until(time, ChronoUnit.HOURS)); System.out.printf("%s minutes%n", now.until(time, ChronoUnit.MINUTES)); System.out.printf("%s seconds%n", now.until(time, ChronoUnit.SECONDS)); } }
The example calculates the time that has to elapse until another time in hours, minutes and seconds.
System.out.printf("%s hours%n", now.until(time, ChronoUnit.HOURS));
With ChronoUnit.HOURS
, we specify that we calculate the
the time difference in hours.
10 hours 657 minutes 39476 seconds
Java LocalTime compare
The following example shows how to compare times.
package com.zetcode; import java.time.LocalTime; public class JavaLocalTimeCompare { public static void main(String[] args) { LocalTime time1 = LocalTime.of(4, 23, 12); LocalTime time2 = LocalTime.of(8, 03, 50); LocalTime time3 = LocalTime.of(12, 47, 35); if (time1.compareTo(time2) == 0) { System.out.println("time1 and time2 are equal"); } else { System.out.println("time1 and time2 are not equal"); } if (time2.isBefore(time3)) { System.out.println("time2 comes before time3"); } else { System.out.println("time2 does not come before time3"); } if (time3.isAfter(time1)) { System.out.println("time3 comes after time1"); } else { System.out.println("time3 does not come after time1"); } } }
The example compares times. We check if they are equal, if the come before or after another time.
if (time1.compareTo(time2) == 0) {
The compareTo
compares two local times.
if (time2.isBefore(time3)) {
The isBefore
checks if a time comes before another
time.
if (time3.isAfter(time1)) {
The isAfter
checks if a time comes after another time.
time1 and time2 are not equal time2 comes before time3 time3 comes after time1
Java LocalTime truncate
The LocalTime's
truncatedTo
method returns
a copy of a local time with the time truncated.
package com.zetcode; import java.time.LocalTime; import java.time.temporal.ChronoUnit; public class JavaLocaTimeTruncate { public static void main(String[] args) { LocalTime now = LocalTime.now(); System.out.println(now); System.out.println(now.truncatedTo(ChronoUnit.HALF_DAYS)); System.out.println(now.truncatedTo(ChronoUnit.HOURS)); System.out.println(now.truncatedTo(ChronoUnit.MINUTES)); System.out.println(now.truncatedTo(ChronoUnit.SECONDS)); System.out.println(now.truncatedTo(ChronoUnit.MICROS)); } }
The example uses truncatedTo
to truncate time to
half days, hours, minutes, seconds, and micros.
11:27:56.309 00:00 11:00 11:27 11:27:56 11:27:56.309
Source
Java LocalTime - language reference
In this article we have worked with Java LocalTime.
Author
List all Java tutorials.