timezone.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 726 行 · 第 1/2 页
JAVA
726 行
public String getDisplayName(boolean daylight, int style, Locale locale) { /* NOTES: * (1) We use SimpleDateFormat for simplicity; we could do this * more efficiently but it would duplicate the SimpleDateFormat code * here, which is undesirable. * (2) Attempts to move the code from SimpleDateFormat to here also run * around because this requires SimpleDateFormat to keep a Locale * object around, which it currently doesn't; to synthesize such a * locale upon resurrection; and to somehow handle the special case of * construction from a DateFormatSymbols object. */ if (style != SHORT && style != LONG) { throw new IllegalArgumentException("Illegal style: " + style); } // We keep a cache, indexed by locale. The cache contains a // SimpleDateFormat object, which we create on demand. SoftReference data = (SoftReference)cachedLocaleData.get(locale); SimpleDateFormat format; if (data == null || (format = (SimpleDateFormat)data.get()) == null) { format = new SimpleDateFormat("", locale); cachedLocaleData.put(locale, new SoftReference(format)); } // Create a new SimpleTimeZone as a stand-in for this zone; the stand-in // will have no DST, or DST during January, but the same ID and offset, // and hence the same display name. We don't cache these because // they're small and cheap to create. SimpleTimeZone tz; if (daylight && useDaylightTime()) { int savings = getDSTSavings(); tz = new SimpleTimeZone(getRawOffset(), getID(), Calendar.JANUARY, 1, 0, 0, Calendar.FEBRUARY, 1, 0, 0, savings); } else { tz = new SimpleTimeZone(getRawOffset(), getID()); } format.applyPattern(style == LONG ? "zzzz" : "z"); format.setTimeZone(tz); // Format a date in January. We use the value 10*ONE_DAY == Jan 11 1970 // 0:00 GMT. return format.format(new Date(864000000L)); } /** * Returns the amount of time to be added to local standard time * to get local wall clock time. * <p> * The default implementation always returns 3600000 milliseconds * (i.e., one hour) if this time zone observes Daylight Saving * Time. Otherwise, 0 (zero) is returned. * <p> * If an underlying TimeZone implementation subclass supports * historical Daylight Saving Time changes, this method returns * the known latest daylight saving value. * * @return the amount of saving time in milliseconds * @since 1.4 */ public int getDSTSavings() { if (useDaylightTime()) { return 3600000; } return 0; } /** * Queries if this time zone uses daylight savings time. * <p> * If an underlying <code>TimeZone</code> implementation subclass * supports historical Daylight Saving Time schedule changes, the * method refers to the latest Daylight Saving Time schedule * information. * * @return true if this time zone uses daylight savings time, * false, otherwise. */ public abstract boolean useDaylightTime(); /** * Queries if the given date is in daylight savings time in * this time zone. * @param date the given Date. * @return true if the given date is in daylight savings time, * false, otherwise. */ abstract public boolean inDaylightTime(Date date); /** * Gets the <code>TimeZone</code> for the given ID. * * @param ID the ID for a <code>TimeZone</code>, either an abbreviation * such as "PST", a full name such as "America/Los_Angeles", or a custom * ID such as "GMT-8:00". Note that the support of abbreviations is * for JDK 1.1.x compatibility only and full names should be used. * * @return the specified <code>TimeZone</code>, or the GMT zone if the given ID * cannot be understood. */ public static synchronized TimeZone getTimeZone(String ID) { return getTimeZone(ID, true); } private static TimeZone getTimeZone(String ID, boolean fallback) { TimeZone tz = ZoneInfo.getTimeZone(ID); if (tz == null) { tz = parseCustomTimeZone(ID); if (tz == null && fallback) { tz = new ZoneInfo(GMT_ID, 0); } } return tz; } /** * Gets the available IDs according to the given time zone offset. * @param rawOffset the given time zone GMT offset. * @return an array of IDs, where the time zone for that ID has * the specified GMT offset. For example, "America/Phoenix" and "America/Denver" * both have GMT-07:00, but differ in daylight savings behavior. */ public static synchronized String[] getAvailableIDs(int rawOffset) { return ZoneInfo.getAvailableIDs(rawOffset); } /** * Gets all the available IDs supported. * @return an array of IDs. */ public static synchronized String[] getAvailableIDs() { return ZoneInfo.getAvailableIDs(); } /** * Gets the platform defined TimeZone ID. **/ private static native String getSystemTimeZoneID(String javaHome, String country); /** * Gets the custom time zone ID based on the GMT offset of the * platform. (e.g., "GMT+08:00") */ private static native String getSystemGMTOffsetID(); /** * Gets the default <code>TimeZone</code> for this host. * The source of the default <code>TimeZone</code> * may vary with implementation. * @return a default <code>TimeZone</code>. * @see #setDefault */ public static synchronized TimeZone getDefault() { out: if (defaultZone == null) { // get the time zone ID from the system properties String zoneID = (String) 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.equals("")) { String country = (String) AccessController.doPrivileged( new GetPropertyAction("user.country")); String javaHome = (String) AccessController.doPrivileged( new GetPropertyAction("java.home")); try { zoneID = getSystemTimeZoneID(javaHome, country); 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. defaultZone = getTimeZone(zoneID, false); if (defaultZone == 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; } } final String id = zoneID; AccessController.doPrivileged(new PrivilegedAction() { public Object run() { System.setProperty("user.timezone", id); return null; } }); if (defaultZone != null) { break out; } } defaultZone = getTimeZone(zoneID, true); } return (TimeZone) defaultZone.clone(); } /** * Sets the <code>TimeZone</code> that is * returned by the <code>getDefault</code> method. If <code>zone</code> * is null, reset the default to the value it had originally when the * VM first started. * @param zone the new default time zone * @see #getDefault */ public static synchronized void setDefault(TimeZone zone) { defaultZone = zone; } /** * Returns true if this zone has the same rule and offset as another zone. * That is, if this zone differs only in ID, if at all. Returns false * if the other zone is null. * @param other the <code>TimeZone</code> object to be compared with * @return true if the other zone is not null and is the same as this one, * with the possible exception of the ID * @since 1.2 */ public boolean hasSameRules(TimeZone other) { return other != null && getRawOffset() == other.getRawOffset() && useDaylightTime() == other.useDaylightTime(); } /** * Creates a copy of this <code>TimeZone</code>. * * @return a clone of this <code>TimeZone</code> */ public Object clone() { try { TimeZone other = (TimeZone) super.clone(); other.ID = ID; return other; } catch (CloneNotSupportedException e) { throw new InternalError(); } } // =======================privates=============================== /** * The string identifier of this <code>TimeZone</code>. This is a * programmatic identifier used internally to look up <code>TimeZone</code> * objects from the system table and also to map them to their localized * display names. <code>ID</code> values are unique in the system * table but may not be for dynamically created zones. * @serial */ private String ID; private static TimeZone defaultZone = null; static final String GMT_ID = "GMT"; private static final int GMT_ID_LENGTH = 3; /** * Parses a custom time zone identifier and returns a corresponding zone. * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm) * * @param id a string of the <a href="#CustomID">custom ID form</a>. * @return a newly created TimeZone with the given offset and * no daylight saving time, or null if the id cannot be parsed. */ private static final TimeZone parseCustomTimeZone(String id) { int length; // Error if the length of id isn't long enough or id doesn't // start with "GMT". if ((length = id.length()) < (GMT_ID_LENGTH + 2) || id.indexOf(GMT_ID) != 0) { return null; } ZoneInfo zi; // First, we try to find it in the cache with the given // id. Even the id is not normalized, the returned ZoneInfo // should have its normalized id. zi = ZoneInfoFile.getZoneInfo(id); if (zi != null) { return zi; } int index = GMT_ID_LENGTH; boolean negative = false; char c = id.charAt(index++); if (c == '-') { negative = true; } else if (c != '+') { return null; } int hours = 0; int num = 0; int countDelim = 0; int len = 0; while (index < length) { c = id.charAt(index++); if (c == ':') { if (countDelim > 0) { return null; } if (len > 2) { return null; } hours = num; countDelim++; num = 0; len = 0; continue; } if (c < '0' || c > '9') { return null; } num = num * 10 + (c - '0'); len++; } if (index != length) { return null; } if (countDelim == 0) { if (len <= 2) { hours = num; num = 0; } else { hours = num / 100; num %= 100; } } else { if (len != 2) { return null; } } if (hours > 23 || num > 59) { return null; } int gmtOffset = hours * 60 + num; if (gmtOffset == 0) { zi = ZoneInfoFile.getZoneInfo(GMT_ID); if (negative) { zi.setID("GMT-00:00"); } else { zi.setID("GMT+00:00"); } } else { zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset); } return zi; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?