package cn.hp.demo01;
import java.io.IOException;
import java.util.Date;
public class Demo02 {
public static void main(String[] args) throws InterruptedException, IOException {
//倒计时多少米秒后关机
int s = 60;
//60秒后的时间
Date mb = new Date();
mb.setMinutes(mb.getMinutes()+1);//在原有分钟+1
long a=mb.getTime();//从1970年至今的毫秒数
while (true){
Date now = new Date();//当前时间
long b = now.getTime();//从1970年至今的毫秒数
//时间差
long c = a-b;
//倒计时显示
System.out.println("剩余"+c/1000+"秒关机");
//是否时间到了,是否c<=0
if (c<=0){
System.out.println("时间到了,开始关机");
//Runtime是计算机环境,getRuntime()获取环境对象
Runtime runtime = Runtime.getRuntime();
runtime.exec("shutdown -s -t 60 -f");//执行计算机名命令的方法
break;//循环结束
}
//休眠
Thread.sleep(1000);
}
}
}
暑期编程PK赛
得CSDN机械键盘等精美礼品!