2007-07-20
java如何获得服务器时间使用j
给的信息太少,推断你是CS结构的系统,Client取Server端时间,简单说就是一个信息传递的问题,不知道你现在的CS间传递信息方式,一般有RMI、Soap等等,以RMI为例=======接口部分===============import java。 rmi。Remote;import java。rmi。RemoteException;public interface IDoRemote extends Remote {public int getServerTime() throws RemoteException;}=======接口实现===============...全部
给的信息太少,推断你是CS结构的系统,Client取Server端时间,简单说就是一个信息传递的问题,不知道你现在的CS间传递信息方式,一般有RMI、Soap等等,以RMI为例=======接口部分===============import java。
rmi。Remote;import java。rmi。RemoteException;public interface IDoRemote extends Remote { public int getServerTime() throws RemoteException;}=======接口实现===============public class DoRemoteImpl extends UnicastRemoteObject implements IDoRemote { /** * @throws RemoteException */ protected DoRemoteImpl() throws RemoteException { super(); } private static final long serialVersionUID = -8158779541912069375L; /** * @see iy。
test。RemoteServer。IDoRemote#getServerTime() */ public int getServerTime() throws RemoteException { return rseInt(new SimpleDateFormat("yyyyMMdd")。
format(new Date())); // return new Date(); }========Server端绑定=============== public static void main(String[] args) { try { eateRegistry(8808); DoRemoteImpl Server = new DoRemoteImpl(); bind("//localhost:8808/Date-Server", Server); } catch ( 。
MalformedURLException me) { intln("Malformed URL: " + String()); } catch (RemoteException re) { intln("Remote exception: " + String()); } }===========客户端的调用方式============ String url = "//localhost:8808/Date-Server"; IDoRemote RmiObject = (IDoRemote) Naming。
lookup(url); intln(" Server Date: " + tServerTime());PS。 如果使用jdk5。0以下版本需要用rmic编译一下可以搜搜网上相关文档,有很多例子。
收起