英文:
When comparing dates, put the objects that fall in the last week on the list
问题
I have translated the code-related portion of your text as you requested:
我正在进行一个项目,在该项目中,用户在添加书籍时会添加完成日期。我将其保存在一个名为`FinishedBooks`的对象中,包括书籍的完成日期和其他数据。我在另一个活动中列出这些书籍。屏幕角落有一个筛选按钮。此按钮将列出最近一周完成的书籍。我之前写了一个用于此目的的方法,以下是我的方法:
private void lastWeekFilter(List<FinishedBooks> finishedBooks) {
List<FinishedBooks> lastWeekList = new ArrayList<>();
// 获取当前时间
long currentTimeMillis = System.currentTimeMillis();
// 计算一周前的时间
long oneWeekMillis = 7L * 24L * 60L * 60L * 1000L;
long oneWeekAgoMillis = currentTimeMillis - oneWeekMillis;
// 检查列表中的每个项目
for (FinishedBooks book : finishedBooks) {
long endDateMillis = book.endDate;
System.out.println("结束日期:" + endDateMillis);
System.out.println("一周前的毫秒数:" + oneWeekAgoMillis);
System.out.println("******************************");
// 如果截止日期在过去1周内,将项目添加到filtered列表中
if (endDateMillis >= oneWeekAgoMillis && endDateMillis <= currentTimeMillis) {
lastWeekList.add(book);
}
}
finishedBooksAdapter.setLastWeekFilter(lastWeekList);
}
And here's the logcat output you provided:
I/System.out: 结束日期:1684159940
I/System.out: 一周前的毫秒数:1683740684856
I/System.out: ******************************
I/System.out: 结束日期:1683987159
I/System.out: 一周前的毫秒数:1683740684856
I/System.out: ******************************
It appears that your code didn't work as expected, and the objects you wanted to list are not included in the lastWeekList
. The list remains empty, even though the incoming data meets the conditions.
英文:
I am working on a project where the user adds a completion date when adding a book. I save it in an object called FinishedBooks
with the completion date and other data of the book. I list these books in another activity. I have a filter button in the corner of the screen. This button will list the books that have been finished in the last week. I wrote a method for this before, here is my method:
private void lastWeekFilter(List<FinishedBooks> finishedBooks) {
List<FinishedBooks> lastWeekList = new ArrayList<>();
// take now time
long currentTimeMillis = System.currentTimeMillis();
// Calculate the time one week ago
long oneWeekMillis = 7L * 24L * 60L * 60L * 1000L;
long oneWeekAgoMillis = currentTimeMillis - oneWeekMillis;
// Check each item in the list
for (FinishedBooks book : finishedBooks) {
long endDateMillis = book.endDate;
System.out.println("End Date: " + endDateMillis);
System.out.println("One Week Ago Millis: " + oneWeekAgoMillis);
System.out.println("******************************");
// If the due date falls within the last 1 week, add the item to the filteredList
if (endDateMillis >= oneWeekAgoMillis && endDateMillis <= currentTimeMillis) {
lastWeekList.add(book);
}
}
finishedBooksAdapter.setLastWeekFilter(lastWeekList);
}
My code didn't work as I wanted, it never enters the if block. So I used a System.out.println();
to see if the values meet the if conditions. Here's my logcat output:
I/System.out: End Date: 1684159940
I/System.out: One Week Ago Millis: 1683740684856
I/System.out: ******************************
I/System.out: End Date: 1683987159
I/System.out: One Week Ago Millis: 1683740684856
I/System.out: ******************************
Although my incoming data meets the conditions, the object I want is not included in the list. It does not enter the if block. lastWeekList.size()
returns 0.
I am grateful for your help in advance.
I was expecting the conditions to be met and these objects to be listed, but the list remains empty.
答案1
得分: 1
书中的日期 - 实际上是一个时间戳 - 不包括毫秒。请注意,长度少了3位数字。将它乘以1000以使其与其他时间戳可比较。
英文:
The date from the book -which is really a timestamp- doesn't include milliseconds. Notice that the length has 3 less digits. Multiply it by 1000 to make it comparable to the other timestamp.
答案2
得分: 0
book.endDate
似乎返回了一个计算错误的值。
long endDateMillis = book.endDate;
> I/System.out: 结束日期:1684159940
实际上,它多了1000的倍数。
您可以在 book
对象类内部修复此问题,或者简单地将 endDateMillis
乘以 1000。
long endDateMillis = book.endDate * 1000L;
英文:
It appears that book.endDate
is returning an incorrectly computed value.
long endDateMillis = book.endDate;
> I/System.out: End Date: 1684159940
Essentially, it is off by a multiple of 1000.
You can fix this either within the book
object class, or simply multiply endDateMillis
by 1000.
long endDateMillis = book.endDate * 1000L;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论