January 5, 2023

LocalTime in Java With Examples

The java.time.LocalTime class is part of new date and time API added in Java 8 that represents a time in the ISO-8601 calendar system, such as 10:12:36. LocalTime class does not store or represent a date or time-zone. Instead, it is a description of the local time as seen on a wall clock.

LocalTime class is immutable thus thread-safe. Since it is marked as final so can't be extended.

In this post we’ll see some examples demonstrating usage of Java LocalTime class.

Creating instances of LocalTime

LocalTime class doesn't have any public constructors to obtain an instance, you will use a factory method to get an instance.

1. Using now() method you can obtain the current time from the system clock in the default time-zone.

LocalTime currentTime = LocalTime.now();
System.out.println(currentTime); //19:08:04.782387100

2. To obtain an instance of LocalTime by passing hour, minute, second using of() method.

LocalTime currentTime = LocalTime.of(18, 22, 32);
System.out.println(currentTime); //18:22:32

Note that of() method is overloaded so that you can pass all the four arguments (hour, minute, second, nano second) or only three (hour, minute, second) or only two (hour, minute)

  • of(int hour, int minute)
  • of(int hour, int minute, int second)
  • of(int hour, int minute, int second, int nanoOfSecond)

Local Time for specific time-zone

You can also obtain the current time from the system clock in the specified time-zone by passing the zone id.

ZoneId zone1 = ZoneId.of("America/Los_Angeles");
ZoneId zone2 = ZoneId.of("Asia/Kolkata");
LocalTime time1 = LocalTime.now(zone1);
LocalTime time2 = LocalTime.now(zone2);

System.out.println(time1); //06:52:20.179414600
System.out.println(time2); //19:22:20.222512

Getting hour, minute, second values from LocalTime

Using getHour(), getMinute(), getSecond() and getNano() methods of the LocalTime class you can get hour, minute, second and nano second field respectively.

public class FormatDate {
  public static void main(String[] args) {
    LocalTime time = LocalTime.of(18, 22, 32);
    System.out.println("Hour- " + time.getHour());
    System.out.println("Minute- " + time.getMinute());
    System.out.println("Second- " + time.getSecond());
  }
}
Output
Hour- 18
Minute- 22
Second- 32

Formatting LocalTime

Using DateTimeFormatter you can specify the pattern for formatting LocalTime.

public class FormatDate {
  public static void main(String[] args) {
    LocalTime time = LocalTime.of(18, 22, 32);
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("hh:mm:ss a");
    System.out.println("Time- "+time.format(dtf));
    
    dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
    System.out.println("Time- "+time.format(dtf));
  }
}

Time calculations using LocalTime

There are methods to add or subtract hours, minutes, seconds, nano seconds from a LocalTime.

  • minusHours(long hoursToSubtract)- Returns a copy of this LocalTime with the specified number of hours subtracted.
  • minusMinutes(long minutesToSubtract)- Returns a copy of this LocalTime with the specified number of minutes subtracted.
  • minusSeconds(long secondsToSubtract)- Returns a copy of this LocalTime with the specified number of seconds subtracted.
  • minusNanos(long nanosToSubtract)- Returns a copy of this LocalTime with the specified number of nanoseconds subtracted.
  • plusHours(long hoursToAdd)- Returns a copy of this LocalTime with the specified number of hours added.
  • plusMinutes(long minutesToAdd)- Returns a copy of this LocalTime with the specified number of minutes added.
  • plusSeconds(long secondstoAdd)- Returns a copy of this LocalTime with the specified number of seconds added.
  • plusNanos(long nanosToAdd)- Returns a copy of this LocalTime with the specified number of nanoseconds added.
public class FormatDate {
  public static void main(String[] args) {
    LocalTime time = LocalTime.of(20, 25, 45, 534);
    System.out.println("Time- " + time);
    
    System.out.println("Hour after subtraction- " + time.minusHours(2));
    System.out.println("Minute after subtraction- " + time.minusMinutes(10));
    System.out.println("Second after subtraction- " + time.minusSeconds(20));
    System.out.println("NanoSecond after subtraction- " + time.minusNanos(100));
    
    System.out.println("Hour after addition- " + time.plusHours(1));
    System.out.println("Minute after addition- " + time.plusMinutes(15));
    System.out.println("Second after addition- " + time.plusSeconds(25));
    System.out.println("NanoSecond after addition- " + time.plusNanos(100));		
  }
}
Output
Time- 20:25:45.000000534
Hour after subtraction- 18:25:45.000000534
Minute after subtraction- 20:15:45.000000534
Second after subtraction- 20:25:25.000000534
NanoSecond after subtraction- 20:25:45.000000434
Hour after addition- 21:25:45.000000534
Minute after addition- 20:40:45.000000534
Second after addition- 20:26:10.000000534
NanoSecond after addition- 20:25:45.000000634

Comparing LocalTimes in Java

For comparing two LocalTime instances there are the following methods-

  • compareTo(LocalTime other)- Compares this time to another time. Returns a negative value if less than the passed LocalTime, positive if greater.
  • isAfter(LocalTime other)- Checks if this time is after the specified time.
  • isBefore(LocalTime other)- Checks if this time is before the specified time.
public class FormatDate {
  public static void main(String[] args) {
    LocalTime time1 = LocalTime.of(20, 25, 45);
    LocalTime time2 = LocalTime.of(22, 18, 40);
    
    System.out.println(time1.compareTo(time2));
    System.out.println(time2.compareTo(time1));
    
    System.out.println(time1.isAfter(time2));
    System.out.println(time1.isBefore(time2));
  }
}
Output
-1
1
false
true

Converting String to LocalTime

Check this post for String to LocalTime conversion- Convert String to Date in Java

Converting LocalTime to String

Check this post for LocalTime to String conversion- Convert Date to String in Java

That's all for the topic LocalTime in Java With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment