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


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


Java Date类的hashCode()方法返回一个值,该值是此对象的哈希码。

用法:

public int hashCode()

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


返回值:它为此对象返回一个hashCode值。

异常:该函数不会引发任何异常。

下面的程序演示了上述函数:

// Java code to demonstrate 
// hashCode() function of Date class 
  
import java.util.Date; 
import java.util.Calendar; 
public class GfG { 
    // main method 
    public static void main(String[] args) 
    { 
  
        // creating a Calendar object 
        Calendar c1 = Calendar.getInstance(); 
  
        // set Month 
        // MONTH starts with 0 i.e. ( 0 - Jan) 
        c1.set(Calendar.MONTH, 11); 
  
        // set Date 
        c1.set(Calendar.DATE, 05); 
  
        // set Year 
        c1.set(Calendar.YEAR, 1996); 
  
        // creating a date object with specified time. 
        Date dateOne = c1.getTime(); 
  
        System.out.println("Date: " + dateOne); 
  
        // Prints hash Code 
        System.out.println("HashCode: "
                           + dateOne.hashCode()); 
    } 
}
输出:
Date: Thu Dec 05 08:22:04 UTC 1996
HashCode: -629399711
// Java code to demonstrate 
// hashCode() function of Date class 
  
import java.util.Date; 
import java.util.Calendar; 
public class GfG { 
    // main method 
    public static void main(String[] args) 
    { 
  
        // creating a Calendar object 
        Calendar c1 = Calendar.getInstance(); 
  
        // set Month 
        // MONTH starts with 0 i.e. ( 0 - Jan) 
        c1.set(Calendar.MONTH, 11); 
  
        // set Date 
        c1.set(Calendar.DATE, 21); 
  
        // set Year 
        c1.set(Calendar.YEAR, 1999); 
  
        // creating a date object with specified time. 
        Date dateOne = c1.getTime(); 
  
        System.out.println("Date: " + dateOne); 
  
        // Prints hash Code 
        System.out.println("HashCode: "
                           + dateOne.hashCode()); 
    } 
}
输出:
Date: Tue Dec 21 08:22:09 UTC 1999
HashCode: 871724355


相关用法


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