//package exploringjava.net; import java.net.Socket; 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:00 GMT public DateAtHost( String host, int port ) throws IOException { Socket server = new Socket( host, port ); DataInputStream din = new DataInputStream( server.getInputStream() ); int time = din.readInt(); server.close(); setTime( (((1L << 32) + time) - offset) * 1000 ); } public DateAtHost( String host ) throws IOException { this( host, timePort ); } // Example usage: java exploringjava.net.DateAtHost foo.bar.com public static void main (String [] args) throws Exception { System.out.println( new DateAtHost( args[0] ) ); } }