This is supposed to print out the current time every second
import javax.swing.Timer;
public class Timer1{
final static int SPEED = 1000;
public static void main(String[] args){
Timer t = new Timer(SPEED, new TimerListener());
t.start();
}
}
import java.util.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent event){
System.out.println(new Date());
}
}
The above are the 2 classes in my program
I think you do not see any output because the program terminates before the timer gets a chance. If you add a delay at the end, you will see date/time lines.
In the code, I have also added an example with the
java.util.Timer
that alex2410 suggested: