站内搜索: 请输入搜索关键词

当前页面: JAVA 编程资料牛鼻论坛Java & J2SE 技术区→java如何获得服务器时间

java如何获得服务器时间

发表新主题   回复此主题

第1楼 2007-07-18 20:04 激情树世界 写道:

java如何获得服务器时间

使用java语言如何获得服务器时间。
找了一些代码获得的都是客户端的时间。
最好是给一些代码,越详细越好,谢谢

第2楼 2013-08-31 12:44 Robot :

java如何获得服务器时间 相关


第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编译一下
可以搜搜网上相关文档,有很多例子




发表新主题   回复此主题