简体   繁体   English

java-从ID获取TimeZone

[英]java- get TimeZone from ID

For getting timezone offset of Singapore, if I try "Singapore" as ID, I will get correct one as like below: 为了获得新加坡的时区偏移,如果我尝试“新加坡”作为ID,我会得到正确的,如下所示:

TimeZone timeZone = TimeZone.getTimeZone("Singapore");
    int offset = timeZone.getRawOffset();//It prints 28800000 which is correct

But If try SGT in place of Singapore as below, It will return offset of GMT because it does not understand SGT as id: 但是如果尝试SGT代替新加坡,如下所示,它将返回GMT的偏移,因为它不了解SGT作为id:

TimeZone timeZone = TimeZone.getTimeZone("SGT");
    int offset = timeZone.getRawOffset();//It prints 0 which is incorrect and should be 28800000 

Any way for getting correct offset for Singapore by using "SGT" in place of "Singapore"???? 通过使用“SGT”代替“新加坡”来获得新加坡正确抵消的任何方式????

Above issue is not produced for ID "America/Los_Angeles". 以上问题不适用于ID“America / Los_Angeles”。 "PST" and "America/Los_Angeles" returns same offset. “PST”和“America / Los_Angeles”返回相同的偏移量。

Regards 问候

EDITED-- EDITED--

If I have date like "2015-02-08 11:18 AM SGT" then ????? 如果我有“2015-02-08 11:18 AM SGT”的日期那么?????

Still am I not able to get offset of "Singapore" using above date? 我仍然无法使用上述日期抵消“新加坡”吗?

Trying to get how I can make possible. 试着让我如何做到这一点。

Not all three-letter IDs seem to be supported, as stated in the docs: 并非所有三个字母的ID都支持,如文档中所述:

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. 为了与JDK 1.1.x兼容,还支持其他一些三字母时区ID(例如“PST”,“CTT”,“AST”)。 However, their use is deprecated [...] 但是,它们的使用已被弃用[...]

As @duckstep says, just a few short names are supported. 正如@duckstep所说,只支持几个短名称。

So, you must use Asia/Singapore , eg: 所以,你必须使用Asia/Singapore ,例如:

Code : 代码

TimeZone timeZone = TimeZone.getTimeZone("Asia/Singapore");
System.out.println(timeZone.getID());
System.out.println(timeZone.getRawOffset());
System.out.println(timeZone.getDisplayName(false, TimeZone.LONG));
System.out.println(timeZone.getDisplayName(false, TimeZone.SHORT));
System.out.println(timeZone.getDisplayName(true, TimeZone.LONG));
System.out.println(timeZone.getDisplayName(true, TimeZone.SHORT));

Output : 输出

Asia/Singapore
28800000
Singapore Time
SGT
Singapore Summer Time
SGST

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

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