S Singh
S Singh

Reputation: 1473

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.

Upvotes: 1

Views: 4319

Answers (2)

Paul Vargas
Paul Vargas

Reputation: 42010

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

So, you must use Asia/Singapore, e.g.:

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

Upvotes: 2

duckstep
duckstep

Reputation: 1138

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 [...]

Upvotes: 3

Related Questions