我想在应用程序中显示系统时间,我的代码是:
private Date date=new Date();
SimpleDateFormat formatter=new SimpleDateFormat("yyyy.mm.dd at hh:mm:ss");
String datestring=formatter.format(date);
Stautas1.settext(datestring);
可是这样显示的是一开始应用程序的打开时间,它不能随着时间的变化而变化,是一个死时间,我如何解决这样的问题那?
请各位执教!
用子线程,sleep(1000),然后刷新状态栏一次就行了
java的标准类库,import java.lang.thread类,用sleep方法
找本书看看线程方面的章节,你就会写了,很简单。
我根据你的问题自己写出来一个示意性的例子,希望能对你有所启发,代码如下:
public class DisplayDate implements Runnable{
private Thread aThread=null;
private Date date=new Date();
public static void main(String args[]){
DisplayDate displaydate=new DisplayDate();
displaydate.StartDisplay();
}
public void StartDisplay(){
aThread=Thread(this);
aThread.start();
}
public void run(){
while(true){
try{
//Add your display time code here;
Thread.sleep(1000);
}catch(Exception e){
}
}
}
}