java Source

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            14 Posts :: 24 Stories :: 8 Comments :: 0 Trackbacks
          import java.io.InputStream;
          import java.net.Socket;

          /**
           * 
           * 
          @author ray (justinlei@gmail.com)
           * @date 2009-8-27
           * 
          @version 1.0
           
          */
          public class TimeUtil {
              
          public static final int DEFAULT_PORT = 37;
              
          public static final String DEFAULT_HOST = "time-nw.nist.gov";
              
          private TimeUtil() {
              };

              
          public static long currentTimeMillis(Boolean sync) {
                  
          if (sync != null && sync.booleanValue() != true)
                      
          return System.currentTimeMillis();
                  
          try {
                      
          return syncCurrentTime();
                  } 
          catch (Exception e) {
                      
          return System.currentTimeMillis();
                  }
              }

              
          public static long syncCurrentTime()  throws Exception {
                  
          // The time protocol sets the epoch at 1900,
                  
          // the java Date class at 1970. This number
                  
          // converts between them.
                  long differenceBetweenEpochs = 2208988800L;

                  
          // If you'd rather not use the magic number uncomment
                  
          // the following section which calculates it directly.

                  
          /*
                   * TimeZone gmt = TimeZone.getTimeZone("GMT"); Calendar epoch1900 =
                   * Calendar.getInstance(gmt); epoch1900.set(1900, 01, 01, 00, 00, 00);
                   * long epoch1900ms = epoch1900.getTime().getTime(); Calendar epoch1970
                   * = Calendar.getInstance(gmt); epoch1970.set(1970, 01, 01, 00, 00, 00);
                   * long epoch1970ms = epoch1970.getTime().getTime();
                   * 
                   * long differenceInMS = epoch1970ms - epoch1900ms; long
                   * differenceBetweenEpochs = differenceInMS/1000;
                   
          */

                  InputStream raw 
          = null;
                  
          try {
                      Socket theSocket 
          = new Socket(DEFAULT_HOST, DEFAULT_PORT);
                      raw 
          = theSocket.getInputStream();

                      
          long secondsSince1900 = 0;
                      
          for (int i = 0; i < 4; i++) {
                          secondsSince1900 
          = (secondsSince1900 << 8| raw.read();
                      }
                      
          if (raw != null)
                          raw.close();
                      
          long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;
                      
          long msSince1970 = secondsSince1970 * 1000;
                      
          return msSince1970;
                  } 
          catch (Exception e) {
                      
          throw new Exception(e);
                  }
              }
          }
          posted on 2011-12-04 02:54 JustinLei 閱讀(1641) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 诏安县| 浦江县| 西藏| 余庆县| 四会市| 永德县| 高碑店市| 邯郸县| 呈贡县| 竹山县| 故城县| 石阡县| 大连市| 太白县| 封开县| 明水县| 泾阳县| 景宁| 宜宾县| 康保县| 华安县| 福清市| 鹿邑县| 临邑县| 揭阳市| 大同县| 临西县| 黄骅市| 白河县| 南涧| 武平县| 宁波市| 庆安县| 三穗县| 威信县| 寻甸| 双桥区| 阿合奇县| 马边| 新干县| 贵港市|