在onLocationChanged方法中设置时间戳

时间:2015-04-05 16:30:33

标签: java android

我想在" onLocationChanged"中获取时间戳。方法但没有成功。如何在" onLocationChanged"中使用location.getTime来设置时间戳。梅索德?正在显示经度和纬度。



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

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

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

			}
		}




1 个答案:

答案 0 :(得分: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);
}