您的位置:首页 > 理论基础 > 计算机网络

从网络获取时间 NTP服务器

2014-09-26 18:02 330 查看
获取网络时间,从听到这个功能就觉得这么大众化的功能应该在网上随便一找一大把,但实际去学的时候耗费了我很多时间。

首先 分清楚获取时间的几种方式
1.获取系统时间,系统时间指你所用的设备的系统自带时间,这个时间你可以随便设置
    java有API封装
使用Date类
Date d=new Date();    //解决
SimpleDateFormat sdf=new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss z");    //这是将日期格式成时间字符串
String date=sdf.format(d);
System.out.println(date);


或者使用Calendar类
Calendar c= Calendar.getInstance();
int year= c.get(Calendar.YEAR) ;
int month=c.get(Calendar.MONTH)+1;
int date= c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR);
int minute =c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
int millisecend =c.get(Calendar.MILLISECOND);


2.通过解析国家授时服务器网页来获取时间
public static void main(String[] args) throws Exception{
<span style="white-space:pre">	</span>//取得资源对象
<span style="white-space:pre">	</span>URL url = new URL("http://www.bjtime.cn");
<span style="white-space:pre">	</span>//生成连接对象
<span style="white-space:pre">	</span>URLConnection uc = url.openConnection();
<span style="white-space:pre">	</span>//发出连接
<span style="white-space:pre">	</span>uc.connect();
<span style="white-space:pre">	</span>long time = uc.getDate();
<span style="white-space:pre">	</span>System.out.println("long time:"+time);
<span style="white-space:pre">	</span>Date date = new Date(time);
<span style="white-space:pre">	</span>System.out.println("date:"+date.toString());
<span style="white-space:pre">	</span>System.out.println(new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(date));
<span style="white-space:pre">	</span>}


3.访问NTP服务器获取网络时间
    网上最通用的方式是通过访问NTP服务器(Network Time Protocol)  
    协议说明
        关于NTP服务器:http://baike.baidu.com/view/2069879.htm
    召唤协议结构

协议结构
 
2
5
8
16
24
32bit
LI
VN
Mode
Stratum
Poll
Precision
Root Delay
Root Dispersion
Reference Identifier
Reference timestamp(64)
Originate Timestamp(64)
Receive Timestamp(64)
Transmit Timestamp(64)
Key Identifier(optional)(32)
Message digest(optional)(128)
LI:跳跃指示器,警告在当月最后一天的最终时刻插入的迫近闺秒(闺秒)。 VN:版本号。 Mode:模式。该字段包括以下值:0-预留;1-对称行为;3-客户机;4-服务器;5-广播;6-NTP 控制信息 Stratum:对本地时钟级别的整体识别。 Poll:有符号整数表示连续信息间的最大间隔。 
Precision:有符号整数表示本地时钟精确度。 Root Delay:有符号固定点序号表示主要参考源的总延迟,很短时间内的位15到16间的分段点。 Root Dispersion:无符号固定点序号表示相对于主要参考源的正常差错,很短时间内的位15到16间的分段点。 Reference Identifier:识别特殊参考源。 Originate Timestamp:这是向服务器请求分离客户机的时间,采用64位时标(Timestamp)格式。 Receive Timestamp:这是向服务器请求到达客户机的时间,采用64位时标(Timestamp)格式。 Transmit Timestamp:这是向客户机答复分离服务器的时间,采用64位时标(Timestamp)格式。 Authenticator(Optional):当实现了 NTP 认证模式,主要标识符和信息数字域就包括已定义的信息认证代码(MAC)信息。

这是请求ntp服务器后获取的字符串 对应上面的协议表
Leap indicator: 0
Version: 3
Mode: 4
Stratum: 2
Poll: 0
Precision: -22 (2.4E-7 seconds)
Root delay: 0.58 ms
Root dispersion: 7.08 ms
Reference identifier: 133.100.53.51
Reference timestamp: 26-九月-2014 16:40:18.044444
Originate timestamp: 26-九月-2014 16:41:24.547000
Receive timestamp: 26-九月-2014 16:42:19.829934
Transmit timestamp: 26-九月-2014 16:42:19.829957

    简而言之: 无论谁都可以建立一个 NTP
