當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。