java模拟十秒以内倒计时

package com.lingshang.status;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Sleep {

/**
 * @param args
 * @throws InterruptedException
 */
public static void main(String[] args) throws InterruptedException {
	// TODO Auto-generated method stub
	/*
	 * int num=10; while(true){ System.out.println(num--);
	 * Thread.sleep(1000); if(num<=0){ break; } }
	 */

	Date endTime = new Date(System.currentTimeMillis() + 10 * 1000);
	long end = endTime.getTime();
	while (true) {
		System.out.println(new SimpleDateFormat("mm:ss").format(endTime));
		endTime = new Date(endTime.getTime() -1000);
		Thread.sleep(1000);
		if (end - 10000 > endTime.getTime()) {
			break;
		}
	}

}

}

你可能感兴趣的:(java)