Java gets the current network time, detailed Java code is as follows:
Packagetest;Importjava.io.IOException;Importjava.net.MalformedURLException;ImportJava.net.URL;Importjava.net.URLConnection;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classGetwebtime { Public Static voidMain (string[] args) {//get current Network timeString weburl= "http://www.baidu.com";//Baidu TimeString webtime=Getnetworktime (WEBURL); System.out.println ("Current Network time is:" +webtime); } /** Get current Network time*/ Public Staticstring Getnetworktime (String webUrl) {Try{URL URL=NewURL (WEBURL); URLConnection Conn=url.openconnection (); Conn.connect (); LongDatel=conn.getdate (); Date Date=NewDate (Datel); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm"); returnDateformat.format (date); }Catch(malformedurlexception e) {e.printstacktrace (); }Catch(IOException e) {//Todo:handle ExceptionE.printstacktrace (); } return""; }}
Java gets current network time