如何在 Windows(非 Java)应用程序中使用 Java 时区 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/583141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:02:51  来源:igfitidea点击:

How to use Java timezone id in a Windows (non-Java) application?

javawindowsoracletimezone

提问by Werner Lehmann

I need to add timezone information to a db table with user maintained locations. The data will be accessed mostly from Java code but there is also some PL/SQL and Win32 (Delphi) code which needs to understand the timezone information.

我需要将时区信息添加到具有用户维护位置的数据库表中。数据将主要从 Java 代码访问,但也有一些 PL/SQL 和 Win32 (Delphi) 代码需要了解时区信息。

It seems straight forward to use the id from java.util.TimeZone. Java can easily convert that (obviously), Hibernate has built-in support for it and apparently also Oracle understands those timezone ids:

使用 java.util.TimeZone 中的 id 似乎很直接。Java 可以很容易地转换它(显然),Hibernate 内置了对它的支持,显然 Oracle 也理解这些时区 ID:

select TZ_OFFSET('Pacific/Marquesas') from dual.

The problem is: the timezone ids do not seem to be compatible with the Windows Timezone DB. For example, the java.util.timezone id "Pacific/Marquesas" (-09:30) is not in the timezone picklist in Windows. The registry does not contain it at all; see

问题是:时区 ID 似乎与 Windows 时区数据库不兼容。例如,java.util.timezone id "Pacific/Marquesas" (-09:30) 不在 Windows 的时区选择列表中。注册表根本不包含它;看

\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones

Here I can only pick either -09:00 or -10:00. So, if I were to store the timezone like this, how can I get to the actual offset/DST infos in Windows (without Java)? Hopefully this does not require a mapping table which I have to keep up to date whenever it changes. Is there a globally accepted standard which works better than the java timezone id?

在这里我只能选择 -09:00 或 -10:00。所以,如果我要像这样存储时区,我怎样才能在 Windows(没有 Java)中获得实际的偏移/夏令时信息?希望这不需要映射表,每当它发生变化时我都必须保持更新。是否有一个全球公认的标准比 java 时区 ID 更有效?

Update

更新

The timezone info is used in combination with DATE columns on the database. Those columns contain local date/time values. If a location can be associated with those values, the location's timezone enables me to convert the date/time value to UTC or any other timezone whenever needed.

时区信息与数据库上的 DATE 列结合使用。这些列包含本地日期/时间值。如果某个位置可以与这些值相关联,则该位置的时区使我能够在需要时将日期/时间值转换为 UTC 或任何其他时区。

I realize that instead of DATE a TIMESTAMP_TZ data type or something similar would be more appropriate. However, this would require a data migration (for which the TZ is required again) and is not supported by the legacy applications which also work on the data (unless a lot of code is changed). The problem is almost the same if I had to convert the values to UTC.

我意识到不是 DATE TIMESTAMP_TZ 数据类型或类似的东西会更合适。但是,这将需要数据迁移(为此再次需要 TZ)并且不受同样处理数据的遗留应用程序的支持(除非更改了大量代码)。如果我必须将值转换为 UTC,问题几乎是一样的。

Bottom line is I need to keep the DATE values in local time but I need to know for some of them which TZ that means.

底线是我需要将 DATE 值保留在本地时间,但我需要知道其中一些 TZ 意味着什么。

回答by John M

I can give a little background, if not a real answer.

如果不是真正的答案,我可以提供一些背景知识。

Many systems use the Olsonimplementation of timezone data. So those names work in many systems (most Unix, Java, Oracle I think). Microsoft does their own thing.

许多系统使用时区数据的Olson实现。所以这些名称在许多系统中都有效(我认为大多数 Unix、Java、Oracle)。微软做他们自己的事。

I see at the bottom of that Wikipedia link there's a reference to some mapping to the Windows world.

我在维基百科链接的底部看到了对 Windows 世界的一些映射的引用。

Good luck!

祝你好运!

回答by Sarel Botha

I realize this is not the best way to do it, but it might be sufficient in your case. Without knowing all the requirements I can't tell.

我意识到这不是最好的方法,但在您的情况下可能就足够了。在不知道所有要求的情况下,我无法判断。

What do you need to use the time zone information for? Just to present the time with the correct offset and maybe also the name of the time zone?

您需要使用时区信息做什么?只是为了显示具有正确偏移量的时间,也许还有时区的名称?

You could continue to use Java to determine what the offset of the user is by looking up the user's selected time zone using Java. Each time the user logs in record in your database what the offset currently is. Then other apps can look at this information to determine how to format the time.

您可以继续使用 Java 通过使用 Java 查找用户选择的时区来确定用户的偏移量。每次用户登录时,在您的数据库中记录当前的偏移量。然后其他应用程序可以查看此信息以确定如何格式化时间。

This assumes that users who regularly login are the ones that this needs to be done for. If that's not the case you could run a daily job to lookup the time zone for each user in Java and record the offset currently in effect.

这假设定期登录的用户是需要这样做的用户。如果不是这种情况,您可以运行每日作业来查找 Java 中每个用户的时区并记录当前有效的偏移量。

Hackish, agreed, but the only other way I see is to maintain a mapping. And what happens when someone selects a time zone that you don't have a mapping for?

Hackish,同意,但我看到的唯一其他方法是维护映射。当有人选择您没有映射的时区时会发生什么?