問(wèn)題描述
我需要將從服務(wù)器獲取的 UTC 時(shí)間戳轉(zhuǎn)換為本地設(shè)備時(shí)間.目前我的時(shí)間相差 5 小時(shí).例如,當(dāng)我發(fā)布到服務(wù)器時(shí),發(fā)布時(shí)間是 5 小時(shí)前而不是一秒前.如何解決這個(gè)問(wèn)題.謝謝
I need to convert the UTC time stamp that i get from the server to local device time. currently i get 5 hrs difference in my time. for example when i post to server the post time says 5 hours ago instead of a second ago. How to fix this issue. thanks
下面是我做的代碼
long timestamp = cursor.getLong(columnIndex);
CharSequence relTime = DateUtils
.getRelativeTimeSpanString(timestamp * 1000
+ TimeZone.getDefault().getRawOffset(),
System.currentTimeMillis(),
DateUtils.MINUTE_IN_MILLIS);
((TextView) view).setText(relTime);
推薦答案
您示例中的代碼乍一看很好.順便說(shuō)一句,如果服務(wù)器時(shí)間戳采用 UTC(即它是一個(gè)紀(jì)元時(shí)間戳),那么您不必應(yīng)用當(dāng)前時(shí)區(qū)偏移量.換句話說(shuō),如果服務(wù)器時(shí)間戳采用 UTC,那么您可以簡(jiǎn)單地獲取服務(wù)器時(shí)間戳和系統(tǒng)時(shí)間 (System.currentTimeMillis()
) 之間的差異,因?yàn)橄到y(tǒng)時(shí)間采用 UTC(紀(jì)元).
The code in your example looks fine at first glance. BTW, if the server timestamp is in UTC (i.e. it's an epoch timestamp) then you should not have to apply the current timezone offset. In other words if the server timestamp is in UTC then you can simply get the difference between the server timestamp and the system time (System.currentTimeMillis()
) as the system time is in UTC (epoch).
我會(huì)檢查來(lái)自您服務(wù)器的時(shí)間戳是否符合您的預(yù)期.如果來(lái)自服務(wù)器的時(shí)間戳沒(méi)有轉(zhuǎn)換為您期望的日期(在本地時(shí)區(qū)),那么時(shí)間戳和當(dāng)前系統(tǒng)時(shí)間之間的差異將不是您期望的.
I would check that the timestamp coming from your server is what you expect. If the timestamp from the server does not convert into the date you expect (in the local timezone) then the difference between the timestamp and the current system time will not be what you expect.
使用 Calendar
獲取當(dāng)前時(shí)區(qū).用當(dāng)前時(shí)區(qū)初始化一個(gè)SimpleDateFormatter
;然后記錄服務(wù)器時(shí)間戳并驗(yàn)證它是否是您期望的日期:
Use Calendar
to get the current timezone. Initialize a SimpleDateFormatter
with the current timezone; then log the server timestamp and verify if it's the date you expect:
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
/* debug: is it local time? */
Log.d("Time zone: ", tz.getDisplayName());
/* date formatter in local timezone */
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
sdf.setTimeZone(tz);
/* print your timestamp and double check it's the date you expect */
long timestamp = cursor.getLong(columnIndex);
String localTime = sdf.format(new Date(timestamp * 1000)); // I assume your timestamp is in seconds and you're converting to milliseconds?
Log.d("Time: ", localTime);
如果打印的服務(wù)器時(shí)間不是你所期望的那么你的服務(wù)器時(shí)間是不是在UTC.
If the server time that is printed is not what you expect then your server time is not in UTC.
如果打印的服務(wù)器時(shí)間是您期望的日期,那么您不必對(duì)其應(yīng)用 rawoffset.所以你的代碼會(huì)更簡(jiǎn)單(減去所有的調(diào)試日志):
If the server time that is printed is the date that you expect then you should not have to apply the rawoffset to it. So your code would be simpler (minus all the debug logging):
long timestamp = cursor.getLong(columnIndex);
Log.d("Server time: ", timestamp);
/* log the device timezone */
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
Log.d("Time zone: ", tz.getDisplayName());
/* log the system time */
Log.d("System time: ", System.currentTimeMillis());
CharSequence relTime = DateUtils.getRelativeTimeSpanString(
timestamp * 1000,
System.currentTimeMillis(),
DateUtils.MINUTE_IN_MILLIS);
((TextView) view).setText(relTime);
這篇關(guān)于如何在android中將UTC時(shí)間戳轉(zhuǎn)換為設(shè)備本地時(shí)間的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!