date.getTime() 差异问题

发布于 2024-10-08 19:51:19 字数 1417 浏览 0 评论 0原文

大家好,

我有一个永远不会通过的简单条件:

if(datas.date.getTime()-temps.date.getTime()>=5000)

我想检查从一个到另一个是否已经过去了 5 秒。


更新

这是 setter 和 getter:

public class Data{
    Date date;
    public Date getDate() {
        return date;
    }
    public void setDate() {
        Date now = new Date();
        this.date = now;
    }
}

所以,我称之为

Data data;
data.setDate();
processValues(data);

processValues:

public void processValues(Data dat){

    if(datas.size()==7){
        writeValues(datas);
        datas=new Vector<Data>();
        temps=new Vector<Data>();
    }
    temps.add(dat);
    datas.add(dat);
}

这是 writeValues:

public void writeValues(Vector<Data> datas){
    for(i=0;i<temps.size();i++)
        for(j=0;j<datas.size();j++){
            if(temps.elementAt(i).epc==datas.elementAt(j).epc)
                if(datas.elementAt(j).date.getTime()-temps.elementAt(i).date.getTime()>=5000)
                    try {
                        dao.writeToDatabase(datas.elementAt(j));
                        i=j;
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
        }

}

Greetings,

I have one simple condition that never passes:

if(datas.date.getTime()-temps.date.getTime()>=5000)

I want to check if it has been 5 seconds from one to another.


Update:

Here is setter and getter:

public class Data{
    Date date;
    public Date getDate() {
        return date;
    }
    public void setDate() {
        Date now = new Date();
        this.date = now;
    }
}

So, I call

Data data;
data.setDate();
processValues(data);

this is processValues:

public void processValues(Data dat){

    if(datas.size()==7){
        writeValues(datas);
        datas=new Vector<Data>();
        temps=new Vector<Data>();
    }
    temps.add(dat);
    datas.add(dat);
}

this is writeValues:

public void writeValues(Vector<Data> datas){
    for(i=0;i<temps.size();i++)
        for(j=0;j<datas.size();j++){
            if(temps.elementAt(i).epc==datas.elementAt(j).epc)
                if(datas.elementAt(j).date.getTime()-temps.elementAt(i).date.getTime()>=5000)
                    try {
                        dao.writeToDatabase(datas.elementAt(j));
                        i=j;
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧瑾黎汐 2024-10-15 19:51:19

如果输出datas.date.getTime()temps.date.getTime(),哪个更大?我的猜测是,它们被颠倒了,减法给出了一个负数 - 这当然永远不会大于 5000。或者数据点相距不超过 5 秒。

If you output datas.date.getTime() and temps.date.getTime(), which one is higher? My guess is that they are reversed and the subtraction is giving a negative number - which of course would never be greater than 5000. Or the data points aren't 5 seconds apart.

轮廓§ 2024-10-15 19:51:19

您说您的 processValues() 方法如下所示:

public void processValues(Data dat){
    if(datas.size()==7){
        writeValues(datas);
        datas=new Vector<Data>();
        temps=new Vector<Data>();
    }
    temps.add(dat);
    datas.add(dat);
}

您将相同的实例(“dat”)添加到两个向量。如果您更新其中一个,则必然会更新另一个,因此 date 字段中永远不会有差异。

You said that your processValues() method looks like this:

public void processValues(Data dat){
    if(datas.size()==7){
        writeValues(datas);
        datas=new Vector<Data>();
        temps=new Vector<Data>();
    }
    temps.add(dat);
    datas.add(dat);
}

You're adding the same instance ("dat") to both vectors. If you update one it will necessarily update the other, so there can never be a difference in the date field.

知足的幸福 2024-10-15 19:51:19

您说您有一个如下所示的 getter:

public Date getDate() {
    return date;
}

Date 对象是可变的,因此您可能应该进行防御性复制。您可以使用clone()来实现此目的。

根据这一点和问题的症状,也许您的两个 Date 对象实际上是同一个对象。您是否尝试过使用 == (或 !=)来确认它们确实是单独的对象?

更新

根据更新的信息,我可以明白为什么这种情况永远不会过去。但是,我不太明白您希望代码做什么。您本质上只是测试是否可以在 5 秒内创建 7 个对象,如果不能,则将其中一些写出来。无论哪种方式,您都会清除 tempsdatas,无论您是否写出对象。我的猜测是您不想清除这些未写出的元素向量。我也不明白为什么你同时拥有 datastemps 当它们包含完全相同的元素时。

You say you have a getter that looks like this:

public Date getDate() {
    return date;
}

Date objects are mutable, so you should probably copy defensively. You can use clone() for this.

Based on that and the syptoms of your problem, perhaps your two Date objects are actually the same object. Have you tried using == (or !=) to confirm that they are indeed separate objects?

Update

Based on the updated information I can see why that condition never passes. I don't really understand what you want you code to do, however. You're essentially just testing if you can create 7 objects in less than 5 seconds, and if you can't then you write some of them out. Either way you clear temps and datas, whether you wrote the objects out or not. My guess is that you do not want to clear these vectors of elements that were not written out. I also don;t understnad why you have both datas and temps when they contain exactly the same elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文