Java 日历 equals()方法

Java 日历 equals()方法

java.util.Calendar.equals()是java.util包中 Calendar 类的一个方法。该方法将这个Calendar与指定的Object进行比较。如果这个对象与Object相等,该方法返回true。如果不是这样,也就是说,如果两个Calendars之间的参数有任何不同,那么就会返回false。

语法:

public boolean equals(Object object)

其中,object是对象要与之比较。

下面是一些例子,以更好地理解Calendar.equals()函数的实现。

例子 1 :

// Java code to show the use of
// equals() method of Calendar class
import java.util.*;
  
class GFG {
  
// Driver code
public static void main(String[] args) 
            throws InterruptedException {
                  
    // creating calendar object
    Calendar cal_obj1 = Calendar.getInstance();
    Calendar cal_obj2 = cal_obj1;
                  
    // printing current date
    System.out.println("Time 1 : " + cal_obj1.getTime());
                  
    // printing current date
    System.out.println("Time 2 : " + cal_obj2.getTime());
      
    // checking if 1st date is equal to 2nd date
    // and printing the result
    System.out.println(cal_obj1.equals(cal_obj2));
    }
}

输出:

Time 1 : Thu Mar 01 09:36:17 UTC 2018
Time 2 : Thu Mar 01 09:36:17 UTC 2018
true

例2:

// Java code to show the use of
// equals() method of Calendar class
import java.util.*;
  
class GFG {
      
    // Driver code
    public static void main(String[] args) {
  
    // creating calendar objects
    Calendar cal_obj1 = Calendar.getInstance();
    Calendar cal_obj2 = Calendar.getInstance();
  
    // displaying the current date
    System.out.println("Current date is : " +
                        cal_obj1.getTime());
  
    // changing year in cal_obj2 calendar
    cal_obj2.set(Calendar.YEAR, 2010);
      
    // displaying the year
    System.out.println("Year is " + 
                        cal_obj2.get(Calendar.YEAR));
  
    // check if calendar date is equal to current date
    System.out.println("Result : " + 
                        cal_obj1.equals(cal_obj2));
    }
}

输出:

Current date is : Thu Mar 01 09:39:30 UTC 2018
Year is 2010
Result : false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程