备注:刚开始学的时候写的,比较简陋,呵呵,在此记录一下,如果以后用得着
服务端:
package TimeSync;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.net.*;
public class TimeServer {
private Socket socket;
private ServerSocket ss;
PrintWriter out;
String strDate;
public void GetDate(){
try{
URL url=new URL("http://www.baidu.com/");//取得资源对象//www.time.ac.cn/stime
URLConnection uc=url.openConnection();//生成连接对象
//发出连接
uc.connect();
System.out.println("远端建立连接成功......");
//取得网站日期时间
long ld=uc.getDate();
//转换为String类型
strDate = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(ld));
System.out.println("远端服务时间串:"+strDate);
}catch (Exception e )
{
System.out.println("远端连接失败,获取本地时间.......");
strDate= new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
System.out.println(" 本地时间串:"+strDate);
}
}
public TimeServer() throws IOException {
ss = new ServerSocket(57777);
while (true) {
socket = ss.accept();
out =new PrintWriter(socket.getOutputStream());
GetDate();
out.println(strDate);
out.close();
System.out.println("同步结束.......");
}
}
public static void main(String[] args) {
try {
new TimeServer();
} catch (IOException e) {
e.printStackTrace();
}
}
}
更新时间类:
package TimeSync;
import java.io.IOException;
public class SetTime {
//时间,例如20140506120106
public static String strTime;
//Operating system name
private String osName = System.getProperty("os.name");
private String cmd = "";
public SetTime() throws IOException{
//时间处理
String winDate=strTime.substring(0,4)+ "-"+strTime.substring(4,6)+"-"+strTime.substring(6,8);
String liuDate=strTime.substring(0,8);
String time=strTime.substring(8,10)+ ":"+strTime.substring(10,12)+":"+strTime.substring(12,14);
if (osName.matches("^(?i)Windows.*$")) {// Window 系统
// 格式 HH:mm:ss
cmd = " cmd /c time "+time;
Runtime.getRuntime().exec(cmd);
// 格式:yyyy-MM-dd
cmd = " cmd /c date "+winDate;
Runtime.getRuntime().exec(cmd);
}
else {// Linux 系统
// 格式:yyyyMMdd
cmd = " date -s "+liuDate;
Runtime.getRuntime().exec(cmd);
// 格式 HH:mm:ss
cmd = " date -s "+time;
Runtime.getRuntime().exec(cmd);
}
}
}
客户端:
package TimeSync;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeClient implements Runnable{
Socket client;
PrintWriter out;
public void run(){
while(true)
{
try{
//连接服务器
client=new Socket("133.160.137.235",57777);
System.out.println("成功连接时间服务器...");
//获取时间
String strDate =new BufferedReader( new InputStreamReader(client.getInputStream())).readLine();
//更新时间
System.out.println("本机当前时间:"+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
System.out.println("远端服务时间串:"+strDate);
SetTime.strTime=strDate;
new SetTime();
Thread.sleep(10000); //可设置更新频率
}
catch (UnknownHostException ex1)
{System.out.println("连接服务器失败........");}
catch(IOException ex2)
{System.out.println("获取时间串出错........");}
catch(InterruptedException ex3)
{System.out.println("线程中断........");}
}
}
public static void main(String[] args) {
//启动线程
TimeClient timeClient =new TimeClient();
Thread t=new Thread(timeClient);
t.start();
}
}
转载于:https://blog.51cto.com/yanlfcto/1591611