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


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


java.util.GregorianCalendar.hashCode()是Java中的内置函数,该函数为此GregorianCalendar实例生成哈希码。

用法:

public int hashCode()

参数:该函数不接受任何参数。


返回值:此函数返回一个整数值,该值表示此GregorianCalendar实例的哈希码。

例子:

Input : Thu Jul 24 00:53:29 UTC 1997
Output : 2121703905

Input : Tue Jul 24 00:54:11 UTC 2018
Output : -909136554

下面的程序演示了java.util.GregorianCalendar.hashCode()函数:
示例1:

// Java Program to illustrate hashCode() function 
// of GregorianCalendar class 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
     public static void main(String[] args) { 
      
      // Create a new calendar 
      GregorianCalendar c = (GregorianCalendar)  
                GregorianCalendar.getInstance(); 
  
      // Display the current date and time 
      System.out.println("Current Date and Time : "
                                    + c.getTime()); 
        
      // Display hashcode for this calendar 
      System.out.println("Hash Code:" + c.hashCode()); 
   } 
}
输出:
Current Date and Time : Fri Jul 27 11:43:16 UTC 2018
Hash Code:-612105832

示例2:

// Java Program to illustrate hashCode() function 
// of GregorianCalendar class 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
     public static void main(String[] args) { 
      
      // Create a new calendar 
      GregorianCalendar c = (GregorianCalendar)  
                 GregorianCalendar.getInstance(); 
  
      // Display the current date and time 
      System.out.println("Current Date and Time : "
                                     + c.getTime()); 
        
      c.add((GregorianCalendar.YEAR), -12); 
      System.out.println("Modified Date and Time : "
                                    + c.getTime()); 
        
      // Display hashcode for this calendar 
      System.out.println("Hash Code of the " +  
      "modified date and time:" + c.hashCode()); 
   } 
}
输出:
Current Date and Time : Fri Jul 27 11:43:18 UTC 2018
Modified Date and Time : Thu Jul 27 11:43:18 UTC 2006
Hash Code of the modified date and time:-1346149059

参考: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#hashCode()



相关用法


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