将上周落在列表上的对象放在比较日期时。

huangapple go评论70阅读模式
英文:

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&lt;FinishedBooks&gt; finishedBooks) {
    List&lt;FinishedBooks&gt; lastWeekList = new ArrayList&lt;&gt;();

    // 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(&quot;End Date: &quot; + endDateMillis);
        System.out.println(&quot;One Week Ago Millis: &quot; + oneWeekAgoMillis);
        System.out.println(&quot;******************************&quot;);

        // If the due date falls within the last 1 week, add the item to the filteredList
        if (endDateMillis &gt;= oneWeekAgoMillis &amp;&amp; endDateMillis &lt;= 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;

huangapple
  • 本文由 发表于 2023年5月18日 02:21:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定