Java实现定时重启电脑

public static void main(String[] args) {
	SimpleDateFormat simpleDateFormatH = new SimpleDateFormat("HH");
		SimpleDateFormat simpleDateFormatM = new SimpleDateFormat("mm");
		SimpleDateFormat simpleDateFormatS = new SimpleDateFormat("ss");
		String hh = simpleDateFormatH.format(new Date());
		String mm = simpleDateFormatM.format(new Date());
		String ss = simpleDateFormatS.format(new Date());
		int Sum = 0;
		if(18>Integer.parseInt(hh)) {
			int h1 = 16-Integer.parseInt(hh);
			Sum = h1*60*60-Integer.parseInt(mm)*60-Integer.parseInt(ss);
		}else if(18<Integer.parseInt(hh)) {
			int h1 = Integer.parseInt(hh)-14;
			Sum = h1*60*60-Integer.parseInt(mm)*60-Integer.parseInt(ss);
		}else if(18==Integer.parseInt(hh)) {
			if(Integer.parseInt(mm)==0&&Integer.parseInt(ss)==0) {//这个情况是刚好6点00.00启动的软件
				Sum = 24*60*60;
			}else {//情况为6点零几分
				Sum = 24*60*60-Integer.parseInt(mm)*60-Integer.parseInt(ss);
			}
		}
		System.out.println("liuchang ########### 系统时间:"+hh+"--"+mm+"--"+ss);
		System.out.println("liuchang ############# Sum:"+Sum);
		try {
				Runtime.getRuntime().exec("shutdown -r -t "+Sum);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
}

重启电脑的命令使用的是cmd命令重启电脑
shutdown -s -t 60,s即shutdown的缩写,t即time的缩写,意思是60秒后关机。
shutdown -r -t 60,r即restart的缩写,意思是60秒后重启;
shutdown -l -t 60,l即logout的缩写,意思是60秒后注销;
shutdown -h -t 60,h即hibernate的缩写,意思是60秒后休眠;
shutdown -a,a即all的缩写,意思是取消所有的shutdown命令,但是不能取消上一步的命令;
当执行了重启命令后,在倒计时内使用cmd窗口 shutdown -a命令就可以取消重启。测试会用到的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值