我从服务器上获取了一些数据,其中一部分是日期。日期采用UTC格式。我正在使用相对时间向用户显示时间getRelativeTimeSpanString
。
题
如何将UTC时间转换为用户设备上设置的本地时区。
这就是我从服务器获取日期并使用的方式 relativeTimeSpanString
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).
parse("2014-05-21 12:21:41");
Date currentDate = new Date();
CharSequence cs = DateUtils.getRelativeTimeSpanString(date.getTime(), currentDate.getTime(), DateUtils.SECOND_IN_MILLIS);
尝试这样的事情。它考虑了时区以及夏令时。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Date date = sdf.parse("2014-05-21 12:21:41");
TimeZone tz = TimeZone.getDefault();
int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(date) ? tz.getDSTSavings() : 0);
String result = sdf.format(date.getTime() + currentOffsetFromUTC);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句