java.util.Calendar equals()方法

java.util.Calendar.equals() 方法比较是否相等的两个对象,并且如果它们相等,则返回true。Object类中提供了equals() 方法。作为对象类,是Java中所有其他类的父类。Calendar类继承了equals() 方法。它将这个(当前日历对象)日历与指定的对象进行比较。当且仅当参数是同一日历系统的Calendar对象,并且该对象在相同的Calendar参数下代表相同的时间值时,结果才为true。

1 语法

public boolean equals(Object object)  

2 参数

object:要比较的对象

3 返回值

返回布尔值

4 示例1 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar equals()方法的例子
 */
import java.util.Calendar;  
public class CalendarEqualsExample1 {  
public static void main(String[] args) {  
      // create a new calendar object  
     Calendar cal1 =  (Calendar) Calendar.getInstance();  
      // print the current date and time  
      System.out.println("" + cal1.getTime());  
      // create a second calendar object  
        Calendar cal2 = (Calendar) Calendar.getInstance();  
      // print cal2  
      System.out.println("" + cal2.getTime());  
      // compare the first and second calendar object  
      System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));  
   }  
}  

输出结果为:

Wed Aug 01 02:31:56 PDT 2018
Wed Aug 01 02:31:57 PDT 2018
Cal1 and Cal2 are equal:false

5 示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar equals()方法的例子
 */
import java.util.Calendar;  
public class CalendarEqualsExample2 {  
public static void main(String[] args) {  
      // create a new calendar object  
    Calendar cal1 =  (Calendar) Calendar.getInstance();  
      // print the current date and time  
      System.out.println("" + cal1.getTime());  
      // create clone of first calendar object  
      Calendar cal2 = (Calendar) (Calendar) cal1.clone();  
      // print cal2  
      System.out.println("" + cal2.getTime());  
      // compare the first calendar object and cloned calendar object  
      System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));      
   }  
}  

输出结果为:

Wed Aug 01 02:34:20 PDT 2018
Wed Aug 01 02:34:20 PDT 2018
Cal1 and Cal2 are equal:true

6 示例3

import java.util.Calendar;  
public class CalendarEqualsExample3 {  
public static void main(String[] args) {  
      // create a new calendar object  
   Calendar cal1 =  (Calendar) Calendar.getInstance();  
      // print the current date and time  
      System.out.println("" + cal1.getTime());  
      // create a clone of first calendar object  
      Calendar cal2 = (Calendar) (Calendar) cal1.clone();  
      // print cal2  
      System.out.println("" + cal2.getTime());  
      // change YEAR field of cloned calendar object  
      cal2.add(Calendar.YEAR, 5);  
      // compare the two calendars  
      System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));  
   }  
}  

输出结果为:

Wed Aug 01 02:36:22 PDT 2018
Wed Aug 01 02:36:22 PDT 2018
Cal1 and Cal2 are equal:false

 

热门文章

优秀文章