用java写了个日历怎样用Thread来刷新时间

来源:百度知道 编辑:UC知道 时间:2024/06/01 22:43:25
最好写出有关代码,时间代码就不用写了,有点解释更好

class Timer implements Runnable{
private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void run() {
while(true){
try{
Calendar c = Calendar.getInstance();
System.out.println(df.format(c.getTime()));
Thread.sleep(1000);
}catch(Throwable t){
t.printStackTrace();
}
}
}

public static void main(String[] args) {
Thread t = new Thread(new Timer());
t.start();
}

}

String timeStr = "";
class RefreshTimeThread extends Thread{
public void run(){
while(true){
timeStr = (new Date()).toLocaleString();//得到当前时间
// TODO: 将timeStr设置到显示区域进行刷新
try {
Thread.sleep(1000); // 设置线程sleep间歇时间为1秒
} catch (InterruptedException e) {
e.printStackTrace();