📜  Java中的 LocalDateTime equals() 方法及示例

📅  最后修改于: 2022-05-13 01:54:28.320000             🧑  作者: Mango

Java中的 LocalDateTime equals() 方法及示例

Java中LocalDateTime 类equals()方法检查此日期时间是否等于另一个日期时间。这个其他日期时间作为参数传递。此方法返回一个布尔值,显示相同。

句法:

public boolean equals(Object date2)

参数:此方法接受一个参数date2 ,它检查此日期时间是否等于另一个日期时间。

返回:如果这等于另一个日期时间,则该函数返回一个布尔值

下面的程序说明了 LocalDateTime.equals() 方法:

方案一:

// 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

方案二:

// 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: Java Java)