Java LocalTime class

Java LocalTime class

Java LocalTime class is an immutable class that represents time with a default format of hour-minute-second. It inherits Object class and implements the Comparable interface.


Java LocalTime class declaration

Let’s see the declaration of java.time.LocalTime class.

  1. public final class LocalTime extends Object
  2. implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable

Methods of Java LocalTime

Method Description
LocalDateTime atDate(LocalDate date) It is used to combine this time with a date to create a LocalDateTime.
int compareTo(LocalTime other) It is used to compare this time to another time.
String format(DateTimeFormatter formatter) It is used to format this time using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this time as an int.
LocalTime minusHours(long hoursToSubtract) It is used to return a copy of this LocalTime with the specified number of hours subtracted.
LocalTime minusMinutes(long minutesToSubtract) It is used to return a copy of this LocalTime with the specified number of minutes subtracted.
static LocalTime now() It is used to obtain the current time from the system clock in the default time-zone.
static LocalTime of(int hour, int minute, int second) It is used to obtain an instance of LocalTime from an hour, minute and second.
LocalTime plusHours(long hoursToAdd) It is used to return a copy of this LocalTime with the specified number of hours added.
LocalTime plusMinutes(long minutesToAdd) It is used to return a copy of this LocalTime with the specified number of minutes added.

 

Java LocalTime Example: now()

  1. import java.time.LocalTime;
  2. public class LocalTimeExample {
  3.   public static void main(String[] args) {
  4.     LocalTime time = LocalTime.now();
  5.     System.out.printl(time);
  6.   }
  7. }

Output:

15:19:47.459

Java LocalTime Example: of()

  1. import java.time.LocalTime;
  2. public class LocalDateExample {
  3.   public static void main(String[] args) {
  4.     LocalTime time = LocalTime.of(10,43,12);
  5.     System.out.println(time);
  6.   }
  7. }

Output:

10:43:12

Java LocalTime Example: minusHours() and minusMinutes()

  1. import java.time.LocalTime;
  2. public class LocalDateExample {
  3.   public static void main(String[] args) {
  4.     LocalTime time1 = LocalTime.of(10,43,12);
  5.     System.out.println(time1);
  6.     LocalTime time2=time1.minusHours(2);
  7.     LocalTime time3=time2.minusMinutes(34);
  8.     System.out.println(time3);
  9.   }
  10. }

Output:

10:43:12
08:09:12

Java LocalTime Example: plusHours() and plusMinutes()

  1. import java.time.LocalTime;
  2. public class LocalTimeExample {
  3.   public static void main(String[] args) {
  4.     LocalTime time1 = LocalTime.of(10,43,12);
  5.     System.out.println(time1);
  6.     LocalTime time2=time1.plusHours(4);
  7.     LocalTime time3=time2.plusMinutes(18);
  8.     System.out.println(time3);
  9.   }
  10. }

Output:

10:43:12
15:01:12

Java LocalTime Example

  1. import java.time.*;
  2. import java.time.temporal.ChronoUnit;
  3. public class LocalTimeExample {
  4.   public static void main(String… args) {
  5.     ZoneId zone1 = ZoneId.of(“Asia/Kolkata”);
  6.     ZoneId zone2 = ZoneId.of(“Asia/Tokyo”);
  7.     LocalTime time1 = LocalTime.now(zone1);
  8.     System.out.println(“India Time Zone: “+time1);
  9.     LocalTime time2 = LocalTime.now(zone2);
  10.     System.out.println(“Japan Time Zone: “+time2);
  11.     long hours = ChronoUnit.HOURS.between(time1, time2);
  12.     System.out.println(“Hours between two Time Zone: “+hours);
  13.     long minutes = ChronoUnit.MINUTES.between(time1, time2);
  14.     System.out.println(“Minutes between two time zone: “+minutes);
  15.   }
  16. }

Output:

India Time Zone: 14:56:43.087
Japan Time Zone: 18:26:43.103
Hours between two Time Zone: 3
Minutes between two time zone: 210
Next TopicJava LocalDateTime
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java LocalTime class Java LocalTime class is an immutable class that represents time with a default format of hour-minute-second. It inherits Object class and implements the Comparable interface. Java LocalTime class declaration Let’s see the declaration of java.time.LocalTime class. public final class LocalTime extends Object implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable Methods of Java LocalTime Method Description LocalDateTime atDate(LocalDate date) It is used to combine […]

Tags:

....
...