获得网络时间

转自:http://www.rgagnon.com/javadetails/java-0589.html

 

We connect to a publicly accessible time server on the internet and parse the result.

List of available time servers : http://tf.nist.gov/service/time-servers.html

NOTE : All users should ensure that their software NEVER queries a server more frequently than once every 4 seconds. Systems that exceed this rate will be refused service. In extreme cases, systems that exceed this limit may be considered as attempting a denial-of-service attack.

import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;

public final class DateUtils {
  // NIST, Boulder, Colorado  (time-a.timefreq.bldrdoc.gov)
  public static final String ATOMICTIME_SERVER="http://132.163.4.101:13";
  // NIST, Gaithersburg, Maryland (time-a.nist.gov)
  // public static final String ATOMICTIME_SERVER="http://129.6.15.28:13";
  public final static GregorianCalendar getAtomicTime() throws IOException{
    BufferedReader in = null;

    try {
       URLConnection conn = new URL(ATOMICTIME_SERVER).openConnection();
       in = new BufferedReader
         (new InputStreamReader(conn.getInputStream()));

       String atomicTime;
       while (true) {
          if ( (atomicTime = in.readLine()).indexOf("*") > -1) {
             break;
          }
       }
       System.out.println("DEBUG : " + atomicTime);
       String[] fields = atomicTime.split(" ");
       GregorianCalendar calendar = new GregorianCalendar();

       String[] date = fields[1].split("-");
       calendar.set(Calendar.YEAR, 2000 +  Integer.parseInt(date[0]));
       calendar.set(Calendar.MONTH, Integer.parseInt(date[1])-1);
       calendar.set(Calendar.DATE, Integer.parseInt(date[2]));

       // deals with the timezone and the daylight-saving-time
       TimeZone tz = TimeZone.getDefault();
       int gmt = (tz.getRawOffset() + tz.getDSTSavings()) / 3600000;
       System.out.println("DEBUG : " + gmt);

       String[] time = fields[2].split(":");
       calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]) + gmt);
       calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
       calendar.set(Calendar.SECOND, Integer.parseInt(time[2]));
       return calendar;
    }
    catch (IOException e){
       throw e;
    }
    finally {
       if (in != null) {
         in.close();
       }
    }
  }

  public static void main(String args[]) throws IOException {
    SimpleDateFormat sdf = 
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("Atomic time : " + 
        sdf.format(DateUtils.getAtomicTime().getTime()));
  }

  /*
    ref : http://www.bldrdoc.gov/doc-tour/atomic_clock.html

                       49825 95-04-18 22:24:11 50 0 0 50.0 UTC(NIST) *

                       |     |        |     | | |  |      |      |
    These are the last +     |        |     | | |  |      |      |
    five digits of the       |        |     | | |  |      |      |
    Modified Julian Date     |        |     | | |  |      |      |
                             |        |     | | |  |      |      |
    Year, Month and Day <----+        |     | | |  |      |      |
                                      |     | | |  |      |      |
    Hour, minute, and second of the <-+     | | |  |      |      |
    current UTC at Greenwich.               | | |  |      |      |
                                            | | |  |      |      |
    DST - Daylight Savings Time code <------+ | |  |      |      |
    00 means standard time(ST), 50 means DST  | |  |      |      |
    99 to 51 = Now on ST, goto DST when local | |  |      |      |
    time is 2:00am, and the count is 51.      | |  |      |      |
    49 to 01 = Now on DST, goto ST when local | |  |      |      |
    time is 2:00am, and the count is 01.      | |  |      |      |
                                              | |  |      |      |
    Leap second flag is set to "1" when <-----+ |  |      |      |
    a leap second will be added on the last     |  |      |      |
    day of the current UTC month.  A value of   |  |      |      |
    "2" indicates the removal of a leap second. |  |      |      |
                                                |  |      |      |
    Health Flag.  The normal value of this    <-+  |      |      |
    flag is 0.  Positive values mean there may     |      |      |
    be an error with the transmitted time.         |      |      |
                                                   |      |      |
    The number of milliseconds ACTS is advancing <-+      |      |
    the time stamp, to account for network lag.           |      |
                                                          |      |
    Coordinated Universal Time from the National <--------+      |
    Institute of Standards & Technology.                         |
                                                                 |
    The instant the "*" appears, is the exact time. <------------+
  */
}

// thanks to TrueJavaProgrammer for the idea!

It's not possible to set your local computer clock in pure Java.

You need to use an external utility provided by the OS or call a JNI routine, see this HowTo.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值