在Java中构建TimeZone对象

发布于 2024-10-08 20:55:57 字数 523 浏览 7 评论 0 原文

我使用 TimeZone 字符串(如 GMT-8、GMT、PST、EST 等)构建 Java TimeZone 对象。 这没有考虑时区是否启用夏令时。现在需要包含此检查,并且不是将输入作为 PDT 或 EDT,而是将夏令时作为单独的标志给出。我不确定 TimeZone 是否已启用更改该时区的夏令时属性的直接方法。

因此,如果我得到像 PST 和 DaylightSaving 这样的输入为 true,那么我必须将字符串更改为 PDT。更糟糕的是,有时我会得到像 GMT-8 或 GMT-6 这样的输入,其中 Daylight 标志为 true 或 false。有出路吗?

我无法使用第三方 TimeZone 相关类

代码示例:

TimeZone  timeZone = TimeZone.getTimeZone("EST");
TimeZone  timeZone = TimeZone.getTimeZone("PST");
TimeZone  timeZone = TimeZone.getTimeZone("GMT-8");

I build a Java TimeZone object using the TimeZone String like GMT-8,GMT,PST,EST etc.
This did not consider whether the timezone is daylight saving enabled or not.Now there is a requirement to include this check and instead of giving the input as PDT or EDT the daylight saving is given as a seperate flag.I am not sure whether TimeZone has a direct method to change the dayLight saving property of that TimeZone.

So if I get an input like PST and DaylightSaving as true then I have to change the string as PDT.Whats even worse is sometimes I will get inputs like GMT-8 or GMT-6 with Day light flag as either true or false. Is there a way out ?

I can't use thirdparty TimeZone related classes

Code Sample:

TimeZone  timeZone = TimeZone.getTimeZone("EST");
TimeZone  timeZone = TimeZone.getTimeZone("PST");
TimeZone  timeZone = TimeZone.getTimeZone("GMT-8");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

翻身的咸鱼 2024-10-15 20:55:57

“PST”和“GMT-8”等时区字符串通常不明确,并且通常不会告诉您夏令时规则是否有效。 (例如,“PST”既表示“太平洋标准时间”又表示“巴基斯坦标准时间”。)

如果您希望时区和夏令时规则正确,您必须使用完整的时区名称获取TimeZone对象;即形式的名称:“America/Los_Angeles”。

或者使用 ISO 8601 日期/时间值

必须更改大量代码,并且说服其他人获得批准并不困难。

好吧,我想说你在这里别无选择......如果你希望你的代码获得时区等正确。

做一些研究,了解问题,向老板/客户解释问题,并让他们在无法正确获取夏令时的应用程序和使用更合理的日期格式和时区规范的应用程序之间进行选择方法。

Timezone strings like "PST" and "GMT-8" are often ambiguous, and often do not tell you whether daylight savings rules are in force. (For instance, "PST" means both "Pacific Standard Time" and "Pakistan Standard Time".)

If you want to get the timezones and daylight saving rules correct, you have to use full timezone names to obtain the TimeZone object; i.e. names of the form: "America/Los_Angeles".

Alternatively use ISO 8601 date / time values.

Have to change lots of codes and its little difficult to convince others to get approvals.

Well, I'd say you've got little choice here ... if you want your code to get the timezones, etc correct.

Do some research, understand the problems, explain the problem your boss / client, and let them choose between an application that cannot get daylight savings time right, and one that uses a more sensible date format and timezone specification method.

独行侠 2024-10-15 20:55:57

我要补充的唯一一件事是,不推荐使用 3 个字母的时区 id,如下所示 JavaDoc。您需要使用此处所示的名称

三字母时区 ID

为了与 JDK 1.1.x 兼容,一些
其他三字母时区 ID(例如
如“PST”、“CTT”、“AST”)也是
支持。然而,它们的用途是
已弃用,因为相同
缩写通常用于
多个时区(例如,
“CST”可能是美国“中央标准”
时间”和“中国标准时间”),以及
Java 平台只能
认出其中之一。

The only other thing I would add is that use of 3 letter timezone id's is deprecated as shown below from JavaDoc. You need to use the names as shown here

Three-letter time zone IDs

For compatibility with JDK 1.1.x, some
other three-letter time zone IDs (such
as "PST", "CTT", "AST") are also
supported. However, their use is
deprecated because the same
abbreviation is often used for
multiple time zones (for example,
"CST" could be U.S. "Central Standard
Time" and "China Standard Time"), and
the Java platform can then only
recognize one of them.

仅此而已 2024-10-15 20:55:57

我认为 Java 应该自己弄清楚时区是否使用夏令时,以及何时启用。

因此,如果您尝试使用该时区格式化某个 Date 对象,输出应反映夏令时。

我不知道这是否可靠(考虑到该领域的频繁变化)。您可能应该更新到最新的 JVM 以获取最新的时区数据库文件(如果您无法做到这一点,可以使用 单独的时区更新工具可用)。

I think Java is supposed to figure out by itself, whether the time zone is using daylight savings or not, and when this is active.

So if you try to format a certain Date object with that timezone, the output should reflect the daylight savings.

Whether that works reliably (given the frequent changes in this area), I do not know. You should probably update to the latest JVM to get the newest timezone database files (if you cannot do that, there is a separate Time Zone Update tool available).

赠意 2024-10-15 20:55:57

您并没有真正使用您展示的示例代码“构建”时区。您正在获取已在 Java 平台中配置的一个。

这些内置 TimeZone 对象已经具有有关是否使用夏令时、何时开始等的适当信息。所有这些都是您要获取的时区的属性。

正如另一条评论中提到的,内置 TimeZone 对象的这些属性会不时发生变化。 Java 运行时的更新会带来这些更改。

如果您确实想真正构建一个新的 TimeZone 实例,可以使用子类 SimpleTimeZone 似乎有有用的构造函数。但真的不能使用内置时区吗?看来你正在承担比必要的更大的工作。 TimeZone.getAvailableIDs() 将为您提供 Java 运行时中的时区名称列表。

You're not really "building" a TimeZone with the example code you show. You're fetching one that's configured in the Java platform already.

And those built-in TimeZone objects already have the appropriate info about whether daylight saving time is used, when it starts, etc. All those are attributes of the timezone you're fetching.

As mentioned in another comment, those attributes of the built-in TimeZone objects change from time to time. Updates to the Java runtime pick up those changes.

If you did want to really build a new TimeZone instance, there's the subclass SimpleTimeZone that seems to have useful constructors. But is it truly the case that you can't use built-in TimeZones? Seems like you're taking on a bigger job than necessary. TimeZone.getAvailableIDs() will give you the list of TimeZone names in your Java runtime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文