Im currently writing a program that runs a class that implements runnable
.
I have it so the time in a format of HH:MM:SS
is printed to the screen every second.
Heres the code:
public class LaunchCounter
{
public static void main(String[] args)
{
//Runs the CounterThread
new CounterThread().start();
}
}
And here is the counter class
public class CounterThread implements Runnable
{
//Declare new thread
private Thread thread;
public void start()
{
thread = new Thread(this, "");
thread.start();
}
@Override
public void run()
{
//Formatter used to display just time not date
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
//never ending forloop to display time
for(int i = 1; i > 0; i++)
{
try
{
//Runtime.getRuntime().exec( "cmd /c cls" );
//Sleep for 1 second after each loop
Thread.sleep(1000);
//new calender is created
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
}
catch(Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
This works perfectly fine.
What i am trying to achieve is that the line that is printed is cleared after waiting a second, and the the new time is printed and so on.
So 12:00:01
becocomes 12:00:02
with out taking a new line.
I've tried System.out.print("\b\b\b\b\b\b\b")
and Runtime.getRuntime().exec( "cmd /c cls" );
But this is just printing squares to the console.
How would i achieve this?