Using The Gps (locationmanager) How To Get The Current Time?
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.getLongitud
Solution 1:
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();
Datedate=newDate(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
:
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Stringtext= 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 = newDate();
That's it!
Does that help? If not, could you clarify what you're trying to do?
Solution 2:
Datedate = newDate(location.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String myDate= sdf.format(date);
System.out.println(myDate)
Solution 3:
String timestamp = parseDate(location.getTime().toDate());
publicStringparseDate(Date date){
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = newSimpleDateFormat(format, Locale.US);
return sdf.format(date).toString();
}
Post a Comment for "Using The Gps (locationmanager) How To Get The Current Time?"