Java TimeZone hasSameRules() 方法



描述

Java TimeZone hasSameRules(TimeZone other) 方法返回 true,如果此時區與另一個時區具有相同的規則和偏移量。

宣告

以下是 java.util.TimeZone.hasSameRules() 方法的宣告。

public boolean hasSameRules(TimeZone other)

引數

other − 這是要比較的 TimeZone 物件。

返回值

如果 other 時區不為空且與此時區相同,則方法呼叫返回 true,但 ID 可能除外。

異常

檢查兩個相同時區是否具有相同規則和偏移量的示例

以下示例演示瞭如何使用 Java TimeZone hasSameRules() 方法比較兩個時區物件。我們使用 getDefault() 方法建立了兩個 TimeZone 物件。使用 hasSameRules() 我們比較了 TimeZone 物件並列印了結果。

package com.tutorialspoint;

import java.util.TimeZone;

public class TimeZoneDemo {
   public static void main( String args[] ) {

      // create two time zone objects     
      TimeZone timezoneone = TimeZone.getDefault();
      TimeZone timezonetwo = TimeZone.getDefault(); 

      // comparing two time zones
      boolean res = timezoneone.hasSameRules(timezonetwo);

      // checking the result   
      System.out.println("Comparison result:" +res );
   }    
}

輸出

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

Comparison result:true

檢查 GMT 和 BST 時區是否具有相同偏移量以具有相同規則和偏移量的示例

以下示例演示瞭如何使用 Java TimeZone hasSameRules() 方法比較兩個時區物件。我們使用 GMT 和 BST 的 SimpleTimeZone 物件建立了兩個 TimeZone 物件,它們具有相同的偏移量。使用 hasSameRules() 我們比較了 TimeZone 物件並列印了結果。

package com.tutorialspoint;

import java.util.SimpleTimeZone;
import java.util.TimeZone;

public class TimeZoneDemo {
   public static void main( String args[] ) {

      // create two time zone objects     
      TimeZone timezoneone = new SimpleTimeZone(60, "GMT");
      TimeZone timezonetwo = new SimpleTimeZone(60, "BST");

      // comparing two time zones
      boolean res = timezoneone.hasSameRules(timezonetwo);

      // checking the result   
      System.out.println("Comparison result:" +res );
   }    
}

輸出

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

Comparison result:true

檢查 GMT 和 BST 時區是否具有不同偏移量以具有相同規則和偏移量的示例

以下示例演示瞭如何使用 Java TimeZone hasSameRules() 方法比較兩個時區物件。我們使用 GMT 和 BST 的 SimpleTimeZone 物件建立了兩個 TimeZone 物件,它們具有不同的偏移量。使用 hasSameRules() 我們比較了 TimeZone 物件並列印了結果。

package com.tutorialspoint;

import java.util.SimpleTimeZone;
import java.util.TimeZone;

public class TimeZoneDemo {
   public static void main( String args[] ) {

      // create two time zone objects     
      TimeZone timezoneone = new SimpleTimeZone(60, "GMT");
      TimeZone timezonetwo = new SimpleTimeZone(100, "BST");

      // comparing two time zones
      boolean res = timezoneone.hasSameRules(timezonetwo);

      // checking the result   
      System.out.println("Comparison result:" +res );
   }    
}

輸出

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

Comparison result:false
java_util_timezone.htm
廣告