[英]java- get TimeZone from ID
為了獲得新加坡的時區偏移,如果我嘗試“新加坡”作為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-02-08 11:18 AM SGT”的日期那么?????
我仍然無法使用上述日期抵消“新加坡”嗎?
試着讓我如何做到這一點。
並非所有三個字母的ID都支持,如文檔中所述:
為了與JDK 1.1.x兼容,還支持其他一些三字母時區ID(例如“PST”,“CTT”,“AST”)。 但是,它們的使用已被棄用[...]
正如@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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.