我哪里搞错了?我试图每秒在窗口中显示当前时间。 但它显示
ActionListener AL = new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent)
{
Date currentTime=gregorianCalendarCurrrentTime.getTime();
JLabelCurrentTime.setText(String.valueOf(currentTime));
}
};
Timer timer = new Timer(1000, AL);
timer.start();
最佳答案
假设 gregorianCalendarCurrentTime
的类型为 java.util.GregorianCalendar
,则方法 getTime()
返回一个 Date
code> 对象,它表示日历对象的时间,该时间在创建时或最后一次使用日历对象的设置方法后为“当前”。该方法无法确定当前时间。要将当前时间设置为标签文本,您只需使用 new Date()
。
LabelCurrentTime.setText(String.valueOf(new Date()));
关于java - Timer + gregorianCalendar.getTime() 未正确刷新日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31939753/