Java GregorianCalendar getTimeZone()方法

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()函数。

程序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()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程