Java控制台打印动态时间

实现方式:

1、使用定时器对象(Timer)

2、使用时间对象(SimpleDateFormat)

3、使用 ' \r ' 实现不换行打印(return 到当前行的最左边)

代码部分

  import java.util.Timer;   
  import java.util.TimerTask;

 1 public class PrintTime{
 2     public static void main(String[] args) {
 3 
 4         Timer timer = new Timer();
 5         //创建时间对象,获取当前系统时间
 6         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 7         //执行定时器任务
 8         timer.schedule(new TimerTask() {
 9             @Override
10             public void run() {
11                 System.out.print("\r当前时间:" + format.format(new java.util.Date()));
13             }
14         }, 0, 1000);      //0延迟,每隔1秒(1000毫秒)执行一次
15     
16     }
17 }    

猜你喜欢

转载自www.cnblogs.com/ITRonion/p/12384278.html
今日推荐