日期:2014-05-16  浏览次数:20698 次

用java抓网页读取国际时间,执行linux shell命令更改时间

?

//同步时间
	public static void updateTime() throws  Exception {
		HttpClient client=new HttpClient();
		String str="http://123.125.114.102/special/time/";
		GetMethod get=new GetMethod(str);
		client.executeMethod(get);
		byte b[]=get.getResponseBody();
		String html=new String(b,"gbk");
		int ind=html.indexOf("baidu_time(");
		if(ind!=-1){
			int end=html.indexOf(")", ind);
			System.out.println(html.substring(ind+11,end));
			Calendar now=Calendar.getInstance();
			now.setTimeInMillis(new BigDecimal(html.substring(ind+11,end)).longValue());
			System.out.println(getStringFromDate(now.getTime()));
			String[] cmd = {"date","-s",CalendarHelper.getStringFromDate(now.getTime()) };
			ProcessBuilder pb=new ProcessBuilder(cmd);
			Map<String,String> map=pb.environment();
			Iterator it=map.keySet().iterator();
			while(it.hasNext()){
				System.out.println(it.next());
			}
	        pb.start();
	        
	        //以下同步硬件时间
			String cmd2[]={"hwclock","--systohc" };
			pb=new ProcessBuilder(cmd2);
			pb.start();;
			
		}
	}

?

另外还有个命令更简单:

rdate ? ?tick.greyware.com ? ? ? ? ? 或 ??rdate 71.252.193.25


一行命令搞定。

?