定时关闭

 

public class guan {
    public static void main(String[] args) {
        //多长时间关机
        new Thread(()->{
            long t1 = System.currentTimeMillis();
            while (true){
                long ok =System.currentTimeMillis()-t1;//当前时间减去当前时间
                if (ok>=5*1000)break;//时间大于5秒,出循环
            }
            System.out.println("正在关机一");
        }).start();

    new Thread(()->{
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("系统正在关机二");
    }).start();

    //z指定时间关机
        new Thread(()->{
            String s ="2021-1-30 9:25:59";
            SimpleDateFormat lsp =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long end =0;
            try {
                end=lsp.parse(s).getTime();//获取指定时间
            } catch (ParseException e) {
                e.printStackTrace();
            }
            try {//减去当前时间
                TimeUnit.MILLISECONDS.sleep(end-System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.printf("%s-系统正在关机%n",s);
        }).start();

    }

 

posted @ 2021-01-30 09:43  谷先生  阅读(382)  评论(0)    收藏  举报