Create a scheduled task using timer
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; publicclass Main extends TimerTask { private DateFormat formatter = new SimpleDateFormat("hh:mm:ss a"); publicstaticvoid main(String[] args) { TimerTask task = new Main(); Timer timer = new Timer(); timer.scheduleAtFixedRate(task, new Date(), 1000); } publicvoid run() { System.out.println(formatter.format(new Date())); } }
Related examples in the same category