【发布时间】:2021-10-03 13:04:59
【问题描述】:
我正在尝试制作一个每秒更新 currentTime 的程序,以便在控制台中它将变为 1s、2s、3s、4s 等等。
public static void main(String[] args) throws InterruptedException{
OSpanel runner = new OSpanel();
runner.currentTime();
}
public static void currentTime() throws InterruptedException{
if(true) {
Date currentTime = new Date();
while(true) {
Thread.sleep(1000);
System.out.println(currentTime);
Thread.sleep(1000);
System.out.println(currentTime);
}
}
}
【问题讨论】:
-
是否要从应用程序开始计算时间...
-
还是四舍五入到最接近的秒数? Java Date rounding - Stack Overflow
-
无关,但
if(true)应该做什么?此外,如果您想将 currentTime 函数用作实例方法,则它不应该是静态的