0
            String start_time = cursor.getString(cursor
                    .getColumnIndex(STARTING_TIME));
            Log.d("timetracker","start time"+start_time);

            String stop_time = cursor.getString(cursor
                    .getColumnIndex(STOPPING_TIME));
            Log.d("timetracker","stop time"+stop_time);

in log i am getting this output for the above code

 01-08 10:27:32.107: D/timetracker(29800): start time2016-01-07 14:36:28.000

   01-08 10:27:32.107: D/timetracker(29800): stop time2016-01-07 14:36:40.000

   01-08 10:27:32.107: D/timetracker(29800): start time2016-01-07 14:37:43.000

   01-08 10:27:32.107: D/timetracker(29800): stop time2016-01-07 14:37:44.000

   01-08 10:27:32.107: D/timetracker(29800): start time2016-01-07 14:38:26.000

   01-08 10:27:32.107: D/timetracker(29800): stop time2016-01-07 14:38:27.000

i need to find difference btw start_time and stop_time eg: btw 2016-01-07 14:36:28.000 and 2016-01-07 14:36:40.000

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
  • 1
    instead of having the date and time as strings, you need to save time in millis. It would be easy to get difference from that – Logic Jan 08 '16 at 05:12
  • follow this link: http://stackoverflow.com/questions/10690370/how-do-i-get-difference-between-two-dates-in-android-tried-every-thing-and-pos – prakash Jan 08 '16 at 05:13
  • 1
    first convert those string to Date object and follow the posted answers visit [SimpleDateFormat](https://developer.android.com/reference/java/text/SimpleDateFormat.html) – Bharatesh Jan 08 '16 at 05:18

4 Answers4

0

Convert both of your string in Date object.

String secondsString = TimeUnit.MILLISECONDS.toSeconds(date1.getTime() - date2.getTime()) + " seconds ago";
Ravi
  • 34,851
  • 21
  • 122
  • 183
0

Calendar c1 = Calendar.getInstance(); c1.setTime(date1);

Calendar c2 = Calendar.getInstance(); c2.setTime(date2);

long ms1 = c1.getTimeInMillis(); long ms2 = c2.getTimeInMillis();

longdiff=ms2-ms1;

long Second=TimeUnit.SECONDS.toSecond(draftStartDiff);

Kaushal Patel
  • 338
  • 4
  • 19
0

you can convert String date to object of date in your class....

private Long secondsBetween(Date first, Date second)
      {
    return (first.getTime() - second.getTime())/1000;
      }
koutuk
  • 832
  • 1
  • 8
  • 17
0

Let the database compute it:

SELECT starting,
       stopping,
       strftime('%s', stopping) - strftime('%s', starting) AS duration
FROM ...;
CL.
  • 173,858
  • 17
  • 217
  • 259