📜  Java中的 LocalDateTime hashCode() 方法及示例

📅  最后修改于: 2022-05-13 01:55:31.111000             🧑  作者: Mango

Java中的 LocalDateTime hashCode() 方法及示例

LocalDateTime 类hashCode()方法用于返回此 LocalDateTime 实例的哈希码。如果对象没有改变,哈希码总是相同的。该方法派生自Java的 Object 类,并以类似的方式执行。

句法:

public int hashCode()

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

返回:此方法返回整数值,它是此 LocalDateTime 实例的哈希码值。

下面的程序说明了 LocalDateTime.hashCode() 方法:

方案一:

Java
// Java program to demonstrate
// LocalDateTime.hashCode() method
 
import java.time.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a LocalDateTime Object
        LocalDateTime local
            = LocalDateTime.parse("2017-07-22T09:32:42");
 
        // get hashcode for LocalDateTime
        int hashcode = local.hashCode();
 
        // print result
        System.out.println("hashCode value: "
                           + hashcode);
    }
}


Java
// Java program to demonstrate
// LocalDateTime.hashCode() method
 
import java.time.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a LocalDateTime Object
        LocalDateTime local
            = LocalDateTime.parse("2006-01-02T19:32:42");
 
        // get hashcode for LocalDateTime
        int hascode = local.hashCode();
 
        // print result
        System.out.println("hashCode value: "
                           + hascode);
    }
}


输出:
hashCode value: -2030906730

方案二:

Java

// Java program to demonstrate
// LocalDateTime.hashCode() method
 
import java.time.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a LocalDateTime Object
        LocalDateTime local
            = LocalDateTime.parse("2006-01-02T19:32:42");
 
        // get hashcode for LocalDateTime
        int hascode = local.hashCode();
 
        // print result
        System.out.println("hashCode value: "
                           + hascode);
    }
}
输出:
hashCode value: 1849330620

参考: https: Java/time/LocalDateTime.html#hashCode()