How to convert UTC timestamp to device local time in android(如何在android中将UTC时间戳转换为设备本地时间)
问题描述
我需要将从服务器获取的 UTC 时间戳转换为本地设备时间.目前我的时间相差 5 小时.例如,当我发布到服务器时,发布时间是 5 小时前而不是一秒前.如何解决这个问题.谢谢
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);
推荐答案
您示例中的代码乍一看很好.顺便说一句,如果服务器时间戳采用 UTC(即它是一个纪元时间戳),那么您不必应用当前时区偏移量.换句话说,如果服务器时间戳采用 UTC,那么您可以简单地获取服务器时间戳和系统时间 (System.currentTimeMillis()
) 之间的差异,因为系统时间采用 UTC(纪元).
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).
我会检查来自您服务器的时间戳是否符合您的预期.如果来自服务器的时间戳没有转换为您期望的日期(在本地时区),那么时间戳和当前系统时间之间的差异将不是您期望的.
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
获取当前时区.用当前时区初始化一个SimpleDateFormatter
;然后记录服务器时间戳并验证它是否是您期望的日期:
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);
如果打印的服务器时间不是你所期望的那么你的服务器时间是不是在UTC.
If the server time that is printed is not what you expect then your server time is not in UTC.
如果打印的服务器时间是您期望的日期,那么您不必对其应用 rawoffset.所以你的代码会更简单(减去所有的调试日志):
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);
这篇关于如何在android中将UTC时间戳转换为设备本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在android中将UTC时间戳转换为设备本地时间
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01