java.util.GregorianCalendar.getTimeZone()方法是Java中的內置方法,可獲取時區並返回與此日曆關聯的TimeZone對象。
用法:
public TimeZone getTimeZone()
參數:此方法不接受任何參數。
返回值:此方法返回一個TimeZone對象,該對象表示此日曆的時區。
例子:
Input : Mon Jul 23 19:45:33 UTC 2018 Output : Coordinated Universal Time Input : Wed Oct 23 20:02:52 UTC 2019 cal.setTimeZone(TimeZone.getTimeZone("CST")); Output : Central Standard Time
以下程序說明了Java中的java.util.GregorianCalendar.getTimeZone()函數:
示例1:
// Java Program to illustrate
// GregorianCalendar.getTimeZone()
// function
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) {
// Create a new calendar
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
//Display the current date and time
System.out.println("Current Date and Time : "
+ cal.getTime());
/* Creating a Time Zone object and
storing this TimeZone */
TimeZone t = cal.getTimeZone();
// Display the time zone
System.out.println("Time Zone : " +
t.getDisplayName());
}
}
輸出:
Current Date and Time : Wed Jul 25 11:25:16 UTC 2018 Time Zone : Coordinated Universal Time
示例2:在此程序中,我們使用setTimeZone()更改當前時區,然後使用getTimeZone()方法獲取新時區。
// Java Program to illustrate
// GregorianCalendar.getTimeZone()
// function
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) {
// Create a new calendar
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
// Display the current date and time
System.out.println("Current Date and Time : "
+ cal.getTime());
// Changing the current time zone
cal.setTimeZone(TimeZone.getTimeZone("CST"));
/* Creating a Time Zone object and
storing the TimeZone */
TimeZone t = cal.getTimeZone();
// Display the time zone
System.out.println("Time Zone : " +
t.getDisplayName());
}
}
輸出:
Current Date and Time : Wed Jul 25 11:25:22 UTC 2018 Time Zone : Central Standard Time
參考: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#getTimeZone()
相關用法
- Java DateFormat getTimeZone()用法及代碼示例
- Java Calendar getTimeZone()用法及代碼示例
- Java TimeZone getTimeZone()用法及代碼示例
- Java GregorianCalendar add()用法及代碼示例
- Java GregorianCalendar hashCode()用法及代碼示例
- Java GregorianCalendar getGreatestMinimum()用法及代碼示例
- Java GregorianCalendar equals()用法及代碼示例
- Java GregorianCalendar setTimeZone()用法及代碼示例
- Java GregorianCalendar clone()用法及代碼示例
- Java GregorianCalendar getMaximum()用法及代碼示例
- Java GregorianCalendar isLeapYear()用法及代碼示例
- Java GregorianCalendar getMinimum()用法及代碼示例
- Java GregorianCalendar getLeastMaximum()用法及代碼示例
- Java Java.util.GregorianCalendar用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 GregorianCalendar getTimeZone() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。