Java 日历 hashCode()方法及示例
Calendar类中的 hashCode() 方法用于返回该日历对象的哈希代码。
语法:
public int 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-