📄 timezone.java
字号:
*/ public abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds); /** * Get the time zone offset for the specified date, modified in case of * daylight savings. This is the offset to add to UTC to get the local * time. * @param date the date represented in millisecends * since January 1, 1970 00:00:00 GMT. * @since 1.4 */ public int getOffset(long date) { return (inDaylightTime(new Date(date)) ? getRawOffset() + getDSTSavings() : getRawOffset()); } /** * Gets the time zone offset, ignoring daylight savings. This is * the offset to add to UTC to get the local time. * @return the time zone offset in milliseconds. */ public abstract int getRawOffset(); /** * Sets the time zone offset, ignoring daylight savings. This is * the offset to add to UTC to get the local time. * @param offsetMillis the time zone offset to GMT. */ public abstract void setRawOffset(int offsetMillis); /** * Gets the identifier of this time zone. For instance, PST for * Pacific Standard Time. * @returns the ID of this time zone. */ public String getID() { return ID; } /** * Sets the identifier of this time zone. For instance, PST for * Pacific Standard Time. * @param id the new time zone ID. * @throws NullPointerException if <code>id</code> is <code>null</code> */ public void setID(String id) { if (id == null) throw new NullPointerException(); this.ID = id; } /** * This method returns a string name of the time zone suitable * for displaying to the user. The string returned will be the long * description of the timezone in the current locale. The name * displayed will assume daylight savings time is not in effect. * * @return The name of the time zone. */ public final String getDisplayName() { return (getDisplayName(false, LONG, Locale.getDefault())); } /** * This method returns a string name of the time zone suitable * for displaying to the user. The string returned will be the long * description of the timezone in the specified locale. The name * displayed will assume daylight savings time is not in effect. * * @param locale The locale for this timezone name. * * @return The name of the time zone. */ public final String getDisplayName(Locale locale) { return (getDisplayName(false, LONG, locale)); } /** * This method returns a string name of the time zone suitable * for displaying to the user. The string returned will be of the * specified type in the current locale. * * @param dst Whether or not daylight savings time is in effect. * @param style <code>LONG</code> for a long name, <code>SHORT</code> for * a short abbreviation. * * @return The name of the time zone. */ public final String getDisplayName(boolean dst, int style) { return (getDisplayName(dst, style, Locale.getDefault())); } /** * This method returns a string name of the time zone suitable * for displaying to the user. The string returned will be of the * specified type in the specified locale. * * @param dst Whether or not daylight savings time is in effect. * @param style <code>LONG</code> for a long name, <code>SHORT</code> for * a short abbreviation. * @param locale The locale for this timezone name. * * @return The name of the time zone. */ public String getDisplayName(boolean dst, int style, Locale locale) { DateFormatSymbols dfs; try { dfs = new DateFormatSymbols(locale); // The format of the value returned is defined by us. String[][]zoneinfo = dfs.getZoneStrings(); for (int i = 0; i < zoneinfo.length; i++) { if (zoneinfo[i][0].equals(getID())) { if (!dst) { if (style == SHORT) return (zoneinfo[i][2]); else return (zoneinfo[i][1]); } else { if (style == SHORT) return (zoneinfo[i][4]); else return (zoneinfo[i][3]); } } } } catch (MissingResourceException e) { } return getDefaultDisplayName(dst); } private String getDefaultDisplayName(boolean dst) { int offset = getRawOffset(); if (dst && this instanceof SimpleTimeZone) { // ugly, but this is a design failure of the API: // getDisplayName takes a dst parameter even though // TimeZone knows nothing about daylight saving offsets. offset += ((SimpleTimeZone) this).getDSTSavings(); } StringBuffer sb = new StringBuffer(9); sb.append("GMT"); offset = offset / (1000 * 60); int hours = Math.abs(offset) / 60; int minutes = Math.abs(offset) % 60; if (minutes != 0 || hours != 0) { sb.append(offset >= 0 ? '+' : '-'); sb.append((char) ('0' + hours / 10)); sb.append((char) ('0' + hours % 10)); sb.append(':'); sb.append((char) ('0' + minutes / 10)); sb.append((char) ('0' + minutes % 10)); } return sb.toString(); } /** * Returns true, if this time zone uses Daylight Savings Time. */ public abstract boolean useDaylightTime(); /** * Returns true, if the given date is in Daylight Savings Time in this * time zone. * @param date the given Date. */ public abstract boolean inDaylightTime(Date date); /** * Gets the daylight savings offset. This is a positive offset in * milliseconds with respect to standard time. Typically this * is one hour, but for some time zones this may be half an our. * <p>The default implementation returns 3600000 milliseconds * (one hour) if the time zone uses daylight savings time * (as specified by {@link #useDaylightTime()}), otherwise * it returns 0. * @return the daylight savings offset in milliseconds. * @since 1.4 */ public int getDSTSavings () { return useDaylightTime () ? 3600000 : 0; } /** * Gets the TimeZone for the given ID. * @param ID the time zone identifier. * @return The time zone for the identifier or GMT, if no such time * zone exists. */ // FIXME: XXX: JCL indicates this and other methods are synchronized. public static TimeZone getTimeZone(String ID) { // First check timezones hash TimeZone tz = (TimeZone) timezones().get(ID); if (tz != null) { if (tz.getID().equals(ID)) return tz; // We always return a timezone with the requested ID. // This is the same behaviour as with JDK1.2. tz = (TimeZone) tz.clone(); tz.setID(ID); // We also save the alias, so that we return the same // object again if getTimeZone is called with the same // alias. timezones().put(ID, tz); return tz; } // See if the ID is really a GMT offset form. // Note that GMT is in the table so we know it is different. if (ID.startsWith("GMT")) { int pos = 3; int offset_direction = 1; if (ID.charAt(pos) == '-') { offset_direction = -1; pos++; } else if (ID.charAt(pos) == '+') { pos++; } try { int hour, minute; String offset_str = ID.substring(pos); int idx = offset_str.indexOf(":"); if (idx != -1) { hour = Integer.parseInt(offset_str.substring(0, idx)); minute = Integer.parseInt(offset_str.substring(idx + 1)); } else { int offset_length = offset_str.length(); if (offset_length <= 2) { // Only hour hour = Integer.parseInt(offset_str); minute = 0; } else { // hour and minute, not separated by colon hour = Integer.parseInt (offset_str.substring(0, offset_length - 2)); minute = Integer.parseInt (offset_str.substring(offset_length - 2)); } } return new SimpleTimeZone((hour * (60 * 60 * 1000) + minute * (60 * 1000)) * offset_direction, ID); } catch (NumberFormatException e) { } } // Finally, return GMT per spec return getTimeZone("GMT"); } /** * 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 has the specified GMT * offset. For example <code>{"Phoenix", "Denver"}</code>, since both have * GMT-07:00, but differ in daylight savings behaviour. */ public static String[] getAvailableIDs(int rawOffset) { int count = 0; Iterator iter = timezones().entrySet().iterator(); while (iter.hasNext()) { // Don't iterate the values, since we want to count // doubled values (aliases) Map.Entry entry = (Map.Entry) iter.next(); if (((TimeZone) entry.getValue()).getRawOffset() == rawOffset) count++; } String[] ids = new String[count]; count = 0; iter = timezones().entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); if (((TimeZone) entry.getValue()).getRawOffset() == rawOffset) ids[count++] = (String) entry.getKey(); } return ids; } /** * Gets all available IDs. * @return An array of all supported IDs. */ public static String[] getAvailableIDs() { return (String[]) timezones().keySet().toArray(new String[timezones().size()]); } /** * Returns the time zone under which the host is running. This * can be changed with setDefault. * * @return A clone of the current default time zone for this host. * @see #setDefault */ public static TimeZone getDefault() { return (TimeZone) defaultZone().clone(); } public static void setDefault(TimeZone zone) { // Hmmmm. No Security checks? defaultZone0 = zone; } /** * Test if the other time zone uses the same rule and only * possibly differs in ID. This implementation for this particular * class will return true if the raw offsets are identical. Subclasses * should override this method if they use daylight savings. * @return true if this zone has the same raw offset */ public boolean hasSameRules(TimeZone other) { return other.getRawOffset() == getRawOffset(); } /** * Returns a clone of this object. I can't imagine, why this is * useful for a time zone. */ public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -