0

有没有办法从时区字符串表示创建(joda)DateTimeZone 对象,如“EDT”或“+0330”?

4

3 回答 3

5

或许值得指出的是,虽然 jodatime 不支持三字母时区标识符(“UTC”除外),java.util.TimeZone但它支持;您可以使用以下方法从 a创建一个DateTimeZone实例:TimeZoneDateTimeZone.forTimeZone

DateTimeZone.forTimeZone(TimeZone.getTimeZone("..."))

TimeZone也不支持“EDT”。来自 Javadoc:

为了与 JDK 1.1.x 兼容,还支持一些其他的三字母时区 ID(例如“PST”、“CTT”、“AST”)。但是,它们的使用已被弃用...

请注意“一些”:在撰写本文时,PDT 未列在TimeZone.getAvailableIDs()(至少在 ideone.com 上)。如果您尝试在此处使用“PDT”,您将得到 GMT 时区

[TimeZone.getTimeZone返回] 指定的时区,如果无法理解给定的 ID,则返回 GMT 时区。

所以最好避免这种方法,除非你能保证你只会尝试将它用于支持的时区标识符。

于 2017-01-11T08:00:05.540 回答
1

Joda API 不支持缩写时区名称(如“EDT”)的映射。

为什么?

因为他们是模棱两可的!例如,EDT 可以表示 UTC-4 或 UTC+11。

因此,如果您想实现映射,您需要决定如何映射名称,然后自己创建和填充映射。

来源: http ://www.timeanddate.com/library/abbreviations/timezones/

于 2013-03-25T14:04:42.093 回答
1

您可以使用此代码获取 DateTimeZone 对象 -

//User Defined
        DateTimeZone dtz = DateTimeZone.forID("America/New_York");
        //System Default TimeZone
        DateTimeZone dtzz = DateTimeZone.forID(TimeZone.getDefault().getID());
于 2013-03-25T11:29:37.580 回答