I am trying to create a code that calls the system time and updates it every minute. Can anybody give me an example that will steer me in the right direction? thanks
I think that you're looking for a Timer. It can schedule a task such as updating anything every minute.
public class MyScheduledTask extends TimerTask{
public void run(){
System.out.println("Message printed every minute");
}
}
public class Main{
public static void main(String... args){
Timer timer = new Timer();
timer.schedule(new MyScheduledTask(), 0, 60*1000);
//Do something that takes time
}
}
For the current system time you can use System.currentTimeMillis().
Resources :
If you were just looking to create a timer you could create a Thread to execute every second within an infinite loop
public class SystemTime extends Thread {
@Override
public void run(){
while (true){
String time = new SimpleDateFormat("HH:MM:ss").format(Calendar.getInstance().getTime());
System.out.println(time);
try{
Thread.sleep(1000);
} catch (InterruptedException ie){
return;
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With