我在半小时内获取gps位置并在textview中显示它还将textview中传递的值保存到sdcard gpsdata.txt文件,使用下面的代码,但在这里我必须保存时间和日期以及编号
public void appendData(String text)
{
File dataFile = new File(Environment.getExternalStorageDirectory() + "/SUNDTH/GpsData.txt");
if (!dataFile.exists())
{
try
{
dataFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(dataFile, true));
buf.append(text);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
谢谢。
答案 0 :(得分:2)
在行buf.append(text);
之前添加这行代码;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
String currentDateandTime = sdf.format(new Date());
text+=" "+currentDateandTime;
答案 1 :(得分:1)
SimpleDateFormat dateFormat = new SimpleDateFormat("d/M/yyyy");
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mmz");
这里是生成单个语句的代码,我假设你使用了Location对象
13.01471695,80.1469223 time:12 30pm date:1/1/2013
Date locationDate = new Date(location.getTime());
StringBuilder sb = new StringBuilder(location.getLatitude())
.append(",")
.append(location.getLongitude())
.append("time:")
.append(timeFormat.format(locationDate))
.append(" date:")
.append(dateFormat.format(locationDate));
String locationDetail = sb.toString();
答案 2 :(得分:1)
为什么不为此使用SQLiteDatabase
,然后你可以简单地使用这样的东西来插入具有当前日期时间的记录:
How to insert a SQLite record with a datetime set to 'now' in Android application?