由于我下班之后从来不关电脑,以备远程连接,因此在周一到周五每晚十一点后执行睡眠命令,保证第二天电脑使用流畅。
import java.util.Calendar;
public class Main {
public static void main(String[] args) throws Exception {
while (true) {
Calendar cal = Calendar.getInstance();
System.out.println("执行----------- " + cal.getTime());
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
//不是星期五星期六
if (dayOfWeek != Calendar.FRIDAY && dayOfWeek != Calendar.SATURDAY) {
int hour = cal.get(Calendar.HOUR_OF_DAY);
//晚11点之后
if (hour >= 23) {
System.out.println("休眠");
Runtime.getRuntime().exec("rundll32.exe powrprof.dll,SetSuspendState 0,1,0");
}
}
Thread.sleep(1800 * 1000);
}
}
}