package test;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class ShutDown {
	/* 转换成毫秒数 */
	public static int getTimemills(int hours, int min) {
		return (hours * 60 * 60 + min * 60) * 1000;
	}

	public static void main(String[] args) {
		Calendar c = Calendar.getInstance();
		Date d = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

		System.out.println("当前时间:" + sdf.format(d));
		System.out.println("请输入定时关机睡觉时间:");
		Scanner input = new Scanner(System.in);
		System.out.println("时:");
		int hours = input.nextInt();
		System.out.println("分:");
		int min = input.nextInt();

		int nowHour = c.get(c.HOUR_OF_DAY);
		int nowMin = c.get(c.MINUTE);
		if (hours < nowHour) {
			hours += 24;
		}
		int pretime = ShutDown.getTimemills(hours, min);

		int nowtime = ShutDown.getTimemills(nowHour, nowMin);
		int exectime = pretime - nowtime;
		System.out.println((exectime / 1000 / 60) + "分钟后准备关机");


		System.out.println("是否设置定时关机?(是/否)");
		String flag = input.next();
		if (flag.equals("是")) {
			System.out.println("准备关机中。。。");
			new Timer().schedule(new TimerTask() {

				@Override
				public void run() {
					try {
						Runtime.getRuntime().exec("shutdown -s");
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}, exectime);


		} else {
			System.out.println("退出系统");
		}

	}
}