java 获取gps时间,使用GPS(LocationManager)如何获取当前时间?

I am developing one GPS Application. Which will send the location data to server for every one hour.

In this I am using following code:

location.getLatitude();

location.getLongitude();

location.getTime();

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.

so now I need to convert that 13 digit number in to the specific format.

解决方案

Location#getTime() returns "the UTC time of this fix, in milliseconds since January 1, 1970."

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);

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();

That's it!

Does that help? If not, could you clarify what you're trying to do?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值