當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。