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);
}

}
}  

解决方案 »

  1.   

    你这样做永远显示的都是最开始的时间。我想你的本意是每隔一段时间显示当前的时间。Date date=new Date(System.currentTimeMillis());
    把这一行放到你的while循环内部就可以了。
      

  2.   

    在while外面声明一个date,
    Date date = new Date();然后再while里面给date赋值,
    date = new Date(System.currentTimeMillis());这样就ok了。
      

  3.   

    刚才用定时器写了一个,参考下吧:
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    /**
     * 测试.
     * 
     * @author Ruidong_pu
     * @version 2013.3.8
     */
    public class TestTop  {

    public TestTop(){
    showTime();//从构造函数调用.
    }

    public static void showTime(){
    new Timer().schedule(new TimerTask() {//匿名定时器对象.
    @Override
    public void run() {
    // TODO Auto-generated method stub
    Calendar ca = Calendar.getInstance();
    System.out.println(ca.get(Calendar.YEAR)+"年"+(ca.get(Calendar.MONTH)+1)+"月"+ca.get(Calendar.DAY_OF_MONTH)+"日"+ca.get(Calendar.HOUR_OF_DAY)+"时"+ca.get(Calendar.MINUTE)+"分"+ca.get(Calendar.SECOND)+"秒");
    }
    },1000,1000);

    }

    public static void main(String args[]) {
    // TODO Auto-generated method stu

    }
    }