服务器 只要你有一个获得准确而可靠UTC的时间来源(可以是原子钟、天文台、卫星,也可以从Internet上获取)。然后向外提供服务

下面贴代码

 
访问NTP服务器操作


public static void main(String[] args)
{
int port = 123;
int timeout = 3000;

// get the address and NTP address request
InetAddress ipv4Addr = null;
try {
ipv4Addr = InetAddress.getByName("133.100.11.8");

//以下是几个在国内测试有反应的NTP服务器地址

//203.117.180.36
//133.100.11.8 日本 福冈大学
//time-a.nist.gov 129.6.15.28 129.6.15.29 NIST, Gaithersburg, Maryland
//time-c.timefreq.bldrdoc.gov 132.163.4.103 NIST, Boulder, Colorado
//utcnist.colorado.edu 128.138.140.44 University of Colorado, Boulder
// 208.184.49.9
} catch (UnknownHostException e1) {
e1.printStackTrace();
}

DatagramSocket socket = null;
try {

socket = new DatagramSocket();
socket.setSoTimeout(timeout);

// 请求ntp服务器
byte[] data = new NtpMessage().toByteArray();
DatagramPacket outgoing = new DatagramPacket(data, data.length, ipv4Addr, port);
socket.send(outgoing);

// 接收ntp服务器响应
DatagramPacket incoming = new DatagramPacket(data, data.length);
socket.receive(incoming);

// Validate NTP Response
// IOException thrown if packet does not decode as expected.
NtpMessage msg = new NtpMessage(incoming.getData());

System.out.println("ntp message : "+ msg.toString());
} catch (InterruptedIOException ex) {
e.printStackTrace();
} catch (NoRouteToHostException e) {
System.out.println("No route to host exception for address: " + ipv4Addr);
} catch (ConnectException e) {
// Connection refused. Continue to retry.
e.fillInStackTrace();
System.out.println("Connection exception for address: " + ipv4Addr);
} catch (IOException ex) {
ex.fillInStackTrace();
System.out.println("IOException while polling address: " + ipv4Addr);
} finally {
if (socket != null)
socket.close();
}
}


NtpMessage类 提供构建解析ntp数据包的工具方法

