问题描述
它返回GMT的默认时区.
It returns the default timezone for GMT.
使用时区为"z"的SimpleTimeFormat,它会显示"PDT".但是"PDT"不在TimeZone.getAvailableIDs()返回的列表中.真奇怪!
Using the SimpleTimeFormat with "z" for timezone, it prints "PDT". But "PDT" is not in the list returned from TimeZone.getAvailableIDs(). Really strange!
任何人都知道为什么"PDT"不是标准的tz吗?怎么处理呢?有人调用我的API,并将"PDT"作为时区传递.谢谢.
Anyone knows why "PDT" is not a standard tz? How to deal with this? Someone invokes my API passing in "PDT" as the timezone. Thanks.
推荐答案
PDT
不是时区
因为"PDT"不是时区!
Because "PDT" is not a time zone!
"PDT"是一种伪时区,媒体使用它来伪装地指示一组时区以及一个指标,如果它们打算在夏令时(DST)是否已启用( PST
).避免使用这2-4个字母代码,因为它们不是真实的时区,不是标准化的,甚至也不是唯一的(!).
The "PDT" is a pseudo-time zone used by the media to indicate vaguely a set of time zones plus an indicator if they intended during the period when Daylight Saving Time (DST) is engaged or not (PST
). Avoid these 2-4 letter codes as they are not true time zones, not standardized, and are not even unique(!).
以 continent/region 指定正确的时区名称./code>,例如
美国/蒙特利尔
, 非洲/卡萨布兰卡
或 Pacific/Auckland
.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
.
通过 PDT
可以使用以下任何时区,以及更多时区:
By PDT
any of these time zones, and more, may be intended:
ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;
ZoneId z = ZoneId.of( "America/Tijuana" ) ;
ZoneId z = ZoneId.of( "America/Whitehorse" ) ;
但是 PDT
可能并不意味着该区域,因为亚利桑那州不参与日光节约时间(DST)的废话,而中间的 D
则表示DST.
But PDT
might not mean this zone as Arizona does not participate in the Daylight Saving Time (DST) nonsense, and the D
in the middle means DST.
ZoneId z = ZoneId.of( "America/Phoenix" ) ;
避免使用旧的日期时间类
避免使用 SimpleTimeFormat
类,因为它是麻烦的旧日期时间类的一部分,该类现在已被遗留,由java.time类取代.使用 DateTimeFormatter
.
Avoid legacy date-time classes
Avoid SimpleTimeFormat
class as it is a part of the troublesome old date-time classes that are now legacy, supplanted by the java.time classes. Use DateTimeFormatter
instead.
也避免使用 TimeZone
.替换为 ZoneId
.
Avoid TimeZone
as well. Replaced by ZoneId
.
更改您的API,以使用 ZoneId
作为参数,而不是单纯的String.这样可以确保有效值,并为您提供类型安全.
Change your API to take a ZoneId
as an argument, rather than a mere String. That ensures valid values and gives you type-safety.
java.time 框架已内置在Java 8及更高版本中.这些类取代了麻烦的旧版日期时间类,例如 java.util.Date
, Calendar
,& SimpleDateFormat
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
Joda-Time 项目,现在位于维护模式,建议迁移到 java.time 类.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,请参见 Oracle教程.并在Stack Overflow中搜索许多示例和说明.规范为 JSR 310 .
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
在哪里获取java.time类?
Where to obtain the java.time classes?
- Java SE 8 , Java SE 9 和更高版本
- 内置.
- 具有捆绑的实现的标准Java API的一部分.
- Java 9添加了一些次要功能和修复.
- 许多java.time功能都向后移植到Java 6&中.7在> ThreeTen-Backport 中./li>
- > ThreeTenABP 项目适应了 ThreeTen-Backport (上面提到).
- 请参阅 如何使用ThreeTenABP… .
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
> ThreeTen-Extra 项目扩展了java.time与其他班级.该项目为将来可能在java.time中添加内容提供了一个试验场.您可能会在这里找到一些有用的类,例如
间隔
,年周"
,YearQuarter
和更多这篇关于Java TimeZone.getTimeZone("PDT")不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!