TZ Var 到 Java 时区? [英] TZ Var to Java TimeZone?

查看:20
本文介绍了TZ Var 到 Java 时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从形式的 Unix TZ 环境变量转换:

I need to convert from the Unix TZ environment variable of the form:

stdoffset[dst[offset][,start[/time],end[/time]]]

转换为 Java TimeZone 对象.

into a Java TimeZone object.

例如,将 AEST-10AEDT-11,M10.1.0/02:00:00,M4.1.0/03:00:00 转换为代表Australia\Sydney"的 Java TimeZone 对象".在我开始自己编写代码之前,我想知道我不会再发明轮子了,那么有谁知道是否有可用的库可以做到这一点?

For example, convert AEST-10AEDT-11,M10.1.0/02:00:00,M4.1.0/03:00:00 into a Java TimeZone object that represents "Australia\Sydney". Before I go and write the code myself I'd like to know I'm not inventing the wheel again, so does anyone know if there's a library already available that can do this?

推荐答案

编写这样的转换程序真的很难.在 Unix 上,您可以自定义日光规则,而 Java 不提供此类功能.可以通过 tzfile 搜索尝试找到匹配项,但您必须将 TZ 规则转换为 tzfile 使用的格式,这是一项艰巨的任务.

It will be really hard to write such a conversion program. On Unix, you can have customized daylight rules and Java provides no such facility. It's possible to search through tzfile trying to find a match but you have to convert the TZ rule into the format used by tzfile, an enormous task.

为什么不使用 Java 中使用的相同区域 ID?例如,您可以将 TZ 设置为,

Why don't you use the same zone id used in Java? For example, you can set TZ as,

出口 TZ=澳大利亚/悉尼

export TZ=Australia/Sydney

大多数现代 Unix 系统(如 Linux、Mac OS X)都支持这一点.搜索 /etc/zoneinfo/usr/share/zoneinfo.如果您可以找到适合您所在时区的文件,则该 ID 将起作用.

This is supported on most modern Unix systems (like Linux, Mac OS X). Search /etc/zoneinfo or /usr/share/zoneinfo. If you can find a file for your timezone, the ID will work.

我也开始使用自定义 TimeZone 来处理旧的 TZ 格式,但我能够说服系统管理员使用区域 ID,所以我没有完成它.难点在于实现useDaylightTime(),因为TZ中使用的规则非常复杂.

I also started on a custom TimeZone to handle the old TZ format but I was able to convince the sysadmin to use the zone id so I didn't finish it. The difficult part is to implement the useDaylightTime() because the rules used in TZ is very complicated.

这篇关于TZ Var 到 Java 时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