views:

46

answers:

1

I've tried different methods around the web but couldn't make it work.

    Cursor cursor = sqlite.myDataBase.rawQuery("SELECT StartDate, EndDate FROM Tracks Where Id="+'"'+trackId+'"',null);

 SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 Date startDate = outputFormat.parse(cursor.getString(cursor.getColumnIndex("StartDate")));
 Date endDate = outputFormat.parse(cursor.getString(cursor.getColumnIndex("EndDate")));

In this way I get both dates in good format. Now I want to find the difference between EndDate and Startdate in seconds.

Any advice ? Thank you.

+1  A: 

You can turn a date object into a long (milliseconds since Jan 1, 1970), and then use TimeUnit to get the number of seconds:

long diffInMs = endDate.getTime() - startDate.getTime();

long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMis);
Mayra