Java DateFormat hashCode()方法及示例
DateFormat类 的 hashCode() 方法是用来返回这个DateFormat对象的哈希代码值。哈希代码是整数类型的。
语法
public int hashCode()
参数: 该方法不接受任何参数。
返回值: 该方法返回该DateFormat对象的哈希代码值,为整数类型。
下面的程序说明了DateFormat的hashCode()方法的工作原理:
例1 :
// Java to illustrate hashCode() method import java.text.*;import java.util.*; public class DateFormat_Demo { public static void main(String[] args) { // Initializing DateFormat DateFormat DFormat = DateFormat.getDateTimeInstance(); // Displaying the formats Date date = new Date(); String str_Date1 = DFormat.format(date); System.out.println("The Original: " + (str_Date1)); // Displaying the hash code System.out.println("The hash code: " + DFormat.hashCode()); }}
输出:
The Original: Mar 27, 2019 10:20:51 AMThe hash code: 2034585966
例2 :
// Java to illustrate hashCode() method import java.text.*;import java.util.*; public class DateFormat_Demo { public static void main(String[] args) { // Initializing DateFormat DateFormat DFormat = new SimpleDateFormat("MM/dd/yyyy"); // Displaying the formats Date date = new Date(); String str_Date1 = DFormat.format(date); System.out.println("The Original: " + (str_Date1)); // Displaying the hash code System.out.println("The hash code: " + DFormat.hashCode()); }}输出:
The Original: 03/27/2019The hash code: 2087096576
参考资料: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#hashCode()
