//同步时间
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 -s tick.greyware.com 或 rdate -s 71.252.193.25
要显示远程服务器时间,用
rdate -p tick.greyware.com
要同步,执行
rdate -s tick.greyware.com
一行命令搞定。
然后把时间同步到硬件
hwclock -w