当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java GregorianCalendar equals()用法及代码示例


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

用法:

public boolean equals(Object obj)

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


返回值:仅当指定的Object是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 calender 
        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()



相关用法


注:本文由纯净天空筛选整理自RICHIK BHATTACHARJEE大神的英文原创作品 GregorianCalendar equals() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。