2

I am fetching the gps location once in half an hour and displaying it in textview also saving the values passed in textview to sdcard gpsdata.txt file, using the below code but here i have to save it with time and date and also numbered.


  • Now i get gps location like this in txt file :

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

  • But i need like this:

1)13.01471695,80.1469223 time:12 30pm date:1/1/2013
2)13.01471670,80.1469260 time:12 45pm date: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();
   }
}

thank you.

3 Answers3

2

Before the line buf.append(text); add this lines of code;

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
String currentDateandTime = sdf.format(new Date());
text+=" "+currentDateandTime;
Lucifer
  • 29,392
  • 25
  • 90
  • 143
Onur A.
  • 3,007
  • 3
  • 22
  • 37
1
SimpleDateFormat dateFormat = new SimpleDateFormat("d/M/yyyy");
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mmz");

here is the code to generate single statement, I assuming that you using Location object

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();
Pratik
  • 30,639
  • 18
  • 84
  • 159
1

Why not use a an SQLiteDatabase for this then you can simply use something like this to insert records with the current date time:

How to insert a SQLite record with a datetime set to 'now' in Android application?

Community
  • 1
  • 1
Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66