|
import java.net.*;
import java.io.*;
public class
DateAtHost extends java.util.Date {
static int timePort = 37;
static final long offset= 2208988800L;//Seconds
from century
//to
Jan 1,1970 00:00GMT
public
DateAtHost( String host ) throws IOException {
this( host, timePort );
}
public
DateAtHost( String host, int port ) throws IOException {
//
Open a socket to this host on port 37
Socket sock = new Socket(
host, port );
//
Open an input stream to this socket.
DataInputStream din =
new DataInputStream(sock.getInputStream()
);
//
Now read the integer time value from the socket stream.
int time = din.readInt();
sock.close();
setTime( (((1L << 32) + time) - offset)
* 1000 ) ;
}
//
Example usage: java DateAtHost helio.ora.com
public static void main (String [] args) {
try {
System.out.println( new DateAtHost(
args[0] ) );
} catch (UnknownHostException e){
System.out.println("Host " + args[0]
+" is unknown.");
} catch (IOException e){
System.out.println("IO Exception");
}
}
}
|