简体   繁体   中英

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:

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:

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"????

Above issue is not produced for ID "America/Los_Angeles". "PST" and "America/Los_Angeles" returns same offset.

Regards

EDITED--

If I have date like "2015-02-08 11:18 AM SGT" then ?????

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:

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated [...]

As @duckstep says, just a few short names are supported.

So, you must use Asia/Singapore , eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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