转载:http://hi.baidu.com/triceratops/blog
import java.io.*;
import javax.swing.JOptionPane;
import java.util.Calendar;
import java.io.Serializable;
public class TimeController{
private Timer timer;
private File f = new File("timer");
public void execute(int hour) throws IOException,ClassNotFoundException,InterruptedException{
FileInputStream fis;
ObjectInputStream ois;
while(true){
try{
fis = new FileInputStream(f);
ois = new ObjectInputStream(fis);
timer = (Timer)ois.readObject();
ois.close();
}
catch(FileNotFoundException ioe){
saveTimer(new Timer());
continue;
}
if(timer.isTimeExhausted() && timer.isTodayOpened()){
closePC();
JOptionPane.showMessageDialog(null,"今日开机时间已经满"+hour+"小时!","警告!",0);
break;
}
if(!timer.isTodayOpened()){
timer.setTimeExhausted(false);
timer.setDate();
}
timer.setStartTime();
Thread.sleep(10*60*1000);
timer.setCurrentTime();
timer.addEclipsedSeconds();
saveTimer(timer);
if(timer.isShutdownTime(hour)){
closePC();
timer.reset();
saveTimer(timer);
JOptionPane.showMessageDialog(null,"请注意:10分钟后将自动关机!","警告!",0);
break;
}
}
}
private void saveTimer(Timer timer) throws IOException{
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(timer);
oos.close();
}
private void closePC() {
new Thread(new Runnable(){
public void run() {
try{
if(timer.getEclipsedSeconds() != 0)
Thread.sleep(10*60*1000);
Runtime.getRuntime().exec("shutdown -s");
}
catch(Exception e){
e.printStackTrace();
}
}
}).start();
}
public static void main(String[] args) throws Exception{
int hour = 3;
if(args.length != 0)
hour = Integer.parseInt(args[0]);
new TimeController().execute(hour);
}
}
=============================================
public class Timer implements Serializable{
transient private long startTime;
transient private long currentTime;
private long eclipsedSeconds;
private boolean timeExhausted;
private int date;
public Timer(){
setStartTime();
setCurrentTime();
setDate();
}
public void setStartTime(){
startTime = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)*60*60+
Calendar.getInstance().get(Calendar.MINUTE)*60+
Calendar.getInstance().get(Calendar.SECOND);
}
public long getStartTime(){
return startTime;
}
public void setCurrentTime(){
currentTime = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)*60*60+
Calendar.getInstance().get(Calendar.MINUTE)*60+
Calendar.getInstance().get(Calendar.SECOND);
}
public long getCurrentTime(){
return currentTime;
}
public void addEclipsedSeconds(){
eclipsedSeconds += currentTime - startTime;
}
public long getEclipsedSeconds(){
return eclipsedSeconds;
}
public void setDate(){
date = Calendar.getInstance().get(Calendar.DATE);
}
public int getDate(){
return date;
}
public void setTimeExhausted(boolean timeExhausted){
this.timeExhausted = timeExhausted;
}
public boolean isTimeExhausted(){
return timeExhausted;
}
public boolean isTodayOpened(){
return date == Calendar.getInstance().get(Calendar.DATE);
}
public boolean isShutdownTime(int hour){
if(eclipsedSeconds>=hour*60*60)
return true;
return false;
}
public void reset(){
eclipsedSeconds = 0;
setTimeExhausted(true);
}
public String toString(){
String eclipsedTime = "今日累计使用了:";
long temp = getEclipsedSeconds();
eclipsedTime += temp/(60*60) + "小时";
temp = temp%(60*60);
eclipsedTime += temp/60 + "分";
temp = temp%60;
eclipsedTime += temp + "秒";
return eclipsedTime;
}
}