使用GPS(LocationManager)如何获取当前时间? [英] Using the GPS (LocationManager) how To get the current Time?
问题描述
我正在开发一台GPS的应用。这将位置数据发送到服务器,每隔一小时。
I am developing one GPS Application. Which will send the location data to server for every one hour.
在此我使用下面的code:
In this I am using following code:
location.getLatitude();
location.getLongitude();
location.getTime();
本code我得到纬度和经度正确,时间还可以,但我得到的时间约13个位数来代替。我做的一些研究,我发现这是一个秒的当前时间。
with this code I am getting Latitude and Longitude correctly and Time also, but I am getting some 13 digits number instead of the time. I done some research on that, I found that is an seconds for of the current time.
所以现在我需要转换到特定格式的13位数字。
so now I need to convert that 13 digit number in to the specific format.
推荐答案
<一个href="http://developer.android.com/reference/android/location/Location.html#getTime%28%29"><$c$c>Location#getTime()$c$c>返回的此修复程序的UTC时间,在自1970年1月1日毫秒。的
这是完全一样的行为<一href="http://download.oracle.com/javase/6/docs/api/java/util/Date.html#getTime%28%29"><$c$c>java.util.Date#getTime()$c$c>.我不清楚你想要做的这个时候数据是什么,但如果你想在位置
的时间转换成 java.util.Date
,你可以做到这一点是这样的:
This is exactly the same behavior as java.util.Date#getTime()
. I'm not clear on what you'd like to do with this time data, but if you'd like to convert the Location
's time into a java.util.Date
, you can do it like this:
long time = location.getTime();
Date date = new Date(time);
现在是比较容易的工作。如果你想创建该日期的特定字符串的输出格式,使用<一个href="http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html"><$c$c>SimpleDateFormat$c$c>:
Now it is somewhat easier to work with. If you'd like to create a particular string output format of that date, use SimpleDateFormat
:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String text = sdf.format(date);
System.out.println(text); // prints something like 2011-01-08 13:35:48
这是说,如果你想要做的就是当前的时间(这是它听起来像什么,你正在试图做的),你并不需要一个位置
在所有的:
That said, if all you'd like to do is get the current time (which is what it sounds like you're trying to do) you don't need a Location
at all:
Date now = new Date();
这就是它!
帮助吗?如果没有,你能澄清你想要做什么?
Does that help? If not, could you clarify what you're trying to do?
这篇关于使用GPS(LocationManager)如何获取当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!