date.getTime() 差异问题
大家好,
我有一个永远不会通过的简单条件:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果输出
datas.date.getTime()
和temps.date.getTime()
,哪个更大?我的猜测是,它们被颠倒了,减法给出了一个负数 - 这当然永远不会大于 5000。或者数据点相距不超过 5 秒。If you output
datas.date.getTime()
andtemps.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.您说您的 processValues() 方法如下所示:
您将相同的实例(“dat”)添加到两个向量。如果您更新其中一个,则必然会更新另一个,因此
date
字段中永远不会有差异。You said that your processValues() method looks like this:
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.您说您有一个如下所示的 getter:
Date
对象是可变的,因此您可能应该进行防御性复制。您可以使用clone()
来实现此目的。根据这一点和问题的症状,也许您的两个 Date 对象实际上是同一个对象。您是否尝试过使用
==
(或!=
)来确认它们确实是单独的对象?更新
根据更新的信息,我可以明白为什么这种情况永远不会过去。但是,我不太明白您希望代码做什么。您本质上只是测试是否可以在 5 秒内创建 7 个对象,如果不能,则将其中一些写出来。无论哪种方式,您都会清除
temps
和datas
,无论您是否写出对象。我的猜测是您不想清除这些未写出的元素向量。我也不明白为什么你同时拥有datas
和temps
当它们包含完全相同的元素时。You say you have a getter that looks like this:
Date
objects are mutable, so you should probably copy defensively. You can useclone()
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
anddatas
, 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 bothdatas
andtemps
when they contain exactly the same elements.