package com.hotel.domain;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.hotel.utils.XmlHelper;

//.getLogger(BJTime.class)
public class BJTime {

	private transient final static Logger log = LogManager.getLogger(BJTime.class);
    public static void main(String[] args) {
        
        String url="http://www.time.ac.cn/timeflash.asp?user=flash";
        try {
            String xml = sendGETRequest(url,null,"gb2312");
            String formatTime = getTime(xml);//如2014-2-11 9:50:4
            String[] times = formatTime.split(" ");
            setTime(times[1],times[0]);
            log.info("设置北京时间成功");
        }
        catch (Exception e) {
            log.error("设置北京时间失败,请检查http://www.time.ac.cn/timeflash.asp?user=flash是否可以访问");
        }
    }
    /**
     * 发送GET请求
     * @param path 请求路径
     * @param params 请求参数
     * @param encoding 编码
     * @return 请求是否成功
     */
    private static String sendGETRequest(String path, Map<String, String> params, String ecoding) throws Exception{
        // http://192.168.1.100:8080/web/ManageServlet?title=xxx&timelength=90
        StringBuilder url = new StringBuilder(path);
        if (params != null && !params.isEmpty()) {
            url.append("?");
            for (Map.Entry<String, String> entry : params.entrySet()) {
                url.append(entry.getKey()).append("=");
                url.append(URLEncoder.encode(entry.getValue(), ecoding));
                url.append("&");
            }
            url.deleteCharAt(url.length() - 1);
        }
        HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if(conn.getResponseCode() == 200){
            
            return read(conn.getInputStream());
        }
        return null;
    }
    public static String read(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len = inStream.read(buffer)) != -1 ){
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return new String(outStream.toByteArray(),"gb2312");
    }
    public static String  getTime(String xmlStr) {
//        String path = System.getProperty("user.dir") + "/time.xml";
//        String xmlStr = XMLDocument.getXmlFromFile(path);
        System.out.println(xmlStr);
        Document doc = null;
        try{
            doc = DocumentHelper.parseText(xmlStr);// 将xml字符串转化为dom4j对象
            Element root = doc.getRootElement();// 最大的Asset
            Element timeElement =XmlHelper.getInstance().getSingleElementByName(root, "time");
            String value = "";
            Element subElement =XmlHelper.getInstance().getSingleElementByName(timeElement, "year");//
            String year= subElement.getText();
            subElement =XmlHelper.getInstance().getSingleElementByName(timeElement, "month");//
            String month= subElement.getText();
            subElement =XmlHelper.getInstance().getSingleElementByName(timeElement, "day");//
            String day= subElement.getText();
            subElement =XmlHelper.getInstance().getSingleElementByName(timeElement, "hour");//
            String hour= subElement.getText();
            subElement =XmlHelper.getInstance().getSingleElementByName(timeElement, "minite");//
            String minite= subElement.getText();
            subElement =XmlHelper.getInstance().getSingleElementByName(timeElement, "second");//
            String second= subElement.getText();
            value=year+"-"+month+"-"+day+" "+hour+":"+minite+":"+second;
            String[] times = value.split(" ");
            System.out.println(value);
            System.out.println(times[0]);
            System.out.println(times[1]);
            return value;
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * {方法功能中文描述}
     * 
     * @param time 时间 如“2014-2-11”
     * @param date 日期 如“9:50:4”
     * @author: wanghaotao
     */
    public static void setTime(String time,String date){
        String osName = System.getProperty("os.name");  
        String cmdTime = "";  
        String cmdDate = "";  
        try {  
            cmdTime = "  cmd /c time "+time;  //  cmd /c time 22:35:00
            cmdDate = " cmd /c date "+date;  // cmd /c date 2009-03-26
            if (osName.matches("^(?i)Windows.*$")) {// Window 系统  
            // 格式 HH:mm:ss  
            Runtime.getRuntime().exec(cmdTime);  
            // 格式:yyyy-MM-dd  
            Runtime.getRuntime().exec(cmdDate);  
        } else {// Linux 系统  
            
            cmdTime = "  date -s "+time.replaceAll("-", "");  //  date -s 20090326
            cmdDate = "  date -s "+date;  //  date -s 22:35:00
           // 格式:yyyyMMdd  
           Runtime.getRuntime().exec(cmdDate);  
           // 格式 HH:mm:ss  
           Runtime.getRuntime().exec(cmdTime);  
        }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
       }
}
最近下载更多
annazhang  LV29 2021年9月25日
tanyc2008  LV6 2020年12月1日
xiehao1020  LV1 2020年3月5日
李慧都  LV2 2019年1月24日
122374655  LV1 2017年3月27日
zouquan1027  LV2 2016年5月22日
zhangleisz  LV1 2016年2月17日
zw5097  LV23 2016年2月15日
George  LV26 2014年3月1日
最近浏览更多
冰糖雪里  LV3 2023年11月10日
qingfengqingzhou  LV1 2022年4月22日
JSPMVC 2021年12月9日
暂无贡献等级
annazhang  LV29 2021年9月25日
dengkaixing  LV2 2021年5月26日
tanyc2008  LV6 2020年12月1日
152162355  LV3 2020年5月25日
lsq54365  LV14 2020年5月20日
qianyunjie  LV8 2020年4月14日
送快递俊  LV3 2020年4月14日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友