Java 在onLocationChanged方法中设置时间戳
我试图用“onLocationChanged”方法获取时间戳,但没有成功。如何在“onLocationChanged”方法中借助location.getTime设置时间戳?正在显示经度和纬度Java 在onLocationChanged方法中设置时间戳,java,android,Java,Android,我试图用“onLocationChanged”方法获取时间戳,但没有成功。如何在“onLocationChanged”方法中借助location.getTime设置时间戳?正在显示经度和纬度 public void onLocation已更改(位置){ //TODO自动生成的方法存根 如果(位置!=null){ double-plong=location.getLongitude(); 双平台=location.getLatitude(); 日期d=新日期(); SimpleDataForma
public void onLocation已更改(位置){
//TODO自动生成的方法存根
如果(位置!=null){
double-plong=location.getLongitude();
双平台=location.getLatitude();
日期d=新日期();
SimpleDataFormat sdf=新的SimpleDataFormat(“dd.mm.yyyy hh:mm:ss”);
字符串s=sdf.format(d);
text time.setText(s);
textLat.setText(Double.toString(pLat));
textLong.setText(Double.toString(plong));
}
}
我只想试试这个:
public void onLocation已更改(位置){
Timestamp time=新的时间戳(location.getTime());
textTime.setText(time.toString());
}
要指定自定义输出字符串格式,可以使用
SimpleDataFormat
:
public void onLocation已更改(位置){
SimpleDataFormat sdf=新的SimpleDataFormat(“yyyy-MM-dd-HH:MM”);
String formatted=sdf.format(新日期(location.getTime());
textTime.setText(格式化);
}
我想知道时间戳在Android中是否有效,它已被弃用。你试过日期而不是看这篇文章,我更新的答案也很好,但我想使用“location.getTime()”方法。是否有任何方法可以从location.getTime()格式获取秒数?location.getTime()的当前输出为“2015-04-05 23:17:26.0”,末尾为0位。通过我的代码,我可以确定使用的格式,例如“dd.mm.yyyy hh:mm:ss”==>不带秒数“dd.mm.yyyy hh:mm”我想要不带秒数的时间戳。