倒计时
public class 倒计时 {
public static void main(String[] args) {
Time time = new Time(10);
new Thread(time).start();
}
}
class Time implements Runnable{
private int times ;
Time(int times){
this.times = times;
}
public void run() {
try {
while(true) {
Thread.sleep(1000);
System.out.println(times--);
if(times < 0) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
时间戳
import java.text.SimpleDateFormat;
import java.util.Date;
public class 时间戳 {
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 (Exception e) {
e.printStackTrace();
}
}
}
}