简体   繁体   English

如何获取给定日期与上一个记录日期之间的天数?

[英]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: 我正在考虑通过以下方式解决它:

  1. Get the id of the current record shown in detailed view 获取详细视图中显示的当前记录的ID
  2. Get its date and save temporarily 获取日期并临时保存
  3. Given that id, what is the id of the record written before current id? 给定该ID,在当前ID之前写入的记录的ID是什么?
  4. Given the id of the previous record, get its date and save temporarily. 给定上一条记录的ID,获取其日期并临时保存。
  5. Calculate number of days between current record in detailed view and previous record. 计算详细视图中的当前记录与上一个记录之间的天数。

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.

相关问题 如何获得给定字符串日期的一周中的天数 - How to get the number of days in a week given a string date 如何获取日期,该日期是前一个日期与下一个日期的7天之和? - How to get date which is the sum of any previous date to 7 days next date? 如何计算 android 中日期范围材料选择器之间的天数 - How to count number of days between date range material picker in android 将日期字符串转换为 Kotlin 中的日期 object? 如何计算 Kotlin 中今天和特定日期之间的天数? - Convert Date String to Date object in Kotlin? How to calculate number of days between today and specific date in Kotlin? Android的日期天数 - Number of Days into Date Android 如何在flutter中仅给出年份​​中的日期来获取日期 - How to get date given only the day of year number in flutter Android-获取自上一个日期以来的天,月和年 - Android - get Days, Months, and Years since a previous date 如何选择日期在date1和date2之间的记录? - How to select records where date is between date1 and date2? 从给定日期获取星期数 - Get Week number from given date 如何在Android中的sqlite中获取当前日期和上一年的当前日期之间的值 - How to get the values between current date and current date of previous year in sqlite in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM