可以使用equals()
Java中LocalTime类中的方法确定两个LocalTime对象的相等性。此方法需要单个参数,即要比较的LocalTime对象。如果两个LocalTime对象相等,则返回true,否则返回false。
演示此程序如下
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("\nBoth LocalTime objects are equal"); else System.out.println("\nBoth LocalTime objects are not equal"); } }
The LocalTime lt1 is: 23:15:30 The LocalTime lt2 is: 23:15:30 Both LocalTime objects are equal
现在让我们了解上面的程序。
显示两个LocalTime对象。使用equals()
方法检查LocalTime对象是否相等。使用if语句显示方法的返回值。演示此代码段如下所示:
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("\nBoth LocalTime objects are equal"); else System.out.println("\nBoth LocalTime objects are not equal");