2015-04-05 32 views
0

我试图在“onLocationChanged”方法中获取时间戳,但没有成功。如何在“onLocationChanged”方法中借助location.getTime设置时间戳?经度和纬度正在显示。在onLocationChanged方法中设置时间戳

public void onLocationChanged(Location location) { 
 
\t \t \t // TODO Auto-generated method stub 
 
\t \t \t if (location != null) { 
 
\t \t \t \t double plong = location.getLongitude(); 
 
\t \t \t \t double pLat = location.getLatitude(); 
 

 
\t \t \t Date d = new Date(); 
 
\t \t \t SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy hh:mm:ss"); 
 
\t \t \t String s = sdf.format(d); 
 
\t \t \t textTime.setText(s); 
 

 
\t \t \t \t textLat.setText(Double.toString(pLat)); 
 
\t \t \t \t textLong.setText(Double.toString(plong)); 
 

 
\t \t \t } 
 
\t \t }

+0

我不知道是否Android的时间戳工作,它已被弃用。你有没有尝试日期而不是 – faljbour 2015-04-05 16:35:04

+0

看到这篇文章,http://stackoverflow.com/questions/16579921/android-locatoin-gettime-always-returns-big-different-time – faljbour 2015-04-05 16:48:59

回答

0

我只想试试这个:

public void onLocationChanged(Location location) { 
    Timestamp time = new Timestamp(location.getTime()); 

    textTime.setText(time.toString()); 
} 

指定自定义输出字符串格式的优良使用SimpleDateFormat

public void onLocationChanged(Location location) { 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
    String formatted = sdf.format(new Date(location.getTime())); 

    textTime.setText(formatted); 
} 
+0

我更新的答案效果很好,但我想用“ 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”我想要没有秒位的时间戳。 – 2015-04-05 21:21:14