GitHub project address: https://github.com/Snowstorm0/learn-sleep
Gitee project address: https://gitee.com/Snowstorm0/learn-sleep
When Java runs a for loop, it is hoped that each loop can pause for a period of time.
Just use the sleep method:
for (int i = 0; i < 10; i++) {
try {
System.out.println("i:" + i + " time:" + getTme());
Thread.sleep(10 * 1000); // 暂停10秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Then define the time format:
Long current_time = System.currentTimeMillis();
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String formateTime = formate.format(current_time);
After running, you can see the output:
i:0 time:15:35:06
i:1 time:15:35:16
i:2 time:15:35:26
i:3 time:15:35:36
i:4 time:15:35:46
Pause for 10 seconds successfully.
To learn more programming knowledge, please follow my official account: