Monday 7 July 2014

Stop Watch in JAVA


The following code displays the seconds. Starts from 60 to 1 second.
You can modify the following code as your requirement. The variable delay 1000 means of 1 second..

Code:

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Timer;
import java.util.TimerTask;

public class StopWatch {
static int interval;
static Timer timer;

 public static void main(String[] args) throws UnknownHostException, IOException   {

    System.out.print("Counter : ");
    String secs = "60";

    int delay = 1000;
    int period = 1000;
    timer = new Timer();
    interval = Integer.parseInt(secs);
    System.out.print(" "+secs);
    timer.scheduleAtFixedRate(new TimerTask() {public void run() {System.out.print(" "+setInterval());} }, delay, period);
}

private static final int setInterval() {
   
    if (interval == 2)
        timer.cancel();
    return --interval;
}
}

No comments:

Post a Comment