Java Data Type How to - Use timer task to output time every five seconds


We would like to know how to use timer task to output time every five seconds.


import java.util.Timer;
import java.util.TimerTask;
/*  ww  w. j  ava 2  s .co  m*/
public class Main {

  public String CurrentDate() {
    java.util.Date dt = new java.util.Date();
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
    String currentTime = sdf.format(dt);
    return currentTime;

  public static void main(String[] args) {
    class SayHello extends TimerTask {
      Main thisObj = new Main();

      public void run() {
        String todaysdate = thisObj.CurrentDate();
    Timer timer = new Timer();
    timer.schedule(new SayHello(), 0, 5000);


The code above generates the following result.