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时间戳转换为设备本地时间
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01