当我写这段代码时:
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("EST"));
System.out.println(cal.getTimeZone().getDisplayName());
输出是
Eastern Standard Time
但是当我写这段代码时:
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("est"));
System.out.println(cal.getTimeZone().getDisplayName());
我得到的输出是:
GMT-05:00
在设置 TimeZone.setTimeZone(String str)
时给出诸如“EST”和“est”之类的参数有什么区别吗(str
是否在考虑调用时传递区分大小写)?
API 没有提及任何相关内容:
getTimeZone
public static TimeZone getTimeZone(String ID)
Gets the TimeZone for the given ID.
Parameters:
ID - the ID for a TimeZone, 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.
Returns:
the specified TimeZone, or the GMT zone if the given ID cannot be understood.
注意:我尝试使用 IST
和 ist
字符串。对于 IST
字符串,它给出了 Indian Standard Time
,对于 ist
,它给出了 Greenwich Mean Time
最佳答案
简而言之,是的,它区分大小写。
按照您的示例 ist
为您提供 GMT
因为找不到具有此类 ID 的时区,因此为您提供默认结果
它适用于 est
(GMT-05:00
是 EASTERN STANDARD TIME
),当然是因为两个 ID 都是已知的,但我不会'指望它(不太确定如果更改平台它是否仍然存在)。
此外,根据 API 的指示,您不应使用这些缩写 ID,而应直接使用全名或自定义 ID。
您可以使用 TimeZone.getAvailableIDs()
获得平台可用 ID 的列表,然后您可以选择正确的 ID。
我自己会考虑使用 GMT-5:00
格式,在我看来这种格式更易读且不易出错。
关于java - 是 TimeZone.setTimeZone ("est") 不同于 TimeZone.setTimeZone ("EST"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13230937/