Java LocalTime by Examples
Tags: Java LocalTime Java 8
In Java the java.time.LocalTime class is represent a time without a time-zone in the ISO-8601 calendar system. The java.time.LocalTime class was introduced from Java 8. In this Java core tutorial we learn how to use the java.time.LocalTime class in Java programming language with different example Java programs.
Table of contents
- Get Current System Time using LocalTime.now() method
- Create LocalTime object from Hour Minute Second and Nano of Second
- Create LocalTime object from Second of Day
- Create LocalTime object from Nano of Day
- Convert String to LocalTime
- Convert LocalTime to String
- Convert LocalTime to Second of Day
- Convert LocalTime to Nano of Day
- Convert LocalTime to Epoch Second
- Convert LocalTime to LocalDateTime
- Convert LocalTime to OffsetTime
- Convert Instant to LocalTime
- Get Minimum and Maximum of LocalTime
- Get Midnight and Noon LocalTime
- Get LocalTime Field Values
- Set Hour to LocalTime using withHour() method
- Set Minute to LocalTime using withMinute() method
- Set Second to LocalTime using withSecond() method
- Set Nano of Second to LocalTime using withNano() method
- Add Hours to LocalTime using plusHours() method
- Add Minutes to LocalTime using plusMinutes() method
- Add Seconds to LocalTime using plusSeconds() method
- Add Nano of Seconds to LocalTime using plusNanos() method
- Subtract Hours from LocalTime using minusHours() method
- Subtract Minutes from LocalTime using minusMinutes() method
- Subtract Seconds from LocalTime using minusSeconds() method
- Subtract Nano of Seconds from LocalTime using minusNanos() method
- Compare two LocalTime objects for Ordering
- Compare two LocalTime objects for Equality
- Check if LocalTime is before another LocalTime
- Check if LocalTime is after another LocalTime
Get Current System Time using LocalTime.now() method
In Java we can create a new LocalTime object of current time from the system clock in the default time zone using the LocalTime.now() static method as below Java code.
LocalTimeExample1.java
import java.time.LocalTime;
public class LocalTimeExample1 {
public static void main(String... args) {
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
}
}
21:40:06.509415100
Or create LocalTime object of current time in a specified time zone as below.
LocalTimeExample2.java
import java.time.LocalTime;
import java.time.ZoneId;
public class LocalTimeExample2 {
public static void main(String... args) {
ZoneId zoneId = ZoneId.of("Asia/Ho_Chi_Minh");
LocalTime localTime = LocalTime.now(zoneId);
System.out.println("Time at Asia/Ho_Chi_Minh: " + localTime);
}
}
Time at Asia/Ho_Chi_Minh: 21:40:29.026879
In the following Java program we show how to create LocalTime object which a specified Clock, for example UTC time zone.
LocalTimeExample3.java
import java.time.Clock;
import java.time.LocalTime;
public class LocalTimeExample3 {
public static void main(String... args) {
Clock clock = Clock.systemUTC();
LocalTime localTime = LocalTime.now(clock);
System.out.println("UTC LocalTime: " + localTime);
}
}
UTC LocalTime: 14:40:55.900365
Create LocalTime object from Hour Minute Second and Nano of Second
In Java we can instantiate a new LocalTime object from given hour, minute, second and nano of second using the LocalTime.of() static method as the following example Java code.
LocalTimeExample4.java
import java.time.LocalTime;
public class LocalTimeExample4 {
public static void main(String... args) {
int hour = 8;
int minute = 30;
int second = 20;
int nanoOfSecond = 999;
LocalTime localTime1 = LocalTime.of(hour, minute);
LocalTime localTime2 = LocalTime.of(hour, minute, second);
LocalTime localTime3 = LocalTime.of(hour, minute, second, nanoOfSecond);
System.out.println(localTime1);
System.out.println(localTime2);
System.out.println(localTime3);
}
}
08:30
08:30:20
08:30:20.000000999
Create LocalTime object from Second of Day
The LocalTime class provides the LocalTime.ofSecondOfDay() static method which can be used to create LocalTime object from a specified seconds of day as Java code below.
LocalTimeExample5.java
import java.time.LocalTime;
public class LocalTimeExample5 {
public static void main(String... args) {
long secondOfDay = 60030;
LocalTime localTime = LocalTime.ofSecondOfDay(secondOfDay);
System.out.println("Second of Day: " + secondOfDay);
System.out.println("LocalTime: " + localTime);
}
}
Second of Day: 60030
LocalTime: 16:40:30
Create LocalTime object from Nano of Day
The LocalTime class provides the LocalTime.ofNanoOfDay() static method which can be used to create LocalTime object from a specified nanos of day as Java code below.
LocalTimeExample6.java
import java.time.LocalTime;
public class LocalTimeExample6 {
public static void main(String... args) {
long nanoOfDay = 999999999999L;
LocalTime localTime = LocalTime.ofNanoOfDay(nanoOfDay);
System.out.println("Nano of Day: " + nanoOfDay);
System.out.println("LocalTime: " + localTime);
}
}
Nano of Day: 999999999999
LocalTime: 00:16:39.999999999
Convert String to LocalTime
In Java we can use the LocalTime.parse() static method to parse a time String into LocalTime object. For example, in the Java code below we parse different String of hour minute second and nano of second to LocalTime object.
LocalTimeExample7.java
import java.time.LocalTime;
public class LocalTimeExample7 {
public static void main(String... args) {
LocalTime localTime1 = LocalTime.parse("08:30");
LocalTime localTime2 = LocalTime.parse("08:30:20");
LocalTime localTime3 = LocalTime.parse("08:30:20.999999999");
System.out.println(localTime1);
System.out.println(localTime2);
System.out.println(localTime3);
}
}
08:30
08:30:20
08:30:20.999999999
With the LocalTime.parse() method we also can use with the DateTimeFormatter class to parse String in custom time patterns as below.
LocalTimeExample8.java
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class LocalTimeExample8 {
public static void main(String... args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("H-mm-ss");
LocalTime localTime1 = LocalTime.parse("01-30-40", dateTimeFormatter);
LocalTime localTime2 = LocalTime.parse("13-20-30", dateTimeFormatter);
System.out.println(localTime1);
System.out.println(localTime2);
}
}
01:30:40
13:20:30
Convert LocalTime to String
In Java we can use the LocalTime.format() method with a specified DateTimeFormatter to format a LocalTime object to a String as below.
LocalTimeExample9.java
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class LocalTimeExample9 {
public static void main(String... args) {
int hour = 20;
int minute = 30;
int second = 20;
LocalTime localTime = LocalTime.of(hour, minute, second);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
String localTimeString = localTime.format(dateTimeFormatter);
System.out.println(localTimeString);
}
}
08:30:20 pm
Or simply use the toString() method with a given LocalTime object as following Java code.
LocalTimeExample10.java
import java.time.LocalTime;
public class LocalTimeExample10 {
public static void main(String... args) {
int hour = 20;
int minute = 30;
int second = 20;
LocalTime localTime = LocalTime.of(hour, minute, second);
String localTimeString = localTime.toString();
System.out.println(localTimeString);
}
}
20:30:20
Convert LocalTime to Second of Day
With a given LocalTime object we can use the toSecondOfDay() method to convert LocalTime to second of day value as below.
LocalTimeExample11.java
import java.time.LocalTime;
public class LocalTimeExample11 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(20, 30, 50);
int secondOfDay = localTime.toSecondOfDay();
System.out.println("LocalTime: " + localTime);
System.out.println("Second of Day: " + secondOfDay);
}
}
LocalTime: 20:30:50
Second of Day: 73850
Convert LocalTime to Nano of Day
With a given LocalTime object we can use the toNanoOfDay() method to convert LocalTime to nano second of day value as below.
LocalTimeExample12.java
import java.time.LocalTime;
public class LocalTimeExample12 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(20, 30, 50);
long nanoOfDay = localTime.toNanoOfDay();
System.out.println("LocalTime: " + localTime);
System.out.println("Nano of Day: " + nanoOfDay);
}
}
LocalTime: 20:30:50
Nano of Day: 73850000000000
Convert LocalTime to Epoch Second
With the LocalTime.toEpochSecond() method we can convert a LocalTime object to the number of seconds since the epoch of 1970-01-01T00:00:00Z as below Java code.
LocalTimeExample13.java
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class LocalTimeExample13 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(20, 30, 50);
LocalDate localDate = LocalDate.now();
ZoneOffset zoneOffset = ZoneId.systemDefault().getRules().getOffset(LocalDateTime.now());
long epochSecond = localTime.toEpochSecond(localDate, zoneOffset);
System.out.println("LocalTime: " + localTime);
System.out.println("Epoch Second: " + epochSecond);
}
}
LocalTime: 20:30:50
Epoch Second: 1650375050
Convert LocalTime to LocalDateTime
To convert LocalTime to LocalDateTime in Java we need to combine LocalTime object with a LocalDate object using the LocalTime.atDate() method as below Java code.
LocalTimeExample14.java
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class LocalTimeExample14 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(20, 30, 50);
LocalDate today = LocalDate.now();
LocalDateTime localDateTime = localTime.atDate(today);
System.out.println("LocalTime: " + localTime);
System.out.println("LocalDate: " + today);
System.out.println("LocalDateTime: " + localDateTime);
}
}
LocalTime: 20:30:50
LocalDate: 2022-04-19
LocalDateTime: 2022-04-19T20:30:50
Convert LocalTime to OffsetTime
To convert LocalTime to OffsetTime in Java we can combine a LocalTime object with a ZoneOffset object using the LocalTime.atOffset() method as the example Java program below.
LocalTimeExample15.java
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class LocalTimeExample15 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(20, 30, 50);
ZoneOffset zoneOffset = ZoneId.systemDefault().getRules().getOffset(LocalDateTime.now());
OffsetTime offsetTime = localTime.atOffset(zoneOffset);
System.out.println("LocalTime: " + localTime);
System.out.println("ZoneOffset: " + zoneOffset);
System.out.println("OffsetTime: " + offsetTime);
}
}
LocalTime: 20:30:50
ZoneOffset: +07:00
OffsetTime: 20:30:50+07:00
Convert Instant to LocalTime
To convert Instant to LocalTime in Java we can use the LocalTime.ofInstant() static method as the following Java program.
LocalTimeExample16.java
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
public class LocalTimeExample16 {
public static void main(String... args) {
Instant instant = Instant.now();
ZoneId zoneId = ZoneId.of("Australia/Sydney");
LocalTime localTime = LocalTime.ofInstant(instant, zoneId);
System.out.println(localTime);
}
}
01:54:00.825990
Get Minimum and Maximum of LocalTime
The LocalTime class provide MIN and MAX constants for minimum and maximum value of LocalTime object as we can learn how to use it below.
LocalTimeExample17.java
import java.time.LocalTime;
public class LocalTimeExample17 {
public static void main(String... args) {
LocalTime min = LocalTime.MIN;
LocalTime max = LocalTime.MAX;
System.out.println("Minimum LocalTime: " + min);
System.out.println("Maximum LocalTime: " + max);
}
}
Minimum LocalTime: 00:00
Maximum LocalTime: 23:59:59.999999999
Get Midnight and Noon LocalTime
The LocalTime class provide MIDNIGHT and NOON constants to get midnight and noon LocalTime object as we can learn how to use it below.
LocalTimeExample18.java
import java.time.LocalTime;
public class LocalTimeExample18 {
public static void main(String... args) {
LocalTime midnight = LocalTime.MIDNIGHT;
LocalTime noon = LocalTime.NOON;
System.out.println("Midnight LocalTime: " + midnight);
System.out.println("Noon LocalTime: " + noon);
}
}
Midnight LocalTime: 00:00
Noon LocalTime: 12:00
Get LocalTime Field Values
In the following Java program we learn how to get field values of a given LocalTime object such as hour, minute, seconds or nanos value.
LocalTimeExample19.java
import java.time.LocalTime;
public class LocalTimeExample19 {
public static void main(String... args) {
LocalTime localTime = LocalTime.now();
int hours = localTime.getHour();
int minutes = localTime.getMinute();
int seconds = localTime.getSecond();
int nanos = localTime.getNano();
System.out.println("Hours: " + hours);
System.out.println("Minutes: " + minutes);
System.out.println("Seconds: " + seconds);
System.out.println("Nanos: " + nanos);
}
}
Hours: 23
Minutes: 9
Seconds: 28
Nanos: 201102300
Set Hour to LocalTime using withHour() method
Using the LocalTime.withHour() method we can create a new LocalTime object with updated hour value as the Java code below.
LocalTimeExample20.java
import java.time.LocalTime;
public class LocalTimeExample20 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(1, 1, 1);
// set hour to 5
localTime = localTime.withHour(5);
System.out.println(localTime);
}
}
05:01:01
Set Minute to LocalTime using withMinute() method
Using the LocalTime.withMinute() method we can create a new LocalTime object with updated minute value as the Java code below.
LocalTimeExample21.java
import java.time.LocalTime;
public class LocalTimeExample21 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(1, 1, 1);
// set minute to 7
localTime = localTime.withMinute(7);
System.out.println(localTime);
}
}
01:07:01
Set Second to LocalTime using withSecond() method
Using the LocalTime.withSecond() method we can create a new LocalTime object with updated second value as the Java code below.
LocalTimeExample22.java
import java.time.LocalTime;
public class LocalTimeExample22 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(1, 1, 1);
// set second to 30
localTime = localTime.withSecond(30);
System.out.println(localTime);
}
}
01:01:30
Set Nano of Second to LocalTime using withNano() method
Using the LocalTime.withNano() method we can create a new LocalTime object with updated nano value as the Java code below.
LocalTimeExample23.java
import java.time.LocalTime;
public class LocalTimeExample23 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(1, 1, 1, 1);
// set nano to 9999
localTime = localTime.withNano(9999);
System.out.println(localTime);
}
}
01:01:01.000009999
Add Hours to LocalTime using plusHours() method
Using the LocalTime.plusHours() method we can create a new LocalTime object with number of hours be added as below Java code.
LocalTimeExample24.java
import java.time.LocalTime;
public class LocalTimeExample24 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(1, 1, 1, 1);
// plus 7 hours
localTime = localTime.plusHours(7);
System.out.println(localTime);
}
}
08:01:01.000000001
Add Minutes to LocalTime using plusMinutes() method
Using the LocalTime.plusMinutes() method we can create a new LocalTime object with number of minutes be added as below Java code.
LocalTimeExample25.java
import java.time.LocalTime;
public class LocalTimeExample25 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(1, 1, 1, 1);
// plus 20 minutes
localTime = localTime.plusMinutes(20);
System.out.println(localTime);
}
}
01:21:01.000000001
Add Seconds to LocalTime using plusSeconds() method
Using the LocalTime.plusSeconds() method we can create a new LocalTime object with number of seconds be added as below Java code.
LocalTimeExample26.java
import java.time.LocalTime;
public class LocalTimeExample26 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(1, 1, 1, 1);
// plus 50 seconds
localTime = localTime.plusSeconds(50);
System.out.println(localTime);
}
}
01:01:51.000000001
Add Nano of Seconds to LocalTime using plusNanos() method
Using the LocalTime.plusNanos() method we can create a new LocalTime object with number of nanos be added as below Java code.
LocalTimeExample27.java
import java.time.LocalTime;
public class LocalTimeExample27 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(1, 1, 1, 1);
// plus 998 nanos
localTime = localTime.plusNanos(998);
System.out.println(localTime);
}
}
01:01:01.000000999
Subtract Hours from LocalTime using minusHours() method
Using the LocalTime.minusHours() method we can create a new LocalTime object with number of hours be subtracted as below Java code.
LocalTimeExample28.java
import java.time.LocalTime;
public class LocalTimeExample28 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(7, 30, 40, 999);
// subtract 2 hours
localTime = localTime.minusHours(2);
System.out.println(localTime);
}
}
05:30:40.000000999
Subtract Minutes from LocalTime using minusMinutes() method
Using the LocalTime.minusMinutes() method we can create a new LocalTime object with number of minutes be subtracted as below Java code.
LocalTimeExample29.java
import java.time.LocalTime;
public class LocalTimeExample29 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(7, 30, 40, 999);
// subtract 20 minutes
localTime = localTime.minusMinutes(20);
System.out.println(localTime);
}
}
07:10:40.000000999
Subtract Seconds from LocalTime using minusSeconds() method
Using the LocalTime.minusSeconds() method we can create a new LocalTime object with number of seconds be subtracted as below Java code.
LocalTimeExample30.java
import java.time.LocalTime;
public class LocalTimeExample30 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(7, 30, 40, 999);
// subtract 20 seconds
localTime = localTime.minusSeconds(20);
System.out.println(localTime);
}
}
07:30:20.000000999
Subtract Nano of Seconds from LocalTime using minusNanos() method
Using the LocalTime.minusNanos() method we can create a new LocalTime object with number of nanos be subtracted as below Java code.
LocalTimeExample31.java
import java.time.LocalTime;
public class LocalTimeExample31 {
public static void main(String... args) {
LocalTime localTime = LocalTime.of(7, 30, 40, 999);
// subtract 222 nanos
localTime = localTime.minusNanos(222);
System.out.println(localTime);
}
}
07:30:40.000000777
Compare two LocalTime objects for Ordering
The LocalTime.compareTo() method to return value of -1, 0, 1 if a LocalTime object is before, equal to or after another specified LocalTime object it compare to. We learn how to use the LocalTime.compareTo() method in the following example Java program.
LocalTimeExample32.java
import java.time.LocalTime;
public class LocalTimeExample32 {
public static void main(String... args) {
LocalTime localTime1 = LocalTime.of(8, 30, 40, 999);
LocalTime localTime2 = LocalTime.of(8, 30, 40, 999);
LocalTime localTime3 = LocalTime.of(9, 40, 50, 999);
int compareToResult1 = localTime1.compareTo(localTime2);
int compareToResult2 = localTime1.compareTo(localTime3);
int compareToResult3 = localTime3.compareTo(localTime1);
System.out.println("localTime1: " + localTime1);
System.out.println("localTime2: " + localTime2);
System.out.println("localTime3: " + localTime3);
System.out.println("localTime1 compareTo localTime2: " + compareToResult1);
System.out.println("localTime1 compareTo localTime3: " + compareToResult2);
System.out.println("localTime3 compareTo localTime1: " + compareToResult3);
}
}
localTime1: 08:30:40.000000999
localTime2: 08:30:40.000000999
localTime3: 09:40:50.000000999
localTime1 compareTo localTime2: 0
localTime1 compareTo localTime3: -1
localTime3 compareTo localTime1: 1
Compare two LocalTime objects for Equality
In LocalTime class we can use the equals() method to compare two LocalTime objects for equality as below.
LocalTimeExample33.java
import java.time.LocalTime;
public class LocalTimeExample33 {
public static void main(String... args) {
LocalTime localTime1 = LocalTime.of(8, 30, 40, 999);
LocalTime localTime2 = LocalTime.of(8, 30, 40, 999);
LocalTime localTime3 = LocalTime.of(9, 40, 50, 999);
boolean result1 = localTime1.equals(localTime2);
boolean result2 = localTime1.equals(localTime3);
System.out.println("localTime1: " + localTime1);
System.out.println("localTime2: " + localTime2);
System.out.println("localTime3: " + localTime3);
System.out.println("localTime1 is equals to localTime2: " + result1);
System.out.println("localTime1 is equals to localTime3: " + result2);
}
}
localTime1: 08:30:40.000000999
localTime2: 08:30:40.000000999
localTime3: 09:40:50.000000999
localTime1 is equals to localTime2: true
localTime1 is equals to localTime3: false
Check if LocalTime is before another LocalTime
The LocalTime class provides the isBefore() method which can be used to check if a LocalTime object is before another LocalTime object or not. We learn how to use the LocalTime.isBefore() method in the following example Java code.
LocalTimeExample34.java
import java.time.LocalTime;
public class LocalTimeExample34 {
public static void main(String... args) {
LocalTime localTime1 = LocalTime.of(8, 30, 40, 999);
LocalTime localTime2 = LocalTime.of(8, 30, 40, 999);
LocalTime localTime3 = LocalTime.of(9, 40, 50, 999);
boolean result1 = localTime1.isBefore(localTime2);
boolean result2 = localTime1.isBefore(localTime3);
System.out.println("localTime1: " + localTime1);
System.out.println("localTime2: " + localTime2);
System.out.println("localTime3: " + localTime3);
System.out.println("localTime1 is before localTime2: " + result1);
System.out.println("localTime1 is before localTime3: " + result2);
}
}
localTime1: 08:30:40.000000999
localTime2: 08:30:40.000000999
localTime3: 09:40:50.000000999
localTime1 is before localTime2: false
localTime1 is before localTime3: true
Check if LocalTime is after another LocalTime
The LocalTime class provides the isAfter() method which can be used to check if a LocalTime object is after another LocalTime object or not. We learn how to use the LocalTime.isAfter() method in the following example Java code.
LocalTimeExample35.java
import java.time.LocalTime;
public class LocalTimeExample35 {
public static void main(String... args) {
LocalTime localTime1 = LocalTime.of(8, 30, 40, 999);
LocalTime localTime2 = LocalTime.of(8, 30, 40, 999);
LocalTime localTime3 = LocalTime.of(7, 40, 50, 999);
boolean result1 = localTime1.isAfter(localTime2);
boolean result2 = localTime1.isAfter(localTime3);
System.out.println("localTime1: " + localTime1);
System.out.println("localTime2: " + localTime2);
System.out.println("localTime3: " + localTime3);
System.out.println("localTime1 is after localTime2: " + result1);
System.out.println("localTime1 is after localTime3: " + result2);
}
}
localTime1: 08:30:40.000000999
localTime2: 08:30:40.000000999
localTime3: 07:40:50.000000999
localTime1 is after localTime2: false
localTime1 is after localTime3: true
Happy Coding 😊