2012-07-25 152 views
0

在我的應用程序中,我需要將數據轉換爲位置時間。我有java DateTimeZoneIdTimeZone.getTimeZone()會隨機失敗嗎?

當我從服務中獲取數據時 - 它以UTC形式出現,我也獲得了時區。需要轉換爲該時區的時間。我創建的功能:

private static Date getDateConvertedToTimeZone(Date utcDate, String timeZoneId) 
    { 
     TimeZone timeZone = TimeZone.getTimeZone(timeZoneId); 
     if (timeZone.getID().equalsIgnoreCase(timeZoneId)) 
     { 
      Log.d(LOG_TAG, "Good: " + timeZoneId + " " + Long.toString(utcDate.getTime())); 
      return new Date(utcDate.getTime() + timeZone.getRawOffset() + (timeZone.inDaylightTime(utcDate) ? timeZone.getDSTSavings() : 0)); 
     } 

     Log.d(LOG_TAG, "Bad?: " + timeZoneId + " " + Long.toString(utcDate.getTime())); 
     LogData.d(LOG_TAG, "getDateConvertedToTimeZone bad time zone: " + timeZoneId, LogData.Priority.None); 
     return utcDate; 

    } 

簡單嗎?對。只需通過ID獲取時區並在Date對象上進行一些計算。這是我想...當我解析數據函數重複調用,這裏輸出:

