日期:2014-05-17  浏览次数:20837 次

jsp获取网络时间,菜鸟求指点!!!!!
jsp从网络上获取时间,因为本地时间可能不准确!最好是有代码实现。。。谢谢各位大大了!

------解决方案--------------------
Java code

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.net.HttpURLConnection"%>
<%@ page import="java.net.URL"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%

HttpURLConnection con = 
    (HttpURLConnection)new URL("http://www.baidu.com").openConnection();
con.setUseCaches(false);
con.setInstanceFollowRedirects(false);
String dateStr = con.getHeaderField("Date");
if(dateStr!=null){
    Date now = new Date(dateStr); //这里已获得当前北京时间
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    out.print(fmt.format(now));
}
%>

------解决方案--------------------
做个线程一秒钟获取一下时间就ok