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

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

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

Java中ChronoPeriod 类hashCode() 方法用于获取该周期生成的 hashCode。

句法:

int hashCode()

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

返回值:此方法返回为给定时间段生成的 hashCode。

下面的程序说明了Java中的 hashCode() 方法:

程序 1

// Java code to show the function hashCode()
// to get the hashCode for the given period
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
  
public class ChronoPeriodDemo {
  
    // Function to generate hashCode for the given period
    static void getNumberOfDays(int year, int months, int days)
    {
        ChronoPeriod period = Period.of(year, months, days);
        System.out.println(period.hashCode());
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int year = 12;
        int months = 3;
        int days = 31;
  
        getNumberOfDays(year, months, days);
    }
}
输出:
2032396

方案二

// Java code to show the function hashCode()
// to get the hashCode for the given period
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
  
public class ChronoPeriodDemo {
  
    // Function to generate hashCode for the given period
    static void getNumberOfDays(int year, int months, int days)
    {
        ChronoPeriod period = Period.of(year, months, days);
        System.out.println(period.hashCode());
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int year = -12;
        int months = 3;
        int days = 31;
  
        getNumberOfDays(year, months, days);
    }
}
输出:
2032372

参考:https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/ChronoPeriod.html#hashCode–