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

解决方案 »

  1.   


    <%@ 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));
    }
    %>楼主结贴给分吧