Java GregorianCalendar setTimeZone()方法

Java GregorianCalendar setTimeZone()方法

java.util.GregorianCalendar.setTimeZone() 方法是Java中的一个内置方法,它根据传递的参数将当前时区修改为新的时区。

语法

public void setTimeZone(TimeZone tz)

参数: 该方法需要一个参数tz,即指定新的时区值的TimeZone对象。

返回值: 该方法不返回任何值。

例子

Input : IST
Output : India Standard Time

Input : UTC
Output : Coordinated Universal Time

下面的程序说明了Java中的java.util.GregorianCalendar.setTimeZone()函数。

程序1 :

// Java Program to  illustrate 
// GregorianCalendar.setTimeZone()
// 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("" + cal.getTime());
        
      System.out.println("Current Time Zone : " + 
                    cal.getTimeZone().getDisplayName());
        
      // Convert to another time zone
      cal.setTimeZone(TimeZone.getTimeZone("CST"));
      System.out.println("New Time Zone : " + 
                    cal.getTimeZone().getDisplayName());
   }
}

输出。

Wed Jul 25 11:11:14 UTC 2018
Current Time Zone : Coordinated Universal Time
New Time Zone : Central Standard Time

程序2

// Java Program to  illustrate 
// GregorianCalendar.setTimeZone()
// 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("" + cal.getTime());
        
      System.out.println("Current Time Zone : " + 
                    cal.getTimeZone().getDisplayName());
        
      // Convert to another time zone
      cal.setTimeZone(TimeZone.getTimeZone("IST"));
      System.out.println("New Time Zone : " + 
                    cal.getTimeZone().getDisplayName());
   }
}

输出。

Wed Jul 25 11:11:21 UTC 2018
Current Time Zone : Coordinated Universal Time
New Time Zone : India Standard Time

**参考资料: ** https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#setTimeZone()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程