简体   繁体   English

Ruby时区字符串到Java时区

[英]Ruby Timezone String to java Timezone

I have a ruby process that writes to DB timezones according to ruby TimeZone Class so at the table I get strings such as: 我有一个ruby进程根据ruby TimeZone Class写入数据库时​​区,因此在表上我得到如下字符串:

  • "Pacific Time (US & Canada)" “太平洋时间(美国和加拿大)”
  • "Eastern Time (US & Canada)" “东部时间(美国和加拿大)”
  • "UTC" “世界标准时间”
  • "Paris" “巴黎”

and so on .. Is there a way (not hard coded map) to read those strings and convert them into java Timezone instance ? 等等。是否有一种方法(不是硬编码映射)读取这些字符串并将其转换为Java Timezone实例

The authoritative source for Rails time zone names to standard TZDB/IANA time zone identifiers is the TimeZone.MAPPING constant in the Rails source code here . 用于标准TZDB / IANA时区标识符的Rails时区名称的权威来源是此处的Rails源代码中的TimeZone.MAPPING常量。 If you are able to access this mapping in your Rails code, then you don't need to hardcode anything. 如果您能够在Rails代码中访问此映射,则无需进行任何硬编码。

If rather (as it appears in the question comments) that you need to do the translation in your Java code, then you'll have to parse this mapping from the Rails code into something usable in your Java code, and periodically check for changes. 如果宁愿(如在问题注释中所显示的那样)需要在Java代码中进行翻译,则必须将此映射从Rails代码解析为Java代码中可用的内容,并定期检查更改。 Thankfully, the Rails developers don't seem to change this list often. 幸运的是,Rails开发人员似乎并不经常更改此列表。

I don't know of a Java library that already does this for you, but .NET developers can take advantage of my TimeZoneConverter library, which imports this mapping. 我不知道已经为您完成此操作的Java库,但是.NET开发人员可以利用我的TimeZoneConverter库,该库将导入此映射。 If you want to write your own code for this, feel free to translate my LoadRailsMapping function from C# to Java. 如果要为此编写自己的代码,请随时将我的LoadRailsMapping函数从C#转换为Java。

See also my notes on Rails time zone names at the bottom of the timezone tag wiki . 另请参阅我在时区标签Wiki底部有关Rails时区名称的注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM