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


Java LocalDateTime equals()用法及代碼示例


Java中的LocalDateTime類的equals()方法檢查此日期時間是否等於另一個日期時間。該另一個日期時間作為參數傳遞。此方法返回一個顯示相同值的布爾值。

用法:

public boolean equals(Object date2)

參數:此方法接受參數date2,該參數檢查此日期時間是否等於另一個日期時間。


返回值:如果此值等於另一個日期時間,則該函數返回一個布爾值。

以下示例程序旨在說明LocalDateTime.equals()方法:

示例1:

// Java program to illustrate the equals() method 
  
import java.util.*; 
import java.time.*; 
  
public class GfG { 
    public static void main(String[] args) 
    { 
        // Parses the date 
        LocalDateTime dt1 
            = LocalDateTime 
                  .parse("2018-11-03T12:45:30"); 
  
        // Prints the date 
        System.out.println("Date 1: " + dt1); 
  
        // Parses the date 
        LocalDateTime dt2 
            = LocalDateTime 
                  .parse("2018-11-03T12:45:30"); 
  
        // Prints the date 
        System.out.println("Date 2: " + dt2); 
  
        // Compares the date 
        System.out.println("Is Date 1 "
                           + "equal to Date 2: "
                           + dt2.equals(dt1)); 
    } 
}
輸出:
Date 1: 2018-11-03T12:45:30
Date 2: 2018-11-03T12:45:30
Is Date 1 equal to Date 2: true

示例2:

// Program to illustrate the equals() method 
  
import java.util.*; 
import java.time.*; 
  
public class GfG { 
    public static void main(String[] args) 
    { 
  
        // Parses the date 
        LocalDateTime dt1 
            = LocalDateTime 
                  .parse("1998-01-05T06:20:10"); 
  
        // Prints the date 
        System.out.println("Date 1: " + dt1); 
  
        // Parses the date 
        LocalDateTime dt2 
            = LocalDateTime 
                  .parse("2018-11-03T12:45:30"); 
  
        // Prints the date 
        System.out.println("Date 2: " + dt2); 
  
        // Compares the date 
        System.out.println("Is Date 1 "
                           + "equal to Date 2: "
                           + dt2.equals(dt1)); 
    } 
}
輸出:
Date 1: 1998-01-05T06:20:10
Date 2: 2018-11-03T12:45:30
Is Date 1 equal to Date 2: false

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



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 LocalDateTime equals() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。