TimeZone
概述
<code>TimeZone</code> represents a time zone offset, and also figures out daylight savings.
Typically, you get a <code>TimeZone</code> using <code>getDefault</code> which creates a <code>TimeZone</code> based on the time zone where the program is running.
For example, for a program running in Japan, <code>getDefault</code> creates a <code>TimeZone</code> object based on Japanese Standard Time.
TimeZone代表一个 时间区域偏移;
通常使用getDefault可以获得一个TimeZone(根据程序运行时的时区);
比如,在日本运行的程序,会获取基于日本标准时间的TimeZone;
1 2 3 4 5 6 | TimeZone timeZone = TimeZone.getDefault(); String timeZoneID = timeZone.getID(); System.out.println(timeZoneID); // Asia/Shanghai String displayName = timeZone.getDisplayName(); System.out.println(displayName); // 中国标准时间 |
You can also get a <code>TimeZone</code> using <code>getTimeZone</code> along with a time zone ID.
For instance, the time zone ID for the U.S. Pacific Time zone is "America/Los_Angeles".
So, you can get a U.S. Pacific Time <code>TimeZone</code> object with:TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
可以通过 getTimeZone获取指定的TimeZone;
比如,America/Los_Angeles将会获取 美国/洛杉矶 的TimeZone;
You can use the <code>getAvailableIDs</code> method to iterate through all the supported time zone IDs.
You can then choose a supported ID to get a TimeZone.
If the time zone you want is not represented by one of the supported IDs, then a custom time zone ID can be specified to produce a TimeZone.
可以使用getAvailableIDs查看支持的time zone IDs;
如果没有支持的,可以自定义时区ID;
The syntax of a custom time zone ID is:
自定义时区ID语法:
...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | abstract public class TimeZone implements Serializable, Cloneable { private static volatile TimeZone defaultTimeZone; private String ID; static TimeZone getDefaultRef() { TimeZone defaultZone = defaultTimeZone; if (defaultZone == null ) { // Need to initialize the default time zone. defaultZone = setDefaultZone(); assert defaultZone != null ; } // Don't clone here. return defaultZone; } private static synchronized TimeZone setDefaultZone() { TimeZone tz; // get the time zone ID from the system properties String zoneID = AccessController.doPrivileged( new GetPropertyAction( "user.timezone" )); // if the time zone ID is not set (yet), perform the // platform to Java time zone ID mapping. if (zoneID == null || zoneID.isEmpty()) { String javaHome = AccessController.doPrivileged( new GetPropertyAction( "java.home" )); try { zoneID = getSystemTimeZoneID(javaHome); if (zoneID == null ) { zoneID = GMT_ID; } } catch (NullPointerException e) { zoneID = GMT_ID; } } // Get the time zone for zoneID. But not fall back to // "GMT" here. tz = getTimeZone(zoneID, false ); if (tz == null ) { // If the given zone ID is unknown in Java, try to // get the GMT-offset-based time zone ID, // a.k.a. custom time zone ID (e.g., "GMT-08:00"). String gmtOffsetID = getSystemGMTOffsetID(); if (gmtOffsetID != null ) { zoneID = gmtOffsetID; } tz = getTimeZone(zoneID, true ); } assert tz != null ; final String id = zoneID; AccessController.doPrivileged( new PrivilegedAction<Void>() { @Override public Void run() { System.setProperty( "user.timezone" , id); return null ; } }); defaultTimeZone = tz; return tz; } public static TimeZone getDefault() { return (TimeZone) getDefaultRef().clone(); } public String getID() { return ID; } public static synchronized String[] getAvailableIDs() { return ZoneInfo.getAvailableIDs(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)