我有一個Android應用程序,可以使用GPS模塊和SQLite數據庫。在開始時,一切都聽起來很不錯,但是當我需要使用Date()和時區時,恐怖就會出現。Android Date Date Timezones驅動我堅果
一點背景
GPS模塊有可能從衛星獲取日期。這是一件好事,我需要在我的應用程序中確保有效的數據。從它返回的日期是GMT時間。 此日期作爲StartTime插入到本地數據庫的一個字段中。 StartTime以AES加密存儲。當我解密StartTime時,我需要將它轉換爲Date並將格式保留爲GMT。
我做什麼 獲取LocationListener的從GPS數據:具有默認的GPS日期轉換在設備的當前時區新的Date()
gpsDate = new Date(location.getTime());
。因此,我沒有一個不錯的Sun Dec 05 14:00:00 GMT 2010 time,我得到Sun Dec 05 16:00:00 GMT + 2 2010。它爲什麼這樣做?我只是想從getTime()中取出一個新日期。
我插入數據在數據庫:
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = outputFormat.format(gpsDate);
我檢索日期,當我需要它:
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String myStored = {decrypy from the database}
Date myStartDate = outputFormat.parse(myStoredDate);
myStored顯示:2010-12-05 14:00
myStartDate顯示:Sun Dec 05 16:00:00 GMT + 2 2010
這裏有一些我很想念的東西。我需要能夠以格式獲取日期,插入,轉換,比較,計算差異。只有當我需要顯示給他時,我需要用戶時區中的日期。
請照亮我。
爲什麼你需要一個DATE對象,如果你只是想把它放在數據庫中。只要把你從location.getTime() – Falmarri 2010-12-05 20:28:17
得到的時間,這是一個很好的點...我會嘗試看看它是如何工作的。 – Alin 2010-12-05 20:52:45