當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Calendar setTimZone()用法及代碼示例

java.util.Calendar 類的 setTimZonee() 方法用於以傳入的 TimeZone 值(對象)為參數設置 TimeZone。

用法

public void setTimeZone(TimeZone value)

參數

value- 設置為時區的值。

返回

NA

拋出

NA

例子1

import java.util.Calendar;
import java.util.TimeZone;
public class JavaCalendarsetTimeZoneExample1 {
    public static void main(String[] args) {
    Calendar mycal = Calendar.getInstance();
                //getting the display name of TimeZone therwise it will return in object form
        System.out.println(" TimeZone:"
                + mycal.getTimeZone().getDisplayName());
                // set the time zone of mycal to GMT
        mycal.setTimeZone(TimeZone.getTimeZone("GMT"));
        System.out.println("Time Zone after changing to GMT:"
                + mycal.getTimeZone().getDisplayName());
        }
}

輸出:

TimeZone:Pacific Standard Time
Time Zone after changing to GMT:Greenwich Mean Tim

例子2

import java.util.Calendar;
import java.util.TimeZone;
public class JavaCalendarsetTimeZoneExample2 {
 public static void main(String[] args) {
 Calendar mycal2 = Calendar.getInstance();
  TimeZone timezoneobj = mycal2.getTimeZone();
   System.out.println(" Display Name of Current TimeZone (mycal2):" + timezoneobj.getDisplayName());
              Calendar mycal3 = Calendar.getInstance();
                mycal3.setTimeZone(timezoneobj);
                timezoneobj = mycal3.getTimeZone();
                System.out.println(" Display Name of TimeZone (mycal3):" + timezoneobj.getDisplayName());
                   }
}

輸出:

Display Name of Current TimeZone (mycal2):Pacific Standard Time
Display Name of TimeZone (mycal3):Pacific Standard Time

相關用法


注:本文由純淨天空篩選整理自 Java Calendar setTimZone() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。