[英]How to get the number of days between a given date and the previous records date?
I have a ListView with records and tapping on a record opens a detailed record view. 我有一个带有记录的ListView,点击一条记录会打开一个详细的记录视图。 In this detailed view, I want to print out the number of days between the current record and the previous record. 在此详细视图中,我想打印出当前记录和上一个记录之间的天数。 How can I do that? 我怎样才能做到这一点?
I know there exist an SQLite function getting the last inserted id, but I can't see that it would help here. 我知道有一个SQLite函数获取最后插入的id,但是我看不到它在这里有帮助。
I'm thinking of solving it this way: 我正在考虑通过以下方式解决它:
So, the answer I want is like: this tank lasted for 13 days. 所以,我想要的答案是:这个坦克持续了13天。
Actually I'm stuck on step 3 above, how can I find out, given an arbitrary id, what the record stored in the db before that id, has for id? 实际上,我处于上面的第3步,在给定任意ID的情况下,如何找出该ID之前db中存储的id记录是什么?
An example: 一个例子:
id date note
---------------------------
1 2013-01-03 first note
2 2013-01-13 second note
4 2013-01-20 ...
7 2013-02-01 ...
If I open the detailed view for record with id 4, I want to calculate the number of days gone since record with id 2 was written. 如果打开ID为4的记录的详细视图,我想计算自ID为2的记录被写入以来经过的天数。 Ie 2013-01-20 - 2013-01-13. 即2013-01-20-2013-01-13。
Kind regards, Ramon 亲切的问候,拉蒙
You can use the following approach for date
manipulation:- 您可以使用以下方法进行date
操作:
SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd");
long d1=formater.parse(date1Str).getTime();
long d2=formater.parse(date2Str).getTime();
System.out.println(Math.abs((d1-d2)/(1000*60*60*24)));
You should not go by ID. 您不应该使用ID。 Insert the timestamp while inserting record. 插入记录时插入时间戳。 Get the record order by timestamp. 按时间戳获取记录顺序。
Once you have ordered data, it is just matter of writing date difference function. 订购数据后,只需编写日期差函数即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.