LocalTime equals() method in Java


The equality of two LocalTime objects can be determined using the equals() method in the LocalTime class in Java. This method requires a single parameter i.e. the LocalTime object to be compared. Also it returns true if both the LocalTime objects are equal and false otherwise.

A program that demonstrates this is given as follows

Example

 Live Demo

import java.time.*;
public class Main {
   public static void main(String[] args) {
      LocalTime lt1 = LocalTime.parse("23:15:30");
      LocalTime lt2 = LocalTime.parse("23:15:30");
      System.out.println("The LocalTime lt1 is: " + lt1);
      System.out.println("The LocalTime lt2 is: " + lt2);
      boolean flag = lt1.equals(lt2);
      if(flag)
         System.out.println("
Both LocalTime objects are equal");       else          System.out.println("
Both LocalTime objects are not equal");    } }

output

The LocalTime lt1 is: 23:15:30
The LocalTime lt2 is: 23:15:30
Both LocalTime objects are equal

Now let us understand the above program.

The two LocalTime objects are displayed. It is checked if the LocalTime objects are equal using the equals() method. The returned value of the method is displayed using an if statement. A code snippet that demonstrates this is as follows:

LocalTime lt1 = LocalTime.parse("23:15:30");
LocalTime lt2 = LocalTime.parse("23:15:30");
System.out.println("The LocalTime lt1 is: " + lt1);
System.out.println("The LocalTime lt2 is: " + lt2);
boolean flag = lt1.equals(lt2);
if(flag)
   System.out.println("
Both LocalTime objects are equal"); else    System.out.println("
Both LocalTime objects are not equal");

Updated on: 30-Jul-2019

302 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements