1. 程式人生 > 其它 >Java dos只輸出一行,單行重新整理案例,單行動態列印

Java dos只輸出一行,單行重新整理案例,單行動態列印

技術標籤:Javajava

import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo{
    public static void main(String[] args) throws InterruptedException {
        Date d;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stime;

        while
(true){ d=new Date(); long time = d.getTime(); Thread.sleep(1000); stime =sdf.format(time); System.out.print(stime+"\r"); } } }

主要是這一句

System.out.print(stime+"\r");

兩個要點

  1. .println去掉ln
  2. 末尾加上"\r"
    實現效果,時間是單行動態重新整理的。
    在這裡插入圖片描述