public class Countdown {
public static void main(String[] args) {
Date startTime = new Date(System.currentTimeMillis());//获取当前时间
while(true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime=new Date(System.currentTimeMillis());//更新当前时间,注意这步别忘了
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Java计时器的实现
最新推荐文章于 2025-10-02 11:32:09 发布
本文展示了一个使用Java实现的简单倒计时程序,通过不断更新并打印当前时间来模拟倒计时效果。程序利用了Java的Date类和SimpleDateFormat类来格式化和获取系统当前时间,同时使用Thread.sleep()方法实现每秒更新时间。
252

被折叠的 条评论
为什么被折叠?



