查找android中两个日期时间字符串之间的秒数

时间:2016-01-08 05:06:42

标签: android sqlite datetime

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

在日志中我得到上述代码的输出

 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

我需要找到差异btw start_time和stop_time 例如:btw 2016-01-07 14:36:28.000 and 2016-01-07 14:36:40.000

4 个答案:

答案 0 :(得分:0)

转换Date对象中的两个字符串。

String secondsString = TimeUnit.MILLISECONDS.toSeconds(date1.getTime() - date2.getTime()) + " seconds ago";

答案 1 :(得分:0)

日历c1 = Calendar.getInstance();         c1.setTime(DATE1);

日历c2 = Calendar.getInstance();         c2.setTime(DATE2);

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

longdiff = MS2-MS1;

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

答案 2 :(得分:0)

  

您可以将字符串日期转换为类中的日期对象....

private Long secondsBetween(Date first, Date second)
      {
    return (first.getTime() - second.getTime())/1000;
      }

答案 3 :(得分:0)

让数据库计算它:

SELECT starting,
       stopping,
       strftime('%s', stopping) - strftime('%s', starting) AS duration
FROM ...;