TimeZone

发布时间 2023-12-20 17:08:48作者: anpeiyong

概述

<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;

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语法:

...

 

 

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();
        }
    }