Java TimeZone getTimeZone() 方法



描述

Java TimeZone getTimeZone(String ID) 方法用於獲取給定 ID 的 TimeZone。

宣告

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

public static TimeZone getTimeZone(String ID)

引數

ID − 這是 TimeZone 的 ID。

返回值

方法呼叫返回指定的 TimeZone,如果無法識別給定的 ID,則返回 GMT 時區。

異常

獲取 GMT-8 時區示例

以下示例演示瞭如何使用 Java TimeZone getTimeZone() 方法根據給定的 ID 獲取時區。我們使用 getDefault() 方法建立了一個 TimeZone。使用 getTimeZone(),我們列印了 GMT - 8 的 TimeZone 物件。

package com.tutorialspoint;

import java.util.TimeZone;

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

      // create time zone object     
      TimeZone timezone = TimeZone.getDefault();

      // checking time zone value     
      System.out.println("Time zone: " + timezone.getTimeZone("GMT-8:00"));
   }    
}

輸出

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

Time zone: sun.util.calendar.ZoneInfo[id="GMT-08:00",offset=-28800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

獲取 IST 時區示例

以下示例演示瞭如何使用 Java TimeZone getTimeZone() 方法根據給定的 ID 獲取時區。我們使用 getDefault() 方法建立了一個 TimeZone。使用 getTimeZone(),我們列印了 IST 的 TimeZone 物件。

package com.tutorialspoint;

import java.util.TimeZone;

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

      // create time zone object     
      TimeZone timezone = TimeZone.getDefault();

      // checking time zone value     
      System.out.println("Time zone: " + timezone.getTimeZone("IST"));
   }    
}

輸出

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

Time zone:sun.util.calendar.ZoneInfo[id="IST",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]

獲取 BST 時區示例

以下示例演示瞭如何使用 Java TimeZone getTimeZone() 方法根據給定的 ID 獲取時區。我們使用 getDefault() 方法建立了一個 TimeZone。使用 getTimeZone(),我們列印了 BST 的 TimeZone 物件。

package com.tutorialspoint;

import java.util.TimeZone;

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

      // create time zone object     
      TimeZone timezone = TimeZone.getDefault();

      // checking time zone value     
      System.out.println("Time zone: " + timezone.getTimeZone("BST"));
   }    
}

輸出

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

Time zone: sun.util.calendar.ZoneInfo[id="BST",offset=21600000,dstSavings=0,useDaylight=false,transitions=8,lastRule=null]
java_util_timezone.htm
廣告