Java中的 TimeZone getTimeZone() 方法及示例
Java中TimeZone 类的getTimeZone()方法用于了解任何传递的 TimeZone ID 的实际 TimeZone。
句法:
public static TimeZone getTimeZone(String the_ID)
参数:该方法有一个参数the_ID , 字符串数据类型,是指需要知道TimeZone的ID。
返回值:如果程序无法理解指定的 ID,则该方法返回所传递 ID 的指定时区或 GMT 时区。
下面的程序说明了 TimeZone 的 getTimeZone() 方法的工作:
示例 1:
// Java code to illustrate getTimeZone() method
import java.util.*;
public class TimeZoneDemo {
public static void main(String args[])
{
// Creating a TimeZone
TimeZone the_time_zone
= TimeZone.getDefault();
// Knowing the TimeZone
System.out.println("The TimeZone is: "
+ the_time_zone
.getTimeZone("GMT+5:30"));
}
}
输出:
The TimeZone is: sun.util.calendar.ZoneInfo[id="GMT+05:30",
offset=19800000, dstSavings=0, useDaylight=false, transitions=0, lastRule=null]
示例 2:
// Java code to illustrate getTimeZone() method
import java.util.*;
public class TimeZoneDemo {
public static void main(String args[])
{
// Creating a TimeZone
TimeZone the_time_zone
= TimeZone.getDefault();
// Knowing the TimeZone
System.out.println("The TimeZone is: "
+ the_time_zone
.getTimeZone("GMT-3:30"));
}
}
输出:
The TimeZone is: sun.util.calendar.ZoneInfo[id="GMT-03:30",
offset=-12600000, dstSavings=0, useDaylight=false, transitions=0, lastRule=null]
参考: https: Java/util/TimeZone.html#getTimeZone()