📜  Java中的 GregorianCalendar equals() 方法

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

Java中的 GregorianCalendar equals() 方法

Java .util.GregorianCalendar.equals()方法是Java中的一个内置函数,用于检查GregorianCalendar实例与作为参数传递给函数的 Object 之间的相等性。仅当指定的 Object 是与GregorianCalendar 实例具有相同时间值(距 Epoch 的毫秒偏移量)的 GregorianCalendar 对象时,它才返回 true。

句法:

public boolean equals(Object obj)

参数:该函数接受一个强制参数obj ,该参数将与GregorianCalendar 实例进行比较。

返回值:仅当指定的 Object 是 GregorianCalendar 对象并且与实例具有相同的时间值(距 Epoch 的毫秒偏移量)时,此方法才返回 true,否则返回 false。

例子:

Input : c1 = Mon Jul 23 23:46:14 UTC 2018, c2 = Mon Jul 23 23:46:14 UTC 2018
Output : true

Input : c1 = Mon Jul 23 23:46:14 UTC 2018, c2 = Sun Jul 24 00:02:52 UTC 2022
Output : false

下面的程序说明了Java.util.GregorianCalendar.equals()函数:

方案一:

Java
// Java Program to illustrate the equals() function
// of GregorianCalendar class
 
import java.io.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        // Create a new calendar
        GregorianCalendar c1 = (GregorianCalendar)
                     GregorianCalendar.getInstance();
 
        // Display the current date and time
        System.out.println("Current Date and Time : "
                           + c1.getTime());
 
        // Create a second calendar equal to first one
        GregorianCalendar c2 =
              (GregorianCalendar)(Calendar)c1.clone();
 
        // Compare the two calendars
        System.out.println("Both calendars are equal:"
                           + c1.equals(c2));
 
        // Adding 15 months to second calendar
        c2.add(GregorianCalendar.MONTH, 15);
 
        // Display the current date and time
        System.out.println("Modified Date and Time : "
                           + c2.getTime());
 
        // Compare the two calendars
        System.out.println("Both calendars are equal:"
                           + c1.equals(c2));
    }
}


Java
// Java Program to illustrate the equals() function
// of GregorianCalendar class
 
import java.io.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        // Create a new calendar
        GregorianCalendar c1 = (GregorianCalendar)
                     GregorianCalendar.getInstance();
 
        // Display the current date and time
        System.out.println("Current Date and Time : "
                           + c1.getTime());
 
        // Create a second calendar equal to first one
        GregorianCalendar c2 =
             (GregorianCalendar)(Calendar)c1.clone();
 
        // Compare the two calendars
        System.out.println("Both calendars are equal:"
                           + c1.equals(c2));
 
        // Changing the Time Zone of c2
        c2.setTimeZone(TimeZone.getTimeZone("CST"));
 
        // Compare the two calendars
        System.out.println("Both calendars are equal:"
                           + c1.equals(c2));
    }
}


输出:
Current Date and Time : Fri Jul 27 12:05:05 UTC 2018
Both calendars are equal:true
Modified Date and Time : Sun Oct 27 12:05:05 UTC 2019
Both calendars are equal:false

方案二:

Java

// Java Program to illustrate the equals() function
// of GregorianCalendar class
 
import java.io.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        // Create a new calendar
        GregorianCalendar c1 = (GregorianCalendar)
                     GregorianCalendar.getInstance();
 
        // Display the current date and time
        System.out.println("Current Date and Time : "
                           + c1.getTime());
 
        // Create a second calendar equal to first one
        GregorianCalendar c2 =
             (GregorianCalendar)(Calendar)c1.clone();
 
        // Compare the two calendars
        System.out.println("Both calendars are equal:"
                           + c1.equals(c2));
 
        // Changing the Time Zone of c2
        c2.setTimeZone(TimeZone.getTimeZone("CST"));
 
        // Compare the two calendars
        System.out.println("Both calendars are equal:"
                           + c1.equals(c2));
    }
}
输出:
Current Date and Time : Fri Jul 27 12:05:08 UTC 2018
Both calendars are equal:true
Both calendars are equal:false

参考: https: Java/util/GregorianCalendar.html#equals()