第3楼 2007-07-20 12:45 索然无味 写道:
给的信息太少,推断你是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 cn.shiy.test.RemoteServer.IDoRemote#getServerTime()
*/
public int getServerTime() throws RemoteException {
return Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
// return new Date();
}
========Server端绑定===============
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(8808);
DoRemoteImpl Server = new DoRemoteImpl();
Naming.rebind("//localhost:8808/Date-Server", Server);
} catch (java.net.MalformedURLException me) {
System.out.println("Malformed URL: " + me.toString());
} catch (RemoteException re) {
System.out.println("Remote exception: " + re.toString());
}
}
===========客户端的调用方式============
String url = "//localhost:8808/Date-Server";
IDoRemote RmiObject = (IDoRemote) Naming.lookup(url);
System.out.println(" Server Date: " + RmiObject.getServerTime());
PS. 如果使用jdk5.0以下版本需要用rmic编译一下
可以搜搜网上相关文档,有很多例子