在几秒钟内两个日期之间相差的Android [英] Android difference between two dates in seconds
本文介绍了在几秒钟内两个日期之间相差的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
光标cursor = sqlite.myDataBase.rawQuery(SELECT StartDate,EndDate FROM Tracks Where Id =+''+ trackId + '',空值);
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)));
以这种方式,我将这两个日期格式化。现在我想在几秒钟内找到 EndDate
和 Startdate
之间的区别。
任何建议?谢谢。
解决方案
您可以将日期对象变为长(从1970年1月1日起的毫秒),然后使用 TimeUnit
获取秒数:
long diffInMs = endDate.getTime () - startDate.getTime();
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs);
编辑:
- 修改了写入diffInM(i)的变量diffInMs的名称在第二行。
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.
解决方案
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);
Edit: -Corrected the name of the variable diffInMs which was written diffInM(i)s in the second line.
这篇关于在几秒钟内两个日期之间相差的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文