Main
import java.util.Timer;
public class Main {
public static void main(String[] args) {
Timer timer = new Timer();
MyTask myTask = new MyTask();
timer.schedule(myTask,1000,2000);
}
}
MyTask
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimerTask;
public class MyTask extends TimerTask {
public void run() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(LocalDateTime.now().format(dateTimeFormatter));
}
}