久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在android中將UTC時(shí)間戳轉(zhuǎn)換為設(shè)備本地時(shí)間

How to convert UTC timestamp to device local time in android(如何在android中將UTC時(shí)間戳轉(zhuǎn)換為設(shè)備本地時(shí)間)
本文介紹了如何在android中將UTC時(shí)間戳轉(zhuǎn)換為設(shè)備本地時(shí)間的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出現(xiàn) IncompatibleClassChangeError)
How to get current flavor in gradle(如何在 gradle 中獲取當(dāng)前風(fēng)味)
How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修復(fù)“意外元素lt;查詢(xún)gt;在“清單中找到錯(cuò)誤?)
Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多風(fēng)味庫(kù)的多風(fēng)味應(yīng)用)
Android dependency has different version for the compile and runtime(Android 依賴(lài)在編譯和運(yùn)行時(shí)有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫(kù)的傳遞依賴(lài))
主站蜘蛛池模板: 色婷婷av一区二区三区软件 | 中文字幕视频在线看5 | 亚洲综合婷婷 | 亚洲成人日韩 | 2一3sex性hd| 欧美精品在线免费观看 | 国户精品久久久久久久久久久不卡 | 精品一区二区三区在线视频 | 天堂综合网久久 | 久久久xx | 国产一区二区精品 | 国产免费一区二区三区网站免费 | 奇米av| 国产在线观看不卡一区二区三区 | 精品久久久久久亚洲精品 | 亚洲视频 欧美视频 | 成人精品一区二区三区 | 欧美国产日韩在线观看 | 国产在线一区二区三区 | 成人黄在线观看 | 国产乱码精品一区二区三区中文 | 欧美视频在线观看 | 精品欧美一区二区在线观看 | 亚洲电影免费 | 国产成人精品免高潮在线观看 | 精品欧美一区二区三区免费观看 | 成人伊人 | 在线小视频| 国产精品免费观看 | 久久日本 | 久久91| 91精品国产91 | 亚洲97| 日本视频免费 | 91青娱乐在线 | 国产精品一级在线观看 | 激情五月婷婷丁香 | 99视频| 欧美人人| 国产一区二区在线免费观看 | 日韩一级电影免费观看 |