4
I want to show the system time and the seconds counting in real time in Java.
4
I want to show the system time and the seconds counting in real time in Java.
10
You can use the Java service scheduler to display the hours every second.
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;
public class Relogio {
public static void main(String args[]) throws IOException {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(
new Runnable() {
public void run() {
System.out.println(new SimpleDateFormat("HH:mm:ss").format(new Date()));
}
}, 1, 1, TimeUnit.SECONDS);
}
}
No need to use the class Thread
, or use the method sleep
and nor do incorrect treatment of exception.
Thanks bro, you’re the man ;)
Very good code, much cleaner and readable than I learned to do. I didn’t know this ScheduledExecutorService
take a look at it. I think I can optimize some of my codes. Thanks!
4
According to that response in the OS you can do it like this:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Thread th = new Thread(new Runnable() { //cria uma thread
public void run() {
while(true) { //roda indefinidamente
Date date = Calendar.getInstance().getTime(); //pega a hora do sistema
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String today = formatter.format(date);
System.out.println(today);
try {
Thread.sleep(1000); //espera 1 segundo para fazer a nova evolução
} catch(InterruptedException ex){
//é algo terrível a se fazer mas pelo jeito a API medonha do Java exige
}
}
}
});
th.start();
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
It may not give the best result but that’s it. There are no guarantees that all screen updates will occur every exact 1 second.
It has to position the cursor always in the same position but as I have no way to test I will just leave the code for you to try:
System.out.print(String.format("%c[%d;%df", 0x1B, 0, 0));
thanks. It worked! Thanks for the help.
2
I think this is what you want
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JLabel;
public class relogio {
public static void main(String[] args) {
try {
while (true) {
Date d = new Date();
StringBuffer data = new StringBuffer();
SimpleDateFormat sdfData = new SimpleDateFormat("dd.MM.yyyy");
data.append(sdfData.format(d));
data.append(" - ");
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println(""+data.toString() + sdf.format(d));
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Problema na atualização da data/hora");
ex.printStackTrace();
}
}
}
Thanks for the layout, @jsantos1991 ... worked ;)
Browser other questions tagged java datetime
You are not signed in. Login or sign up in order to post.
I don’t understand, what it is "counting in real time in Java"?
– Maniero
@bigown, observes, if you show the hour, minutes and seconds in java, will present in a static way, ie, seconds will be only those that the last time was caught ... I want to be able to present it dynamic from 01 ... 59 ... you understand?
– user2647038
And you want to do this how? You want to show where?
– Maniero
@bigown, I want to show you on screen ... doing a System.out.println ...
– user2647038