Countdown Timer In Java Using Threading

This is a simple Countdown program in Java which is done using threading. In this program the console is periodically cleared with a Console Object. The Basic design is to make the thread sleep for 1 sec till the give time is reached. The hours and Mins along with seconds are at first converted to seconds and a loop does the rest of the work !

Screenshot [Demo]



The Code:
import java.io.Console;
import java.util.Scanner;
public class timer
{
 public static final char ESC = 27;
 public static void main(String as[])throws Exception 
 {
  Scanner s=new Scanner(System.in);
  String tim;
  int hh,mm=0,ss=0;
  int sec;
  Console c = System.console();
  if (c == null) {
   System.err.println("no console");
   System.exit(1);
  }
  System.out.println("Enter In this format: \"HH:MM:SS\"");
  tim=s.next();
  hh=Integer.parseInt(tim.substring(0,2));
  mm=Integer.parseInt(tim.substring(3,5));
  ss=Integer.parseInt(tim.substring(6,8));
  sec=hh*3600+mm*60+ss;
  //Clearing Screen For The First Time !
  c.writer().print(ESC + "[2J");
  c.flush();
  for(int i=0;i<sec;i++)
  {
   try
   {
    c.writer().print(ESC + "[1;1H");
    c.flush();
    c.writer().println(tim+" :: "+(sec-i)+" Seconds Left !");
    c.flush();
    Thread.sleep(1000);
   }
   catch(Exception e)
   {
    System.out.println("Exception Was caught because: "+e);
   }
  }
   
 }
}


Found any suggestions/ Bugs ?
Please Report us !

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Bit Stuffing Code Implementation in Java

Employee Management System Using Inheritance in Java