📜  Java中的日历 setTimeZone() 方法及示例

📅  最后修改于: 2022-05-13 01:54:26.273000             🧑  作者: Mango

Java中的日历 setTimeZone() 方法及示例

Calendar 类中的setTimeZone(TimeZone time_zone)方法将 Time Zone 值作为参数,修改或设置此 Calendar 表示的时区。

句法:

public void setTimeZone(TimeZone time_zone)

参数:该方法采用一个日期类型的参数time_zone ,并引用要设置的给定日期。

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

下面的程序说明了 Calendar 类的 setTimeZone() 方法的工作:
示例 1:

// Java code to illustrate
// setTime() method
  
import java.util.*;
  
public class Calendar_Demo {
    public static void main(String[] args)
    {
  
        // Creating calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Displaying the current time zone
        String tz_name = calndr.getTimeZone()
                             .getDisplayName();
  
        System.out.println("The Current Time"
                           + " Zone: " + tz_name);
  
        TimeZone time_zone
            = TimeZone.getTimeZone("GMT");
  
        // Modifying the time zone
        calndr.setTimeZone(time_zone);
  
        // Displaying the modified zone
        System.out.println("Modified Zone: "
                           + calndr.getTimeZone()
                                 .getDisplayName());
    }
}
输出:
The Current Time Zone: Coordinated Universal Time
Modified Zone: Greenwich Mean Time

示例 2:

// Java code to illustrate
// setTimeZone() method
  
import java.util.*;
  
public class Calendar_Demo {
    public static void main(String[] args)
    {
  
        // Creating calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Displaying the current time zone
        String tz_name = calndr.getTimeZone()
                             .getDisplayName();
  
        System.out.println("The Current Time"
                           + " Zone: " + tz_name);
  
        TimeZone time_zone
            = TimeZone.getTimeZone("Pacific/Tahiti");
  
        // Modifying the time zone
        calndr.setTimeZone(time_zone);
  
        // Displaying the modified zone
        System.out.println("Modified Zone: "
                           + calndr.getTimeZone()
                                 .getDisplayName());
    }
}
输出:
The Current Time Zone: Coordinated Universal Time
Modified Zone: Tahiti Time

参考: https: Java Java.util.TimeZone)