Java Calendar hashCode() 方法



描述

Java Calendar hashCode() 方法返回此日曆物件的雜湊碼。

宣告

以下是 java.util.Calendar.hashCode() 方法的宣告

public int hashCode()

引數

返回值

該方法返回此日曆物件的雜湊碼值。

異常

獲取當前日期日曆例項的雜湊碼示例

以下示例演示了 Java Calendar hashCode() 方法的使用。我們使用 getInstance() 方法建立一個當前日期的日曆例項,並使用 getTime() 方法列印日期和時間,使用 hashCode() 方法列印雜湊碼。

package com.tutorialspoint;

import java.util.Calendar;

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create a calendar    
      Calendar cal = Calendar.getInstance();

      // print the time
      System.out.println("Date And Time Is: " + cal.getTime());
	  
      // print the hashcode
      System.out.println("HashCode Is: " + cal.hashCode());
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

Date And Time Is: Mon Sep 26 20:13:01 IST 2022
HashCode Is: -1942629770

獲取當前日期 GregorianCalendar 例項的雜湊碼示例

以下示例演示了 Java Calendar hashCode() 方法的使用。我們使用 GregorianCalendar() 方法建立一個當前日期的日曆例項,並使用 getTime() 方法列印日期和時間,使用 hashCode() 方法列印雜湊碼。

package com.tutorialspoint;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create a calendar    
      Calendar cal = new GregorianCalendar();

      // print the time
      System.out.println("Date And Time Is: " + cal.getTime());

      // print the hashcode
      System.out.println("HashCode Is: " + cal.hashCode());
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

Date And Time Is: Mon Sep 26 20:12:40 IST 2022
HashCode Is: -1942650517

獲取給定日期 GregorianCalendar 例項的雜湊碼示例

以下示例演示了 Java Calendar hashCode() 方法的使用。我們使用 GregorianCalendar() 方法建立一個特定日期的日曆例項,並使用 getTime() 方法列印日期和時間,使用 hashCode() 方法列印雜湊碼。

package com.tutorialspoint;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create a calendar    
      Calendar cal = new GregorianCalendar(2025,8,26);

      // print the time
      System.out.println("Date And Time Is: " + cal.getTime());
	  
      // print the hashcode
      System.out.println("HashCode Is: " + cal.hashCode());
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

Date And Time Is: Fri Sep 26 00:00:00 IST 2025
HashCode Is: 1951802754
java_util_calendar.htm
廣告
© . All rights reserved.