Java中的 GregorianCalendar getTimeZone() 方法
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()函数:
方案一:
Java
// 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());
}
}
Java
// 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:16 UTC 2018
Time Zone : Coordinated Universal Time
程序 2:在这个程序中,我们使用 setTimeZone() 更改当前时区,然后使用 getTimeZone() 方法获取新时区。
Java
// 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: Java/util/GregorianCalendar.html#getTimeZone()