Learnitweb

Java LocalTime class with examples

1. Brief Description

  • LocalTime class is available in java.time package. java.time package was added in Java 8.
  • This class represents a time without a time-zone in the ISO-8601 calendar system, such as 11: 30: 40. Time is represented to nanosecond precision.
  • This is a value-based, immutable and thread-safe class.

2. Create instance of LocalTime

We’ll discuss few common used methods to create instance of LocalTime.

2.1 LocalTime representing current time

LocalTime now = LocalTime.now();

2.2 LocalDateTime from hour, minute and second

Syntax: LocalTime time = LocalTime.of(int hour, int minute, int second);
example: LocalTime time = LocalTime.of(10, 15, 30);

There are other variations of method of to get instance of LocalTime:

static LocalTime of(int hour, int minute)
static LocalTime of(int hour, int minute, int second)
static LocalTime of(int hour, int minute, int second, int nanoOfSecond)

2.3 LocalTime instance from a String

static LocalTime parse(CharSequence text)
Example: LocalTime time = LocalTime.parse("12:15:30");

2.4 Example of creating LocalTime instance

import java.time.LocalTime;

public class LocalTimeExample {
	public static void main(String[] args) {
		//LocalTime representing current time
		LocalTime now = LocalTime.now();
		System.out.println("Current time : " + now);
		
		//LocalTime from hour, minute, second
		LocalTime time = LocalTime.of(10, 15, 30);
		System.out.println("LocalTime from hour, minute ans second: " + time);
		
		//LocalTime from String
		LocalTime timeFromString = LocalTime.parse("12:15:30");
		System.out.println("LocalTime from String: " + timeFromString);
	}
}

Output

Current time : 00:09:13.138
LocalTime from hour, minute ans second: 10:15:30
LocalTime from String: 12:15:30

3. Get information from LocalTime instance

Following are the common methods to get information about hour, minute, second and nano second of the day:

int getHour()Returns the hour-of-day field.
int getMinute()Returns the minute-of-hour field.
int getNano()Returns the nano-of-second field.
int getSecond()Returns the second-of-minute field.
import java.time.LocalTime;

public class LocalTimeExample {
	public static void main(String[] args) {
		//LocalTime representing current time
		LocalTime now = LocalTime.now();
		System.out.println("Current time : " + now);
		
		System.out.println("Hour: " + now.getHour());
		System.out.println("Minute: " + now.getMinute());
		System.out.println("Seconds: " + now.getSecond());
		System.out.println("Nano seconds: " + now.getNano());
	}
}

Output

Current time : 00:37:35.340
Hour: 0
Minute: 37
Seconds: 35
Nano seconds: 340000000

4. LocalTime manipulation – plus and minus time

Following are the important methods to manipulate LocalTime:

LocalTime minusHours(long hoursToSubtract)To subtract specified number of hours and return copy of LocalTime
LocalTime minusMinutes(long minutesToSubtract)To subtract specified number of minutes and return copy of LocalTime
LocalTime minusNanos(long nanosToSubtract)To subtract specified number of nano seconds and return copy of LocalTime
LocalTime minusSeconds(long secondsToSubtract)To subtract specified number of seconds and return copy of LocalTime
LocalTime plusHours(long hoursToAdd)To add specified number of hours and return a copy of LocalTime
LocalTime plusMinutes(long minutesToAdd)To add specified number of minutes and return a copy of LocalTime
LocalTime plusNanos(long nanosToAdd)To add specified number of nano seconds and return a copy of LocalTime
LocalTime plusSeconds(long secondstoAdd)To add specified number of seconds and return a copy of LocalTime
import java.time.LocalTime;

public class LocalTimeExample {
	public static void main(String[] args) {
		//LocalTime representing current time
		LocalTime now = LocalTime.now();
		System.out.println("Current time : " + now);
		
		System.out.println("Minus 10 hours: " + now.minusHours(10));
		System.out.println("Minus 15 minutes: " + now.minusMinutes(15));
		System.out.println("Minus 20 seconds: " + now.minusSeconds(20));
		System.out.println("Minus 30 nano seconds: " + now.minusNanos(30));
		
		System.out.println("Add 10 hours: " + now.plusHours(10));
		System.out.println("Ass 15 minutes: " + now.plusMinutes(15));
		System.out.println("Add 20 seconds: " + now.plusSeconds(20));
		System.out.println("Add 30 nano seconds: " + now.plusNanos(30));
	}
}

Output

Current time : 01:07:15.311
Minus 10 hours: 15:07:15.311
Minus 15 minutes: 00:52:15.311
Minus 20 seconds: 01:06:55.311
Minus 30 nano seconds: 01:07:15.310999970
Add 10 hours: 11:07:15.311
Ass 15 minutes: 01:22:15.311
Add 20 seconds: 01:07:35.311
Add 30 nano seconds: 01:07:15.311000030

5. Java LocalTime format

You can use various formatters to format the time. DateTimeFormatter class can be used to format the time.

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class LocalTimeExample {
	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)); 
	}
}

Output

01:21:49.642
01:21:49
01:21:49 AM

6. LocalTime until method

long until(Temporal endExclusive, TemporalUnit unit)This method calculates the amount of time until specified time in terms of specified unit.
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class LocalTimeExample {
	public static void main(String[] args) {
		LocalTime now = LocalTime.now();
		System.out.println("current time: " + now);
		
        LocalTime time = LocalTime.parse("10:15:30");
        System.out.println("Time until: " + time);
        
        System.out.println("hours: " +  now.until(time, ChronoUnit.HOURS));
        System.out.println("minutes: " +  now.until(time, ChronoUnit.MINUTES));
        System.out.println("seconds: " +  now.until(time, ChronoUnit.SECONDS));
	}
}

Output

current time: 01:31:23.334
Time until: 10:15:30
hours: 8
minutes: 524
seconds: 31446

7. Java LocalTime compare

Following are the important methods to compare time:

int compareTo(LocalTime other)Compares one time to another
boolean isAfter(LocalTime other)Checks if specified time is after the specified time
boolean isBefore(LocalTime other)Checks if specified time is before the specified time
import java.time.LocalTime;

public class LocalTimeExample {
	public static void main(String[] args) {

		LocalTime time1 = LocalTime.of(10, 15, 30);
		LocalTime time2 = LocalTime.of(4, 8, 45);
		LocalTime time3 = LocalTime.of(11, 45, 55);

		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");
		}
	}
}

Output

time1 and time2 are not equal
time2 comes before time3
time3 comes after time1

8. Java LocalTime truncate

LocalTime truncatedTo(TemporalUnit unit)This method is used to truncate time according to the specified unit and return a copy of LocalTime.
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class LocalTimeExample {
	public static void main(String[] args) {
		
		// current time
		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));
	}
}

Output

01:45:06.761
00:00
01:00
01:45
01:45:06
01:45:06.761