在几秒钟内两个日期之间的Andr​​oid的差异 [英] Android difference between two dates in seconds

查看:118
本文介绍了在几秒钟内两个日期之间的Andr​​oid的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试过各地的网络不同的方法,但不能使它工作。

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.

有什么建议?谢谢你。

推荐答案

您可以把Date对象为长(自1970年1月1日毫秒),然后用的TimeUnit 获得秒数:

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

编辑: -Corrected这是写diffInM(ⅰ)■在第二行中的变量diffInMs的名称

-Corrected the name of the variable diffInMs which was written diffInM(i)s in the second line.

这篇关于在几秒钟内两个日期之间的Andr​​oid的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