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

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

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

Java中 OffsetDateTime 类的equals()方法检查此日期时间是否等于另一个日期时间。
句法:

public boolean equals(Object obj)

参数:此方法接受一个参数other ,用于检查此日期时间是否等于另一个日期时间。
返回值:如果这等于另一个日期时间,则返回true,否则返回false。
下面的程序说明了equals()方法:
程序 1:

Java
// Java program to demonstrate the equals() method
 
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Parses the date1
        OffsetDateTime date1 = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
 
        // Parses the date2
        OffsetDateTime date2 = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
 
        // Prints both dates
        System.out.println("Date1: " + date1);
        System.out.println("Date2: " + date2);
 
        // Compare both
        System.out.println("On comparing we get " + date1.equals(date2));
    }
}


Java
// Java program to demonstrate the equals() method
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Parses the date1
        OffsetDateTime date1 = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
 
        // Parses the date2
        OffsetDateTime date2 = OffsetDateTime.parse("2015-12-12T13:30:30+05:00");
 
        // Prints both dates
        System.out.println("Date1: " + date1);
        System.out.println("Date2: " + date2);
 
        // Compare both
        System.out.println("On comparing we get " + date1.equals(date2));
    }
}


输出
Date1: 2018-12-12T13:30:30+05:00
Date2: 2018-12-12T13:30:30+05:00
On comparing we get true

方案二

Java

// Java program to demonstrate the equals() method
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Parses the date1
        OffsetDateTime date1 = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
 
        // Parses the date2
        OffsetDateTime date2 = OffsetDateTime.parse("2015-12-12T13:30:30+05:00");
 
        // Prints both dates
        System.out.println("Date1: " + date1);
        System.out.println("Date2: " + date2);
 
        // Compare both
        System.out.println("On comparing we get " + date1.equals(date2));
    }
}
输出
Date1: 2018-12-12T13:30:30+05:00
Date2: 2015-12-12T13:30:30+05:00
On comparing we get false

参考:https: Java/time/OffsetDateTime.html#equals-java.lang.Object-