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
}
}
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;
}
}
}
}
# 1 楼答案
我想你在找一个^{} 。它可以安排任务,例如每分钟更新任何内容
对于当前系统时间,您可以使用
System.currentTimeMillis()
资源:
# 2 楼答案
如果您只是想创建一个计时器,那么可以创建一个线程,在无限循环中每秒执行一次