获取当前网络时间:
package email;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo {
public static void main(String[] args) {
String bdsj = "http://www.baidu.com";// 百度
String time = getNetworkTime(bdsj);
System.out.println(getNetworkTime(bdsj));
}
public static String getNetworkTime(String webUrl) {
try {
URL url = new URL(webUrl);
URLConnection conn = url.openConnection();
conn.connect();
long dateL = conn.getDate();
Date date = new Date(dateL);
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
return dateFormat.format(date);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "获取时间失败!";
}
}