6

I'm trying to write a console program to display the current time recursively on the same line and position.

I tried a lot but it is getting printed in multiple line using normal System.out.println, so I used PrintStream object. Now the thing is that it is appearing in the same position, but not getting updated.

I couldn't be sure whether problem is with thread or PrintStream. Please help me with this problem or is there any other method to make things appear recursively on the same position in a console app?

import java.util.Date;
import java.io.IOException;
import java.io.*;

public class DateDemo {

  // boolean flag=true;

  public static void main(String args[]) {
    DisplayTime dt=new DisplayTime();
    Thread thread1=new Thread(dt,"MyThread");
    //thread1.start();
    thread1.run();
  }
}


class DisplayTime extends Thread {
  public void run() {  
    try {
      while(true){
        showTime();
        Thread.sleep(1000);
        
      }
    } catch(InterruptedException e){
      System.err.println(e);
    }
}

public void showTime(){
    try {
      PrintStream original = new PrintStream(System.out);

      // replace the System.out, here I redirect to 
      System.setOut(new PrintStream(new FileOutputStream("stdout.log")));

      // System.out.println("bar");  // no output
      Date date =new Date();
      original.print(date.toString());
      // System.out.println(date.toString());
      
      System.out.flush();
      // output to stdout
      // original.close();
    } catch(Exception e){
      System.err.println(e);
    }
}

}

informatik01
  • 16,038
  • 10
  • 74
  • 104
rrk
  • 109
  • 1
  • 3
  • 11
  • first of all, use Thread.start() instead of Thread.run() to execute a thread. – Sanjeev Jun 30 '14 at 10:29
  • yes, you are write Sanjeev calling Thread.run() will execute run() method in the same thread, without starting new thread. So use Thread.start() instead. – 4aRk Kn1gh7 Jun 30 '14 at 10:42

4 Answers4

9

First your title is misleading, please read what Recursion means.

What you need to do is to override the curret line with the new time. This is done in a console programm by using the the carriage return charcter \r (to go back one character you can use the backspace character \b)

You could try something like this (Note that for this to work you must not print a new line charachter \n at the end of your line ):

import java.text.SimpleDateFormat;
import java.util.Date;

public class Time {

    public static void main(String... args) throws InterruptedException {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        while(true) {
            System.out.printf("\r%s", sdf.format(new Date()));
            Thread.sleep(1000);
        }
    }
}
A4L
  • 17,353
  • 6
  • 49
  • 70
1

Another way is to delete a number of chars from the current line ((char)8) :

public class Time {

    public static void main(String... args) throws InterruptedException {
        while (true) {
            Date now = new Date();
            System.out.print(now.toString());
            Thread.sleep(1000);
            for (int i = 0; i < now.toString().length(); i++) {
                System.out.print((char) 8);
            }
        }
    }
}

But like A4L's answer, this won't work in every console, i.e. Eclipse.

Stefan
  • 12,108
  • 5
  • 47
  • 66
0

If your problem with system.out.println() was that it printed always another line then just try using system.out.print() and after printing just give it a system.out.print(\n) or system.print.out.println()

Neso
  • 86
  • 5
0

I've never used this library before, but I think it should give you what you want. Basically the java equivalent of old skool curses library for terminal applications.

http://sourceforge.net/projects/javacurses/

Another fellow on stackoverflow mentioned lanterna -> https://stackoverflow.com/a/12896503/3465651

Community
  • 1
  • 1
user3465651
  • 706
  • 1
  • 4
  • 22