java 动态显示时间 结果不对啊???
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class time {
public static void main(String[] args) {
Date date=new Date(System.currentTimeMillis());
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String time=df.format(date);
System.out.println(time);
}
}
}
[解决办法]
你这样做永远显示的都是最开始的时间。我想你的本意是每隔一段时间显示当前的时间。
Date date=new Date(System.currentTimeMillis());
把这一行放到你的while循环内部就可以了。
[解决办法]