📌  相关文章
📜  Java中的 LocalDateTime isEqual() 方法及示例

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

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

Java中LocalDateTime 类isEqual()方法用于检查该日期是否等于指定的日期时间。

句法:

public boolean isEqual(ChronoLocalDateTime other)

参数:此方法接受参数other指定要比较的其他日期时间。它不应该为空。

返回:如果此日期时间等于指定的日期时间,则函数返回布尔值

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

方案一:

// Program to illustrate the isEqual() 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("After comparison: "
                           + dt2.isEqual(dt1));
    }
}
输出:
Date 1: 2018-11-03T12:45:30
Date 2: 2018-11-03T12:45:30
After comparison: true

方案二:

// Program to illustrate the isEqual() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Parses the date
        LocalDateTime dt1
            = LocalDateTime
                  .parse("2010-12-05T12:50:30");
  
        // Prints the date
        System.out.println("Date 1: " + dt1);
  
        // Parses the date
        LocalDateTime dt2
            = LocalDateTime
                  .parse("2012-05-10T12:50:30");
  
        // Prints the date
        System.out.println("Date 2: " + dt2);
  
        // Compares the date
        System.out.println("After comparison: "
                           + dt2.isEqual(dt1));
    }
}
输出:
Date 1: 2010-12-05T12:50:30
Date 2: 2012-05-10T12:50:30
After comparison: false

参考: https: Java Java.time.chrono.ChronoLocalDateTime)