当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LocalTime equals()用法及代码示例


LocalTime类的equals()方法用于将此LocalTime对象与作为参数传递的LocalTime进行比较,并在对象相等时返回true,否则返回false。两个LocalTime之间的比较是基于一天中当地时间的时间轴位置。该方法要返回的值确定如下:

  • 如果两个LocalTime相等,则返回true
  • 如果两个实例不相等,则返回false

用法:

public int equals(Object obj)

参数:此方法接受单个参数对象,该对象是要比较的另一个LocalTime,并且不应为null。


返回值:此方法返回int值,并且返回值的确定如下:

  • 如果两个LocalTime相等,则返回true
  • 如果两个实例不相等,则返回false

以下示例程序旨在说明equals()方法:

示例1:当两个对象相等时。

// Java program to demonstrate 
// LocalTime.equals() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalTime Objects 
        LocalTime time1 = LocalTime.parse("13:08:00"); 
        LocalTime time2 = LocalTime.parse("13:08:00"); 
  
        // print  Values 
        System.out.println("LocalTime1: "
                           + time1); 
        System.out.println("LocalTime2: "
                           + time2); 
  
        // compare both LocalTimes 
        boolean value = time1.equals(time2); 
  
        // print results 
        System.out.println("Are both LocalTimes are equal: "
                           + value); 
    } 
}
输出:
LocalTime1: 13:08
LocalTime2: 13:08
Are both LocalTimes are equal: true

示例2:当两个对象不相等时。

// Java program to demonstrate 
// LocalTime.equals() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalTime Objects 
        LocalTime time1 = LocalTime.parse("03:18:23"); 
        LocalTime time2 = LocalTime.parse("13:08:20"); 
  
        // print values 
        System.out.println("LocalTime1: "
                           + time1); 
        System.out.println("LocalTime2: "
                           + time2); 
  
        // compare both LocalTimes 
        boolean value = time1.equals(time2); 
  
        // print results 
        System.out.println("Are both LocalTimes are equal: "
                           + value); 
    } 
}
输出:
LocalTime1: 03:18:23
LocalTime2: 13:08:20
Are both LocalTimes are equal: false

参考: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#equals(java.lang.Object)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 LocalTime equals() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。