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


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


Calendar类中的hashCode()方法用于返回此Calendar对象的哈希码。

用法:

public int hashCode()

参数:该方法不包含任何参数。


返回值:该方法返回此日历对象的哈希码值。

以下示例程序旨在说明Calendar类的hashCode()方法的用法:
示例1:

// Java code to illustrate 
// hashCode() method 
  
import java.util.*; 
  
public class Calendar_Demo { 
    public static void main(String args[]) 
    { 
        // Creating a calendar object 
        Calendar calndr = Calendar.getInstance(); 
  
        // Displaying the current calendar 
        System.out.println("The time on"
                           + " Calendar shows: "
                           + calndr.getTime()); 
  
        // Getting the hash code 
        int hash_code = calndr.hashCode(); 
        System.out.println("The hash code "
                           + "for this calendar is: "
                           + hash_code); 
    } 
}
输出:
The time on Calendar shows: Wed Feb 20 15:55:22 UTC 2019
The hash code for this calendar is: 194416203

示例2:

// Java code to illustrate 
// hashCode() method 
  
import java.util.*; 
  
public class Calendar_Demo { 
    public static void main(String args[]) 
    { 
        // Creating a calendar 
        Calendar calndr 
            = new GregorianCalendar(2018, 6, 10); 
  
        // Displaying the current calendar 
        System.out.println("The time on"
                           + " Calendar shows: "
                           + calndr.getTime()); 
  
        // Getting the hash code 
        int hash_code = calndr.hashCode(); 
        System.out.println("The hash code "
                           + "for this calendar is: "
                           + hash_code); 
    } 
}
输出:
The time on Calendar shows: Tue Jul 10 00:00:00 UTC 2018
The hash code for this calendar is: -2123101761

参考: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#hashCode–



相关用法


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