2

我每半小时获取一次 gps 位置并在 textview 中显示它,还将 textview 中传递的值保存到 sdcard gpsdata.txt 文件,使用下面的代码,但在这里我必须保存它的时间和日期以及编号。


  • 现在我在 txt 文件中得到这样的 gps 位置:

13.01471695,80.1469223 13.01471695,
80.1469223 13.01471695,80.1469223
13.01471695,80.1469223

  • 但我需要这样:

1)13.01471695,80.1469223 时间:12 30pm 日期:1/1/2013
2)13.01471670,80.1469260 时间:12 45pm 日期:1/1/2013

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();
   }
}

谢谢你。

4

3 回答 3

2

在该行之前buf.append(text);添加这行代码;

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
String currentDateandTime = sdf.format(new Date());
text+=" "+currentDateandTime;
于 2013-04-20T10:28:44.487 回答
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();
于 2013-04-20T10:32:13.377 回答
1

为什么不为此使用 anSQLiteDatabase那么您可以简单地使用类似这样的方法来插入具有当前日期时间的记录:

如何在 Android 应用程序中插入日期时间设置为“现在”的 SQLite 记录?

于 2013-04-20T10:35:27.600 回答