控制上网!!!(版本之1.1) 当前时间从网络取得。

为了防止修改本地时间~

 

 

package com.sxz.timecontroal;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import java.util.Calendar;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

/**
 * 当可以连接网络时,取得网络上的时间使用。
 * 
 * @author sxz 2012/01/29
 */
public class CheckTImeWithNet {
	
	/**
	 * 获取时间数据所在的网点地址
	 */
	private static final String NET_TIME_URL = "http://open.baidu.com/special/time/";
	
	/**
	 * 获取时间数据所在行的key值
	 */
	private static final String NET_TIME_FIND_KEY_WORD = "window.baidu_time(";
	
	/**
	 * 网络上的时间
	 */
	private Calendar netDate = Calendar.getInstance();

	
	/**
	 * 获取当前 网络上的时间
	 */
	public Calendar getNetDate() {
		return netDate;
	}

	/**
	 * 设置当前 网络上的时间
	 */
	public void setNetDate(long netDate) {
		this.netDate.setTimeInMillis(netDate);
	}

	@SuppressWarnings("finally")
	public boolean checkTime() {
		
		boolean flag = true;
		
		try {
			// 创建连接
			HttpClient httpClient = new HttpClient();
		
			// 设置请求参数
			GetMethod getMethod = new GetMethod(NET_TIME_URL);
		
			// 设置读取网页数据时的编码格式
			getMethod.addRequestHeader( "Content ", "text/html,charset=GBK");
			
			// 进行网络连接,并获取返回结果code,根据这个code判断,当前是否连接到网络。
			int statusCode = httpClient.executeMethod(getMethod);
			
			if(statusCode != 200){
				System.out.println("网络数据有问题,有待调整获取资源的途径");
				return false;
			}
			
	        // 获取网页信息
			InputStream responseBody = getMethod.getResponseBodyAsStream();  
	        
			// 设置编码格式
            BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody,"GBK"));
            
            // 一行一行的取出信息
            String tempBf = null;  
            while((tempBf=reader.readLine()) != null){  
            	
            	// 寻找有效数据
            	// window.baidu_time(1327819975724);
                int timeRowCheckFlag = tempBf.indexOf(NET_TIME_FIND_KEY_WORD);
                
                // 找到网络时间数据所在行。
                if(timeRowCheckFlag > 0){
                	// 去掉数据中的无用数据,得到时间字符串
                	// window.baidu_time(1327819975724); ->1327819975724
                	String netTimeStr = tempBf.replaceAll("[^\\d]", "");
                	
                	// 当前网络时间 
                	long netTimeMillis = Long.parseLong(netTimeStr);
                	
                	// 设置当前网络上的时间
                	this.setNetDate(netTimeMillis);
                	
                	// 当找到网络上的的时间时,不必再从该网页上读取数据了!
                    break;
                }
             
            } 
	        
		} catch(UnknownHostException ex){
		
			// 网络连接失败
			flag = true;
			System.out.println("网络连接失败!");
			ex.printStackTrace();
		
		}catch (IOException e) {
			
			flag = true;
			System.out.println("IO错误,程序还需要调整!");
			e.printStackTrace();
			
		} catch(Exception e){
			flag = true;
			System.out.println("其他错误,程序还需要调整!");
			e.printStackTrace();
		}
		finally{
			return flag;
		}
	
	}

}

 

 

 

 

2

 

package com.sxz.timecontroal;

import java.util.Calendar;

/**
 * @author sxz 2012/01/28
 * @author sxz 2012/01/29
 */
public class TimeControal implements Runnable {
    
    private static int countFiveMinute = 0;
    
    /**
     * 程序每次检查的间隔时间设置为5分钟(300000)。
     */
    private static final int TIME_BETWEEN_CHECK = 300000;
    
    /**
     * 程序最多检查的次数( 次数(36) * 时间间隔 = 限定开机时间 )。
     */
    private static final int MAX_CHECK_COUNT = 36;
    
    /**
     * 线程
     */
    public void run() {
        
        try {
            
            CheckTImeWithNet netTime = new CheckTImeWithNet();
            
            // 连接网络,判断并设定当前时间。
            netTime.checkTime();
            
            // 获取当前时间,当连接到网络时,为网络上的时间
            Calendar date = netTime.getNetDate();
            
            // 控制1:超过当前时间22:30
            if(TimeControal.isNowTimeHaveToShutDown(date)){
                // 关机
                TimeControal.shutdown();
            }
            
            // 每隔5分钟再检查一次
            Thread.sleep(TIME_BETWEEN_CHECK);
            
            // 控制2:开机后时间超过3个小时
            if (TimeControal.startedTime()){
                // 关机
                TimeControal.shutdown();
            }
        
        } catch (InterruptedException e) {
            
            e.printStackTrace();
        }
    }
    
    /**
     *  @param 当前时间(当连接到网络时为网络上的时间,没有连接时为系统时间)
     *  
     * 当前时间判断,超过10:30关机
     * 
     * 超过限定时间,返回ture
     * 
     */
    public static boolean isNowTimeHaveToShutDown(Calendar date){
        
        // 是否超过限定时间,判定flag
        boolean flag = false;
        
        if(date.get(Calendar.HOUR_OF_DAY) >= 23 || date.get(Calendar.HOUR_OF_DAY) < 6){
            flag = true;
        }
        
        if(date.get(Calendar.HOUR_OF_DAY) == 22 && date.get(Calendar.MINUTE) > 30){
            flag = true;
        }
        
        return flag;
    }
    
    /**
     *  
     * 开机后时间是否超过3个小时判定。
     * 
     * 如果开机后达到3个小时,返回ture
     * 
     */
    public static boolean startedTime(){
        
        // 是否达到3个小时,判定flag
        boolean flag = false;
        
        // 间隔次数计数
        countFiveMinute ++;
        
        // 有36次5分钟的计时,表明已经3个小时了。
        if(countFiveMinute >= MAX_CHECK_COUNT){
            flag = true;
        }
        
        return flag;
    }
    
    /**
     * 1分钟内关机
     */
    public static void shutdown(){
        try { 

            // 1秒内自动关机
            // 原本想设定为60秒,但是当出现自动关机选项时
            // 你把系统时间往前调,你会发现关机时间也跟着增加
            // 同时,这个程序也有一个bug,就是如果开机后我就运行shutdown -s -t 9000000命令
            // 程序就不会关机了。
            Runtime.getRuntime().exec("shutdown -s -t 1"); 
         
        } catch (Exception e) { 
            e.printStackTrace(); 
        }
    }
}






3

 

 

package com.sxz.timecontroal;

/**
 * @author sxz 2012/01/28
 * @author sxz 2012/01/29
 */
public class MainTimeControal {

    /**
     * 时间控制
     */
    public static void main(String[] args) {
        
        TimeControal timeContraol = new TimeControal();
        
        Thread thread = new Thread(timeContraol);

        try {
            // 刚刚开机以后,可能还没有连接到网络
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        while (true) {
            
            // 使程序一直处于运行状态
            thread.run();
            
        }
    }
}


 

 

 

 

 

 

注意:

在使用jar包的工程中导出jar文件时。

 

我们要把我们引用的jar文件解开后,在导出jar文件。

 

也就是说导出后的jar文件里面没有jar文件!!

 

--------------------------------------------------------

 

如果你不这么做,程序是无法找到那些jar文件中引用的类的!!!

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值