public abstract class TimeZone extends Object implements Serializable, Cloneable
TimeZone表示时区偏移量,也可以显示夏令时。
通常,您可以使用TimeZone使用getDefault ,它根据程序运行的时区创建一个TimeZone 。 例如,对于在日本运行的程序, getDefault基于日本标准时间创建一个TimeZone对象。
您还可以得到一个TimeZone使用getTimeZone具有时区ID一起。 例如,美国太平洋时区的时区ID为“America / Los_Angeles”。 所以,你可以得到美国太平洋时间TimeZone对象:
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
您可以使用getAvailableIDs方法遍历所有支持的时区ID。
然后,您可以选择一个支持的ID来获取一个TimeZone 。
如果您希望的时区不是由支持的ID之一表示,则可以指定自定义时区ID以生成TimeZone。
自定义时区ID的语法为:
小时数必须在0到23之间, 分钟必须介于00到59之间。例如,“GMT + 10”和“GMT + 0010”分别表示GMT前10小时10分钟。CustomID:GMTSign Hours:MinutesGMTSign Hours MinutesGMTSign Hours Sign: one of+ -Hours: Digit Digit Digit Minutes: Digit Digit Digit: one of0 1 2 3 4 5 6 7 8 9
格式与区域设置无关,数字必须取自Unicode标准的基本拉丁方块。 不能使用自定义时区ID指定夏令时转换时间表。 如果指定的字符串与语法"GMT"则使用"GMT" 。
创建TimeZone时,指定的自定义时区ID按以下语法进行标准化:
例如,TimeZone.getTimeZone(“GMT-8”)。getID()返回“GMT-08:00”。NormalizedCustomID:GMTSign TwoDigitHours:Minutes Sign: one of+ -TwoDigitHours: Digit Digit Minutes: Digit Digit Digit: one of0 1 2 3 4 5 6 7 8 9
Calendar , GregorianCalendar , SimpleTimeZone , Serialized Form
| Modifier and Type | Field and Description |
|---|---|
static int |
LONG
的风格说明
getDisplayName()指出长名称,如“太平洋标准时间”。
|
static int |
SHORT
的风格说明
getDisplayName()指出短名称,如“PST”。
|
| Constructor and Description |
|---|
TimeZone()
唯一的构造函数。
|
| Modifier and Type | Method and Description |
|---|---|
Object |
clone()
创建一个这个
TimeZone的副本。
|
static String[] |
getAvailableIDs()
获取支持的所有可用ID。
|
static String[] |
getAvailableIDs(int rawOffset)
根据给定的时区偏移(以毫秒为单位)获取可用ID。
|
static TimeZone |
getDefault()
获取Java虚拟机的默认值
TimeZone 。
|
String |
getDisplayName()
返回此
TimeZone的长标准时间名称,适合在默认语言环境中呈现给用户。
|
String |
getDisplayName(boolean daylight, int style)
返回此style
style的
TimeZone适合以默认语言环境呈现给用户的名称。
|
String |
getDisplayName(boolean daylight, int style, Locale locale)
返回指定的名称
style如此
TimeZone适合于展示给指定的用户
locale 。
|
String |
getDisplayName(Locale locale)
返回此
TimeZone标准时间长的名称,适用于在指定的locale中向用户
locale 。
|
int |
getDSTSavings()
返回添加到本地标准时间以获取本地挂钟时间的时间量。
|
String |
getID()
获取此时区的ID。
|
abstract int |
getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds)
获取当前日期的时区偏移,在夏令时情况下进行修改。
|
int |
getOffset(long date)
在指定的日期返回此时区与UTC的偏移量。
|
abstract int |
getRawOffset()
返回添加到UTC的时间(以毫秒为单位),以获得此时区的标准时间。
|
static TimeZone |
getTimeZone(String ID)
获取给定ID的
TimeZone 。
|
static TimeZone |
getTimeZone(ZoneId zoneId)
获取
TimeZone对于给定
zoneId 。
|
boolean |
hasSameRules(TimeZone other)
如果此区域具有与其他区域相同的规则和偏移量,则返回true。
|
abstract boolean |
inDaylightTime(Date date)
查询给定的
date是否在此时区的夏时制。
|
boolean |
observesDaylightTime()
如果
TimeZone当前处于夏令时,或者如果从将来的时间发生从标准时间到夏令时的转换,则返回
true 。
|
static void |
setDefault(TimeZone zone)
设置
TimeZone由返回
getDefault方法。
|
void |
setID(String ID)
设置时区ID。
|
abstract void |
setRawOffset(int offsetMillis)
将基准时区偏移设置为GMT。
|
ZoneId |
toZoneId()
将此
TimeZone对象转换为
ZoneId 。
|
abstract boolean |
useDaylightTime()
查询,如果这
TimeZone使用夏令时。
|
public static final int SHORT
getDisplayName()指出短名称,如“PST”。
LONG ,
Constant Field Values
public static final int LONG
getDisplayName()指出长名称,如“太平洋标准时间”。
SHORT ,
Constant Field Values
public abstract int getOffset(int era,
int year,
int month,
int day,
int dayOfWeek,
int milliseconds)
如果基础TimeZone实现子类支持历史夏令时计划和GMT偏移量更改,则此方法返回历史正确的偏移量。
era - 给定日期的时代。
year - 给定日期的年份。
month - 给定日期的月份。
月份为0。
例如,1月份为0。
day - 给定日期的日期。
dayOfWeek - 给定日期的星期几。
milliseconds -
标准当地时间的毫秒数。
Calendar.ZONE_OFFSET ,
Calendar.DST_OFFSET
public int getOffset(long date)
如果底层TimeZone实现子类支持历史夏令时计划和GMT偏移量更改,则此方法返回历史上正确的偏移值。
date - 自1970年1月1日00:00:00 GMT以毫秒表示的日期
Calendar.ZONE_OFFSET ,
Calendar.DST_OFFSET
public abstract void setRawOffset(int offsetMillis)
如果底层TimeZone实现子类支持历史GMT偏移量更改,则将指定的GMT偏移量设置为最新的GMT偏移量,并使用与已知最新GMT偏移值的差值来调整所有历史GMT偏移值。
offsetMillis - 给定的基准时区偏移到GMT。
public abstract int getRawOffset()
如果基础TimeZone实现子类支持历史GMT偏移量更改,则该方法返回当前日期的原始偏移值。 例如,在檀香山,其原始偏移量在1947年从GMT-10:30改为GMT-10:00,该方法总是返回-36000000毫秒(即-10小时)。
Calendar.ZONE_OFFSET
public String getID()
public void setID(String ID)
ID - 新的时区ID。
public final String getDisplayName()
TimeZone的较长标准时间名称,适用于以默认语言环境向用户呈现。
此方法相当于:
getDisplayName(false,LONG, Locale.getDefault(Locale.Category.DISPLAY))
getDisplayName(boolean, int, Locale) , Locale.getDefault(Locale.Category) , Locale.Category
public final String getDisplayName(Locale locale)
locale - 提供显示名称的区域设置。
NullPointerException - 如果
locale是
null 。
getDisplayName(boolean, int, Locale)
public final String getDisplayName(boolean daylight, int style)
TimeZone适用于以默认语言环境向用户style的名称。
如果指定的daylight为true ,则返回夏令时名称(即使TimeZone未遵守夏令时)。
否则返回标准时间名称。
此方法相当于:
getDisplayName(daylight, style,
Locale.getDefault(Locale.Category.DISPLAY))
daylight -
true指定夏令时名称,或
false标准时间名称的false
style -
LONG或
SHORT
IllegalArgumentException - 如果
style无效。
getDisplayName(boolean, int, Locale) , Locale.getDefault(Locale.Category) , Locale.Category , DateFormatSymbols.getZoneStrings()
public String getDisplayName(boolean daylight, int style, Locale locale)
style如此TimeZone适合于展示给指定的用户locale 。
如果指定的daylight为true ,则返回夏令时(即使TimeZone未遵守夏令时)。
否则返回标准时间名称。
当寻找一个时区名称, default Locale search path of ResourceBundle从指定的衍生locale使用。 (无fallback Locale执行搜索。)如果在任何一个时区名称Locale搜索路径,包括Locale.ROOT被发现,被返回的名称。 否则,在一个字符串normalized custom ID format返回。
daylight -
true指定夏令时名称,或
false标准时间名称的false
style -
LONG或
SHORT
locale - 提供显示名称的区域设置。
IllegalArgumentException - 如果
style无效。
NullPointerException - 如果
locale是
null 。
DateFormatSymbols.getZoneStrings()
public int getDSTSavings()
如果调用useDaylightTime()返回true则默认实现返回3600000毫秒(即一小时)。 否则返回0(零)。
如果底层的TimeZone实现子类支持历史和未来的夏令时计划更改,则此方法返回最后一个已知的夏令时规则的可用时间预留量。
如果需要任何给定时间戳的节省时间, 请使用此TimeZone和时间戳构建 ( ,并致电Calendar.get ( Calendar.DST_OFFSET ) 。
inDaylightTime(Date) ,
getOffset(long) ,
getOffset(int,int,int,int,int,int) ,
Calendar.ZONE_OFFSET
public abstract boolean useDaylightTime()
TimeZone使用夏令时。
如果基础TimeZone实现子类支持历史和未来的夏令时计划更改,则该方法参考最后一个已知的夏令时规则,该规则可以是将来的预测,并且可能与当前规则不同。 考虑调用observesDaylightTime(),如果当前规则也应该考虑在内。
true如果这
TimeZone使用夏令时,
false ,否则。
inDaylightTime(Date) ,
Calendar.DST_OFFSET
public boolean observesDaylightTime()
TimeZone当前处于夏令时,或者如果从将来的时间发生从标准时间到夏令时的转换,则返回true 。
默认实现返回true如果useDaylightTime()或inDaylightTime(new Date())回报true 。
true如果此TimeZone当前处于夏令时,或者如果在任何将来的时间发生从标准时间到夏令时的过渡;
false否则。
useDaylightTime() ,
inDaylightTime(Date) ,
Calendar.DST_OFFSET
public abstract boolean inDaylightTime(Date date)
date是否在此时区的夏时制。
date - 给定的日期。
true如果给定的日期是在夏令时,
false ,否则。
public static TimeZone getTimeZone(String ID)
TimeZone 。
ID -的ID为TimeZone ,要么的缩写如“PST”,一个完整的名称,如“美国/洛杉矶”,或自定义ID,如“GMT-8:00”。
请注意,缩写的支持仅适用于JDK 1.1.x兼容性,应使用全名。
TimeZone或GMT区域,如果给定的ID不能被理解。
public static TimeZone getTimeZone(ZoneId zoneId)
TimeZone对于给定
zoneId 。
zoneId - 一个ZoneId ,从中获得时区ID
TimeZone或GMT区域,如果给定的ID不能被理解。
NullPointerException - 如果
zoneId是
null
public ZoneId toZoneId()
TimeZone对象转换为
ZoneId 。
ZoneId表示相同的时区,因为这
TimeZone
public static String[] getAvailableIDs(int rawOffset)
rawOffset - 以
rawOffset为单位的给定时区GMT偏移量。
getRawOffset()
public static String[] getAvailableIDs()
public static TimeZone getDefault()
TimeZone 。
如果缓存默认值为TimeZone可用,则返回其克隆。
否则,该方法将采取以下步骤来确定默认时区。
user.timezone属性值作为默认时区ID。 GMT作为最后的手段。 从ID创建的默认值TimeZone被缓存,并且返回其克隆。 user.timezone属性值返回时设置为ID。
TimeZone
setDefault(TimeZone)
public static void setDefault(TimeZone zone)
TimeZone由返回getDefault方法。
zone被缓存。
如果zone为空,缓存默认值TimeZone将被清除。
此方法不会更改user.timezone属性的值。
zone - 新的默认值
TimeZone ,或null
SecurityException - 如果安全管理员的
checkPermission否认
PropertyPermission("user.timezone", "write")
getDefault() , PropertyPermission
public boolean hasSameRules(TimeZone other)
other - 要比较的
TimeZone对象
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.