package com.sen5.firstinstall.timezone;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NtpMessage {
/** *//**
* This is a two-bit code warning of an impending leap second to be
* inserted/deleted in the last minute of the current day. It''s values may
* be as follows:
*
* Value Meaning ----- ------- 0 no warning 1 last minute has 61 seconds 2
* last minute has 59 seconds) 3 alarm condition (clock not synchronized)
*/
public byte leapIndicator = 0;

/** *//**
* This value indicates the NTP/SNTP version number. The version number is 3
* for Version 3 (IPv4 only) and 4 for Version 4 (IPv4, IPv6 and OSI). If
* necessary to distinguish between IPv4, IPv6 and OSI, the encapsulating
* context must be inspected.
*/
public byte version = 3;

/** *//**
* This value indicates the mode, with values defined as follows:
*
* Mode Meaning ---- ------- 0 reserved 1 symmetric active 2 symmetric
* passive 3 client 4 server 5 broadcast 6 reserved for NTP control message
* 7 reserved for private use
*
* In unicast and anycast modes, the client sets this field to 3 (client) in
* the request and the server sets it to 4 (server) in the reply. In
* multicast mode, the server sets this field to 5 (broadcast).
*/
public byte mode = 0;

/** *//**
* This value indicates the stratum level of the local clock, with values
* defined as follows:
*
* Stratum Meaning ---------------------------------------------- 0
* unspecified or unavailable 1 primary reference (e.g., radio clock) 2-15
* secondary reference (via NTP or SNTP) 16-255 reserved
*/
public short stratum = 0;

/** *//**
* This value indicates the maximum interval between successive messages, in
* seconds to the nearest power of two. The values that can appear in this
* field presently range from 4 (16 s) to 14 (16284 s); however, most
* applications use only the sub-range 6 (64 s) to 10 (1024 s).
*/
public byte pollInterval = 0;

/** *//**
* This value indicates the precision of the local clock, in seconds to the
* nearest power of two. The values that normally appear in this field
* range from -6 for mains-frequency clocks to -20 for microsecond clocks
* found in some workstations.
*/
public byte precision = 0;

/** *//**
* This value indicates the total roundtrip delay to the primary reference
* source, in seconds. Note that this variable can take on both positive and
* negative values, depending on the relative time and frequency offsets.
* The values that normally appear in this field range from negative values
* of a few milliseconds to positive values of several hundred milliseconds.
*/
public double rootDelay = 0;

/** *//**
* This value indicates the nominal error relative to the primary reference
* source, in seconds. The values that normally appear in this field range
* from 0 to several hundred milliseconds.
*/
public double rootDispersion = 0;

/** *//**
* This is a 4-byte array identifying the particular reference source. In
* the case of NTP Version 3 or Version 4 stratum-0 (unspecified) or
* stratum-1 (primary) servers, this is a four-character ASCII string, left
* justified and zero padded to 32 bits. In NTP Version 3 secondary servers,
* this is the 32-bit IPv4 address of the reference source. In NTP Version 4
* secondary servers, this is the low order 32 bits of the latest transmit
* timestamp of the reference source. NTP primary (stratum 1) servers should
* set this field to a code identifying the external reference source
* according to the following list. If the external reference is one of
* those listed, the associated code should be used. Codes for sources not
* listed can be contrived as appropriate.
*
* Code External Reference Source ---- ------------------------- LOCL
* uncalibrated local clock used as a primary reference for a subnet without
* external means of synchronization PPS atomic clock or other
* pulse-per-second source individually calibrated to national standards
* ACTS NIST dialup modem service USNO USNO modem service PTB PTB (Germany)
* modem service TDF Allouis (France) Radio 164 kHz DCF Mainflingen
* (Germany) Radio 77.5 kHz MSF Rugby (UK) Radio 60 kHz WWV Ft. Collins (US)
* Radio 2.5, 5, 10, 15, 20 MHz WWVB Boulder (US) Radio 60 kHz WWVH Kaui
* Hawaii (US) Radio 2.5, 5, 10, 15 MHz CHU Ottawa (Canada) Radio 3330,
* 7335, 14670 kHz LORC LORAN-C radionavigation system OMEG OMEGA
* radionavigation system GPS Global Positioning Service GOES Geostationary
* Orbit Environment Satellite
*/
public byte[] referenceIdentifier = { 0, 0, 0, 0 };

/** *//**
* This is the time at which the local clock was last set or corrected, in
* seconds since 00:00 1-Jan-1900.
*/
public double referenceTimestamp = 0;

/** *//**
* This is the time at which the request departed the client for the server,
* in seconds since 00:00 1-Jan-1900.
*/
public double originateTimestamp = 0;

/** *//**
* This is the time at which the request arrived at the server, in seconds
* since 00:00 1-Jan-1900.
*/
public double receiveTimestamp = 0;

/** *//**
* This is the time at which the reply departed the server for the client,
* in seconds since 00:00 1-Jan-1900.
*/
public double transmitTimestamp = 0;

/** *//**
* Constructs a new NtpMessage from an array of bytes.
*/
public NtpMessage(byte[] array) {
// See the packet format diagram in RFC 2030 for details
leapIndicator = (byte) ((array[0] >> 6) & 0x3);
version = (byte) ((array[0] >> 3) & 0x7);
mode = (byte) (array[0] & 0x7);
stratum = unsignedByteToShort(array[1]);
pollInterval = array[2];
precision = array[3];

rootDelay = (array[4] * 256.0) + unsignedByteToShort(array[5]) + (unsignedByteToShort(array[6]) / 256.0) + (unsignedByteToShort(array[7]) / 65536.0);

rootDispersion = (unsignedByteToShort(array[8]) * 256.0) + unsignedByteToShort(array[9]) + (unsignedByteToShort(array[10]) / 256.0) + (unsignedByteToShort(array[11]) / 65536.0);

referenceIdentifier[0] = array[12];
referenceIdentifier[1] = array[13];
referenceIdentifier[2] = array[14];
referenceIdentifier[3] = array[15];

referenceTimestamp = decodeTimestamp(array, 16);
originateTimestamp = decodeTimestamp(array, 24);
receiveTimestamp = decodeTimestamp(array, 32);
transmitTimestamp = decodeTimestamp(array, 40);
}

/** *//**
* Constructs a new NtpMessage
*/
public NtpMessage(byte leapIndicator, byte version, byte mode, short stratum, byte pollInterval, byte precision, double rootDelay, double rootDispersion, byte[] referenceIdentifier, double referenceTimestamp, double originateTimestamp, double receiveTimestamp, double transmitTimestamp) {
// ToDo: Validity checking
this.leapIndicator = leapIndicator;
this.version = version;
this.mode = mode;
this.stratum = stratum;
this.pollInterval = pollInterval;
this.precision = precision;
this.rootDelay = rootDelay;
this.rootDispersion = rootDispersion;
this.referenceIdentifier = referenceIdentifier;
this.referenceTimestamp = referenceTimestamp;
this.originateTimestamp = originateTimestamp;
this.receiveTimestamp = receiveTimestamp;
this.transmitTimestamp = transmitTimestamp;
}

/** *//**
* Constructs a new NtpMessage in client -> server mode, and sets the
* transmit timestamp to the current time.
*/
public NtpMessage() {
// Note that all the other member variables are already set with
// appropriate default values.
this.mode = 3;
this.transmitTimestamp = (System.currentTimeMillis() / 1000.0) + 2208988800.0;
}

/** *//**
* This method constructs the data bytes of a raw NTP packet.
*/
public byte[] toByteArray() {
// All bytes are automatically set to 0
byte[] p = new byte[48];

p[0] = (byte) (leapIndicator << 6 | version << 3 | mode);
p[1] = (byte) stratum;
p[2] = (byte) pollInterval;
p[3] = (byte) precision;

// root delay is a signed 16.16-bit FP, in Java an int is 32-bits
int l = (int) (rootDelay * 65536.0);
p[4] = (byte) ((l >> 24) & 0xFF);
p[5] = (byte) ((l >> 16) & 0xFF);
p[6] = (byte) ((l >> 8) & 0xFF);
p[7] = (byte) (l & 0xFF);

// root dispersion is an unsigned 16.16-bit FP, in Java there are no
// unsigned primitive types, so we use a long which is 64-bits
long ul = (long) (rootDispersion * 65536.0);
p[8] = (byte) ((ul >> 24) & 0xFF);
p[9] = (byte) ((ul >> 16) & 0xFF);
p[10] = (byte) ((ul >> 8) & 0xFF);
p[11] = (byte) (ul & 0xFF);

p[12] = referenceIdentifier[0];
p[13] = referenceIdentifier[1];
p[14] = referenceIdentifier[2];
p[15] = referenceIdentifier[3];

encodeTimestamp(p, 16, referenceTimestamp);
encodeTimestamp(p, 24, originateTimestamp);
encodeTimestamp(p, 32, receiveTimestamp);
encodeTimestamp(p, 40, transmitTimestamp);

return p;
}

/** *//**
* Returns a string representation of a NtpMessage
*/
public String toString() {
String precisionStr = new DecimalFormat("0.#E0").format(Math.pow(2, precision));
return "Leap indicator: " + leapIndicator + " " + "Version: " + version + " " + "Mode: " + mode + " " + "Stratum: " + stratum + " " + "Poll: " + pollInterval + " " + "Precision: " + precision + " (" + precisionStr + " seconds) " + "Root delay: " + new DecimalFormat("0.00").format(rootDelay * 1000) + " ms " + "Root dispersion: " + new DecimalFormat("0.00").format(rootDispersion * 1000) + " ms " + "Reference identifier: " + referenceIdentifierToString(referenceIdentifier, stratum, version) + " " + "Reference timestamp: " + timestampToString(referenceTimestamp) + " " + "Originate timestamp: " + timestampToString(originateTimestamp) + " " + "Receive timestamp:   " + timestampToString(receiveTimestamp) + " " + "Transmit timestamp: " + timestampToString(transmitTimestamp);
}

/** *//**
* Converts an unsigned byte to a short. By default, Java assumes that a
* byte is signed.
*/
public static short unsignedByteToShort(byte b) {
if ((b & 0x80) == 0x80)
return (short) (128 + (b & 0x7f));
else
return (short) b;
}

/** *//**
* Will read 8 bytes of a message beginning at <code>pointer</code> and
* return it as a double, according to the NTP 64-bit timestamp format.
*/
public static double decodeTimestamp(byte[] array, int pointer) {
double r = 0.0;

for (int i = 0; i < 8; i++) {
r += unsignedByteToShort(array[pointer + i]) * Math.pow(2, (3 - i) * 8);
}

return r;
}

/** *//**
* Encodes a timestamp in the specified position in the message
*/
public static void encodeTimestamp(byte[] array, int pointer, double timestamp) {
// Converts a double into a 64-bit fixed point
for (int i = 0; i < 8; i++) {
// 2^24, 2^16, 2^8, .. 2^-32
double base = Math.pow(2, (3 - i) * 8);

// Capture byte value
array[pointer + i] = (byte) (timestamp / base);

// Subtract captured value from remaining total
timestamp = timestamp - (double) (unsignedByteToShort(array[pointer + i]) * base);
}

// From RFC 2030: It is advisable to fill the non-significant
// low order bits of the timestamp with a random, unbiased
// bitstring, both to avoid systematic roundoff errors and as
// a means of loop detection and replay detection.
array[7] = (byte) (Math.random() * 255.0);
}

/** *//**
* Returns a timestamp (number of seconds since 00:00 1-Jan-1900) as a
* formatted date/time string.
*/
public static String timestampToString(double timestamp) {
if (timestamp == 0)
return "0";

// timestamp is relative to 1900, utc is used by Java and is relative
// to 1970
double utc = timestamp - (2208988800.0);

// milliseconds
long ms = (long) (utc * 1000.0);

// date/time
String date = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss").format(new Date(ms));

// fraction
double fraction = timestamp - ((long) timestamp);
String fractionSting = new DecimalFormat(".000000").format(fraction);

return date + fractionSting;
}

/** *//**
* Returns a string representation of a reference identifier according to
* the rules set out in RFC 2030.
*/
public static String referenceIdentifierToString(byte[] ref, short stratum, byte version) {
// From the RFC 2030:
// In the case of NTP Version 3 or Version 4 stratum-0 (unspecified)
// or stratum-1 (primary) servers, this is a four-character ASCII
// string, left justified and zero padded to 32 bits.
if (stratum == 0 || stratum == 1) {
return new String(ref);
}

// In NTP Version 3 secondary servers, this is the 32-bit IPv4
// address of the reference source.
else if (version == 3) {
return unsignedByteToShort(ref[0]) + "." + unsignedByteToShort(ref[1]) + "." + unsignedByteToShort(ref[2]) + "." + unsignedByteToShort(ref[3]);
}

// In NTP Version 4 secondary servers, this is the low order 32 bits
// of the latest transmit timestamp of the reference source.
else if (version == 4) {
return "" + ((unsignedByteToShort(ref[0]) / 256.0) + (unsignedByteToShort(ref[1]) / 65536.0) + (unsignedByteToShort(ref[2]) / 16777216.0) + (unsignedByteToShort(ref[3]) / 4294967296.0));
}

return "";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java ntp 网络时间