07-24 21:31:06.038: DEBUG/com.me.data.TripData(18886): Good: MST 1319896800000 
07-24 21:31:06.042: DEBUG/com.me.data.TripData(18886): Good: MST -2208963600000 
07-24 21:31:06.053: DEBUG/com.me.data.TripData(18886): Bad?: PST 1320949800000 
07-24 21:31:06.057: DEBUG/com.me.data.TripData(18886): Bad?: CST 1320685200000 
07-24 21:31:06.061: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000 
07-24 21:31:06.073: DEBUG/com.me.data.TripData(18886): Bad?: CST 1320645600000 
07-24 21:31:06.077: DEBUG/com.me.data.TripData(18886): Good: EST 1320350400000 
07-24 21:31:06.081: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000 
07-24 21:31:06.085: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000 
07-24 21:31:06.093: DEBUG/com.me.data.TripData(18886): Bad?: CST 1321268400000 
07-24 21:31:06.096: DEBUG/com.me.data.TripData(18886): Bad?: PST 1320957000000 
07-24 21:31:06.108: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000 
07-24 21:31:06.116: DEBUG/com.me.data.TripData(18886): Bad?: PST 1321542000000 
07-24 21:31:06.120: DEBUG/com.me.data.TripData(18886): Good: MST 1321500600000 
07-24 21:31:06.124: DEBUG/com.me.data.TripData(18886): Bad?: CST 1321286400000 
07-24 21:31:06.128: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000 
07-24 21:31:06.135: DEBUG/com.me.data.TripData(18886): Good: EST 1321981200000 
07-24 21:31:06.139: DEBUG/com.me.data.TripData(18886): Bad?: CST 1321884000000 
07-24 21:31:06.143: DEBUG/com.me.data.TripData(18886): Bad?: PST 1321646400000 
07-24 21:31:06.151: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000 
07-24 21:31:06.159: DEBUG/com.me.data.TripData(18886): Bad?: CST 1322485200000 
07-24 21:31:06.163: DEBUG/com.me.data.TripData(18886): Good: EST 1321966800000 
07-24 21:31:06.163: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000 
07-24 21:31:06.175: DEBUG/com.me.data.TripData(18886): Bad?: PST 1322595000000 
07-24 21:31:06.178: DEBUG/com.me.data.TripData(18886): Bad?: CST 1322244000000 
07-24 21:31:06.182: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000 
07-24 21:31:06.194: DEBUG/com.me.data.TripData(18886): Bad?: PST 1322755200000 
07-24 21:31:06.194: DEBUG/com.me.data.TripData(18886): Bad?: PST 1322683440000 
07-24 21:31:06.206: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000 
07-24 21:31:06.218: DEBUG/com.me.data.TripData(18886): Good: EST 1323190800000 
07-24 21:31:06.218: DEBUG/com.me.data.TripData(18886): Good: EST 1323176400000 
07-24 21:31:06.221: DEBUG/com.me.data.TripData(18886): Bad?: PST 1322845200000 
07-24 21:31:06.229: DEBUG/com.me.data.TripData(18886): Bad?: CST 1323356400000 
07-24 21:31:06.233: DEBUG/com.me.data.TripData(18886): Good: EST 1323212400000 
07-24 21:31:06.237: DEBUG/com.me.data.TripData(18886): Good: EST 1323194400000 
07-24 21:31:06.241: DEBUG/com.me.data.TripData(18886): Good: EST 1323183600000 
07-24 21:31:06.245: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000 
07-24 21:31:06.311: DEBUG/com.me.data.TripData(18886): Bad?: PST 1323705600000 
07-24 21:31:06.319: DEBUG/com.me.data.TripData(18886): Bad?: CST 1323351000000 
07-24 21:31:06.323: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000 
07-24 21:31:06.331: DEBUG/com.me.data.TripData(18886): Good: EST 1324054800000 
07-24 21:31:06.335: DEBUG/com.me.data.TripData(18886): Good: EST 1324040400000 
07-24 21:31:06.339: DEBUG/com.me.data.TripData(18886): Bad?: CST 1323961200000 
07-24 21:31:06.343: DEBUG/com.me.data.TripData(18886): Bad?: PST 1323730800000 
07-24 21:31:06.346: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000 
07-24 21:31:06.354: DEBUG/com.me.data.TripData(18886): Bad?: CST 1324303200000 
07-24 21:31:06.358: DEBUG/com.me.data.TripData(18886): Good: EST 1324062000000 
07-24 21:31:06.362: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000 
07-24 21:31:06.370: DEBUG/com.me.data.TripData(18886): Bad?: CST 1324603800000 
07-24 21:31:06.374: DEBUG/com.me.data.TripData(18886): Bad?: PST 1324306800000 
07-24 21:31:06.378: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000 
07-24 21:31:06.385: DEBUG/com.me.data.TripData(18886): Good: EST 1324962000000 
07-24 21:31:06.389: DEBUG/com.me.data.TripData(18886): Bad?: CST 1324620000000 
07-24 21:31:06.393: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000 
07-24 21:31:06.405: DEBUG/com.me.data.TripData(18886): Bad?: CST 1325163600000 
07-24 21:31:06.413: DEBUG/com.me.data.TripData(18886): Good: EST 1324962000000 
07-24 21:31:06.413: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000 
07-24 21:31:06.421: DEBUG/com.me.data.TripData(18886): Bad?: CST 1325768400000 
07-24 21:31:06.425: DEBUG/com.me.data.TripData(18886): Good: EST 1325633400000 
07-24 21:31:06.428: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000 
07-24 21:31:06.436: DEBUG/com.me.data.TripData(18886): Good: EST 1325599200000 
07-24 21:31:06.440: DEBUG/com.me.data.TripData(18886): Good: EST 1325178000000 
07-24 21:31:06.444: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000 
07-24 21:31:06.448: DEBUG/com.me.data.TripData(18886): Bad?: PST 1326290400000 
07-24 21:31:06.456: DEBUG/com.me.data.TripData(18886): Bad?: CST 1325829600000 
07-24 21:31:06.460: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000 
07-24 21:31:06.468: DEBUG/com.me.data.TripData(18886): Good: MST 1326178800000 
07-24 21:31:06.471: DEBUG/com.me.data.TripData(18886): Bad?: PST 1326121200000 
07-24 21:31:06.475: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000 
07-24 21:31:06.483: DEBUG/com.me.data.TripData(18886): Good: EST 1326603600000 
07-24 21:31:06.487: DEBUG/com.me.data.TripData(18886): Good: MST 1326265200000 
07-24 21:31:06.491: DEBUG/com.me.data.TripData(18886): Good: MST -2208963600000 
07-24 21:31:06.499: DEBUG/com.me.data.TripData(18886): Good: EST 1326801600000 
07-24 21:31:06.507: DEBUG/com.me.data.TripData(18886): Good: EST 1326690060000 
07-24 21:31:06.510: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000 
07-24 21:31:06.518: DEBUG/com.me.data.TripData(18886): Bad?: PST 1327064400000 
07-24 21:31:06.522: DEBUG/com.me.data.TripData(18886): Good: MST 1327019400000 
07-24 21:31:06.526: DEBUG/com.me.data.TripData(18886): Bad?: CST 1326837600000 
07-24 21:31:06.530: DEBUG/com.me.data.TripData(18886): Bad?: CST 1326834060000 
07-24 21:31:06.585: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000 
07-24 21:31:06.608: DEBUG/com.me.data.TripData(18886): Bad?: CST 1327672800000 
07-24 21:31:06.616: DEBUG/com.me.data.TripData(18886): Good: EST 1327496400000 
07-24 21:31:06.620: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000 
07-24 21:31:06.628: DEBUG/com.me.data.TripData(18886): Good: EST 1327467600000 
07-24 21:31:06.632: DEBUG/com.me.data.TripData(18886): Good: MST 1327042800000 
07-24 21:31:06.632: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000 
07-24 21:31:06.643: DEBUG/com.me.data.TripData(18886): Good: MST 1327926600000 
07-24 21:31:06.647: DEBUG/com.me.data.TripData(18886): Bad?: CST 1327616100000 
07-24 21:31:06.651: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000 
07-24 21:31:06.659: DEBUG/com.me.data.TripData(18886): Good: EST 1328245200000 
07-24 21:31:06.663: DEBUG/com.me.data.TripData(18886): Good: MST 1327906800000 
07-24 21:31:06.667: DEBUG/com.me.data.TripData(18886): Good: MST -2208963600000 
07-24 21:31:06.678: DEBUG/com.me.data.TripData(18886): Good: EST 1327060800000 
07-24 21:31:06.678: DEBUG/com.me.data.TripData(18886): Bad?: CST 1327060800000 
07-24 21:31:06.682: DEBUG/com.me.data.TripData(18886): Bad?: CST 1327060800000 

回答

0

您是否使用3個字母的時區標識符?這在Andorid中不受支持。

看到更多的不是特殊情況「UTC」和「GMT」其他Documentation

(這是同義在這種情況下,無論是對應於UTC),Android不支持過時的三字母時區ID用於Java 1.1。

你希望使用表單區域/位置,如美國/洛杉磯的

奧爾森名。 getAvailableIDs()方法返回支持的名稱。

您也可以使用GMT + 05:00語法。

+0

是的,我使用的是從Web服務獲得的標識符。有趣的是,它是理解EST,MST而不是CST,PST – katit 2012-07-25 03:15:30