Java SimpleDateFormat hashCode()方法及示例

来源:这里教程网 时间:2026-02-17 21:10:16 作者:

Java SimpleDateFormat hashCode()方法及示例

SimpleDateFormat类hashCode() 方法用于返回该SimpleDateFormat对象的哈希代码值。哈希代码是整数类型的。

语法

public int hashCode()

参数: 该方法不接受任何参数。

返回值: 该方法返回该SimpleDateFormat对象的哈希代码值,为整数类型。

下面的程序说明了SimpleDateFormat的hashCode()方法的工作。

例1 :

// Java to illustrate hashCode() method  import java.text.*;import java.util.*;  public class SimpleDateFormat_Demo {    public static void main(String[] args)        throws InterruptedException    {        // Initializing SimpleDateFormat        SimpleDateFormat SDFormat            = new SimpleDateFormat(                "dd/MM/yyyy");          // Displaying the formats        Date date = new Date();        String str_Date1 = SDFormat.format(date);        System.out.println("The Original: "                           + (str_Date1));          // Displaying the hash code        System.out.println("The hash code: "                           + SDFormat.hashCode());    }}

输出:

The Original: 30/01/2019The hash code: -650712384

例2 :

// Java to illustrate hashCode() method  import java.text.*;import java.util.*;  public class SimpleDateFormat_Demo {    public static void main(String[] args)        throws InterruptedException    {        // Initializing SimpleDateFormat        SimpleDateFormat SDFormat            = new SimpleDateFormat(                "MM/dd/yyyy");          // Displaying the formats        Date date = new Date();        String str_Date1 = SDFormat.format(date);        System.out.println("The Original: "                           + (str_Date1));          // Displaying the hash code        System.out.println("The hash code: "                           + SDFormat.hashCode());    }}

输出:

The Original: 01/30/2019The hash code: 2087096576

相关推荐