Java中的日历 hashCode() 方法及示例
Calendar 类中的hashCode()方法用于返回此日历对象的哈希码。
句法:
public int hashCode()
参数:该方法不带任何参数。
返回值:该方法返回此日历对象的哈希码值。
下面的程序说明了 Calendar 类的 hashCode() 方法的工作:
示例 1:
Java
// 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);
}
}
Java
// 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: Wed Feb 20 15:55:22 UTC 2019
The hash code for this calendar is: 194416203
示例 2:
Java
// 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: Java/util/Calendar.html#hashCode–