我根据设置定期使用位置管理器,在测试用例中2分钟并尝试使用location.geTime()方法.我没有使用LocationManager.getLastKNownLocation().文件说这是UTC时间,我把它转换成当地时间,如下所示:
Date d = new Date(location.getTime());SimpleDateFormat sdf = new SimpleDateFormat("yyMMddkkmmss';sdf.setTimeZone(TimeZone.getTimneZone("UTC");sdf.format(d);
但是我的日期与我的期望不同.我写的当前时间约为130516155000(2013-05-16 15:50:00),但我得到了040015130515.
我删除了时区并将时区设置为’GMT’并且日期已修复,但时间却完全不同.在真实设备和模拟器是相同的.我已经检查了两者的时区设置,它们是正确的.
请告诉我我错过了什么?
谢谢.
编辑:
我添加更多.
Log:05-16 16:09:30.227: D/location.getTime()(1279): 136859041700005-16 16:09:30.237: D/NMEAGPRMCTime(1279): 04001705-16 16:09:30.247: D/NMEAGPRMcdate(1279): 130515
码:
public static String NMEAGPRMCTime(Date d){ SimpleDateFormat sdf = new SimpleDateFormat("kkmmss"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String result = sdf.format(d); Log.d("NMEAGPRMCTime", result); return result;}public static String NMEAGPRMcdate(Date d){ SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String result = sdf.format(d); Log.d("NMEAGPRMcdate", result); return result;}
而已.这正是我使用的代码.
解决方法:
这里的格式绝对没有错.
看看日志:
05-16 16:09:30.227: D/location.getTime()(1279): 136859041700005-16 16:09:30.237: D/NMEAGPRMCTime(1279): 04001705-16 16:09:30.247: D/NMEAGPRMcdate(1279): 130515
使用epochconverter,您可以看到“millis since epoch”值1368590417000实际上是Wed,2013年5月15日04:00:17 UTC.所以040017的时间和130515的日期是完全正确的.
我怀疑你实际上对Location.getTime()
的作用感到困惑 – 它返回修复的时间,而不是当前时间.所以基本上,该位置是在UTC时间04:00:17获得的,无论当前日期/时间如何.
以上是内存溢出为你收集整理的Android位置getTime()总是返回很大的不同时间全部内容,希望文章能够帮你解决Android位置getTime()总是返回很大的不同时间所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)