/Users/lyon/j4p/src/net/date/SimpleClock.java

1    package net.date; 
2     
3    import java.io.BufferedReader; 
4    import java.io.IOException; 
5    import java.io.InputStreamReader; 
6    import java.net.Socket; 
7    import java.net.UnknownHostException; 
8     
9    public class SimpleClock { 
10       private static BufferedReader getTimeBufferedReader() 
11               throws UnknownHostException, 
12               IOException { 
13           Socket s = new Socket( 
14                   "time-A.timefreq.bldrdoc.gov", 13); 
15           return new BufferedReader( 
16                   new InputStreamReader( 
17                           s.getInputStream())); 
18       } 
19    
20       /** 
21        *  return a string representing the time from NIST 
22        *  output looks like: 
23        * 52520 02-09-03 11:06:08 50 0 0 537.5 UTC(NIST) * 
24        */ 
25       public static String getTime() { 
26           String time = ""; 
27           try { 
28               BufferedReader br = getTimeBufferedReader(); 
29               String s = null; 
30               while ((s = br.readLine()) != null) 
31                   time = time + s; 
32           } catch (IOException e) { 
33               e.printStackTrace(); 
34           } 
35           return time; 
36       } 
37    
38       public static void main(String[] args) { 
39           System.out.println("Running simple clock:"); 
40           System.out.println(SimpleClock.getTime()); 
41           System.out.println("clock done"); 
42       } 
43   } 
44