Java教程

Java Calendar getTimeZone()方法

java.util.Calendar类的getTimeZone()方法获取日历对象的TimeZone并返回一个TimeZone对象。

语法

public TimeZone getTimeZone()

参数

未传递任何参数。

返回

返回TimeZone对象。

抛出

不抛出异常。

示例1

import java.util.Calendar;
import java.util.TimeZone;
public class JavaCalendargetTimeZoneExample1 {
    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 Time

示例2

import java.util.Calendar;
import java.util.TimeZone;
public class JavaCalendargetTimeZoneExample2 {
    public static void main(String[] args) {
        Calendar mycal2 = Calendar.getInstance();
        TimeZone timezoneobj = mycal2.getTimeZone();
        System.out.println(" Display Name of Current TimeZone :" + timezoneobj.getDisplayName());
    }
}
输出:
Display Name of Current TimeZone Pacific Standard Time
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4