2015-02-07 43 views
0

獲得時差爲獲得時區新加坡的偏移,如果我嘗試「新加坡」的ID,我會得到正確的一個像如下:Java的從ID

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

但如果到位新加坡作爲嘗試SGT下面,它將返回偏移GMT的,因爲它不理解SGT爲ID:

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

任何方式獲得通過地方「新加坡」的使用「SGT」新加坡糾正偏移????

上述問題不針對ID「America/Los_Angeles」生成。 「PST」和「America/Los_Angeles」返回相同的偏移量。

問候

EDITED--

如果我有像日期 「2015年2月8日11:18 SGT」,然後?????

我還能用上述日期抵消「新加坡」嗎?

試圖讓我如何使可能。

回答

2

並非所有的三個字母的標識似乎得到支持,如文檔指出:

爲了與JDK 1.1.x版本的兼容性,一些其他三字母時區ID(比如「PST 「,」CTT「,」AST「)也被支持。然而,它們的使用已被廢棄[012]

0

正如@duckstep所說,只支持一些短名稱。

所以,你必須使用Asia/Singapore,如:

代碼

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)); 

輸出

Asia/Singapore 
28800000 
Singapore Time 
SGT 
Singapore Summer Time 
SGST 
+0

我們可以得到的結果爲'亞洲/ Singapore'時我們給'TimeZone.getTimeZone(「SGT」)'? – 2017-07-25 06:47:23