我有一个应用程序可以跟踪顾客在餐厅的等待时间。它只是从当前时间中减去客户开始等待的时间,然后使用 StandardDateFormat 将其格式化为 hh:mm 并将其显示为字符串。
问题是计时器总是从 6 小时开始,例如 06:01。
ActionListener actListner = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
aTable.updateTime();
}
};
Timer timer = new Timer(1000, actListner);
timer.start();
}
这是在主程序中
public void updateTime()
{
data.updateTime();
fireTableDataChanged();
fireTableRowsInserted(data.name.size() - 1, data.name.size() - 1);
}
这是在表模型中
public void updateTime()
{
Date newTime = new Date();
for (int i = 0; i < startTime.size(); i++)
{
this.setTimeWaiting(i, hoursMin.format(new Date(newTime.getTime() - startTime.get(i).getTime())));
}
}
public void setTimeWaiting(int index, Object newVar)
{
timeWaiting.remove(index);
timeWaiting.add(index, newVar.toString());
}
这是在数据模型中。
每次添加新行时,都会将该行添加的时间放入一列中,然后将该人在另一列中等待的时间,但等待列提前了 6 小时。否则它工作正常。
最佳答案
这听起来像是时区问题。也许您应该将日期格式的时区设置为 UTC。
关于java - 日期数学后显示错误的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22726570/