在几秒钟内两个日期之间的Andr​​oid的差异差异、日期、两个、几秒钟内

由网友(苦瓜村长i)分享简介:我已经试过各地的网络不同的方法,但不能使它工作。 I've tried different methods around the web but couldn't make it work. Cursor cursor = sqlite.myDataBase.rawQuery("SELECT StartDate, E...

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

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.

阅读全文

相关推荐

最新文章