Java GregorianCalendar equals()方法

Java GregorianCalendar equals()方法

java.util.GregorianCalendar.equals() 方法是Java中的一个内置函数,用于检查此GregorianCalendar 实例和作为参数传递给该函数的Object之间是否相等。只有当指定的对象是一个GregorianCalendar对象,并且与这个 GregorianCalendar实例具有相同的时间值(从纪元的毫秒偏移)时,它才会返回真。

语法

public boolean equals(Object obj)

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

返回值: 只有当指定的对象是GregorianCalendar对象,并且与该实例具有相同的时间值(从纪元的毫秒偏移)时,该方法才会返回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()函数。

程序1 :

// 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));
    }
}

输出

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

程序2

// 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://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#equals()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程