using a timer
Does anyone know how to write a program that uses a timer to print the current time once a second. Apparently this code is useful:
Date now = new Date();
System.out.println(now);
[186 byte] By [
Mrnewbie] at [2007-11-11 6:49:11]

# 1 Re: using a timer
import java.util.*;
import java.text.*;
public class Timer implements Runnable {
static SimpleDateFormat df=new SimpleDateFormat("dd.MM.yy hh:mm.ss");
Thread ticker=null;
public Timer() {}
public void start() {
if (ticker!=null && ticker.isAlive()) {
System.err.println("Timer is already running");
return;
}
ticker=new Thread(this);
ticker.start();
}
public void stop () {
ticker.interrupt();
ticker=null;
}
public void run() {
System.out.println("Timer started");
while (true) {
try {
ticker.sleep(1000);
}
catch (InterruptedException ex) {
System.out.println("Timer stopping");
return;
}
System.out.println(df.format(new java.util.Date(System.currentTimeMillis())));
}
}
public static void main(String[] args) {
Timer t = new Timer();
t.start();
}
}
sjalle at 2007-11-11 22:40:15 >
