import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class dingshi { public static void main(String[] args){ ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); Runnable task =new Runnable() { @Override public void run() { Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); String format = simpleDateFormat.format(date); System.out.println(format); } }; scheduledExecutorService.scheduleAtFixedRate(task,0,1000, TimeUnit.MILLISECONDS); } }
注释:
TimeUnit.MILLISECONDS,表示单位为毫秒,前面的1000表示每1000毫秒也就是1秒执行一次