对于手机,如果有SIM卡支持的话,在设置时间时可以通过选择自动从网络获取时间来取得当地的时间和时区:
但如果手机没有SIM卡,此时如果有Wifi链接,手机依然可以通过网络自动获取时间(时区此时需要手动设置)。 查看Android源码,在android.net 中有 SntpClient类,可以通过访问NTP服务器来取得当前的GMT时间。pool.ntp.org为最常用的一个NTF服务器。修改SntpClient代码,你也可以在自己的应用(包括非Android应用)中通过NTP服务器来取得当前GMT时间,代码如下:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;
public class GetTime {
public static void main(String[] args) {
SntpClient client = new SntpClient();
if (client.requestTime("pool.ntp.org", 30000)) {
long now = client.getNtpTime() + System.nanoTime() / 1000
- client.getNtpTimeReference();
Date current = new Date(now);
System.out.println(current.toString());
}
}
}
class SntpClient {
private static final int ORIGINATE_TIME_OFFSET = 24;
private static final int RECEIVE_TIME_OFFSET = 32;
private static final int TRANSMIT_TIME_OFFSET = 40;
private static final int NTP_PACKET_SIZE = 48;
private static final int NTP_PORT = 123;
private static final int NTP_MODE_CLIENT = 3;
private static final int NTP_VERSION = 3;
// Number of seconds between Jan 1, 1900 and Jan 1, 1970
// 70 years plus 17 leap days
private static final long OFFSET_1900_TO_1970 = ((365L * 70L) + 17L) * 24L * 60L * 60L;
// system time computed from NTP server response
private long mNtpTime;
// value of SystemClock.elapsedRealtime() corresponding to mNtpTime
private long mNtpTimeReference;
// round trip time in milliseconds
private long mRoundTripTime;
/**
* Sends an SNTP request to the given host and processes the response.
*
* @param host
* host name of the server.
* @param timeout
* network timeout in milliseconds.
* @return true if the transaction was successful.
*/
public boolean requestTime(String host, int timeout) {
try {
DatagramSocket socket = new DatagramSocket();
socket.setSoTimeout(timeout);
InetAddress address = InetAddress.getByName(host);
byte[] buffer = new byte[NTP_PACKET_SIZE];
DatagramPacket request = new DatagramPacket(buffer, buffer.length,
address, NTP_PORT);
// set mode = 3 (client) and version = 3
// mode is in low 3 bits of first byte
// version is in bits 3-5 of first byte
buffer[0] = NTP_MODE_CLIENT | (NTP_VERSION << 3);
// get current time and write it to the request packet
long requestTime = System.currentTimeMillis();
long requestTicks = System.nanoTime() / 1000;
writeTimeStamp(buffer, TRANSMIT_TIME_OFFSET, requestTime);
socket.send(request);
// read the response
DatagramPacket response = new DatagramPacket(buffer, buffer.length);
socket.receive(response);
long responseTicks = System.nanoTime() / 1000;
long responseTime = requestTime + (responseTicks - requestTicks);
socket.close();
// extract the results
long originateTime = readTimeStamp(buffer, ORIGINATE_TIME_OFFSET);
long receiveTime = readTimeStamp(buffer, RECEIVE_TIME_OFFSET);
long transmitTime = readTimeStamp(buffer, TRANSMIT_TIME_OFFSET);
long roundTripTime = responseTicks - requestTicks
- (transmitTime - receiveTime);
// receiveTime = originateTime + transit + skew
// responseTime = transmitTime + transit - skew
// clockOffset = ((receiveTime - originateTime) + (transmitTime -
// responseTime))/2
// = ((originateTime + transit + skew - originateTime) +
// (transmitTime - (transmitTime + transit - skew)))/2
// = ((transit + skew) + (transmitTime - transmitTime - transit +
// skew))/2
// = (transit + skew - transit + skew)/2
// = (2 * skew)/2 = skew
long clockOffset = ((receiveTime - originateTime) + (transmitTime - responseTime)) / 2;
// if (Config.LOGD) Log.d(TAG, "round trip: " + roundTripTime +
// " ms");
// if (Config.LOGD) Log.d(TAG, "clock offset: " + clockOffset +
// " ms");
// save our results - use the times on this side of the network
// latency
// (response rather than request time)
mNtpTime = responseTime + clockOffset;
mNtpTimeReference = responseTicks;
mRoundTripTime = roundTripTime;
} catch (Exception e) {
return false;
}
return true;
}
/**
* Returns the time computed from the NTP transaction.
*
* @return time value computed from NTP server response.
*/
public long getNtpTime() {
return mNtpTime;
}
/**
* Returns the reference clock value (value of
* SystemClock.elapsedRealtime()) corresponding to the NTP time.
*
* @return reference clock corresponding to the NTP time.
*/
public long getNtpTimeReference() {
return mNtpTimeReference;
}
/**
* Returns the round trip time of the NTP transaction
*
* @return round trip time in milliseconds.
*/
public long getRoundTripTime() {
return mRoundTripTime;
}
/**
* Reads an unsigned 32 bit big endian number from the given offset in the
* buffer.
*/
private long read32(byte[] buffer, int offset) {
byte b0 = buffer[offset];
byte b1 = buffer[offset + 1];
byte b2 = buffer[offset + 2];
byte b3 = buffer[offset + 3];
// convert signed bytes to unsigned values
int i0 = ((b0 & 0x80) == 0x80 ? (b0 & 0x7F) + 0x80 : b0);
int i1 = ((b1 & 0x80) == 0x80 ? (b1 & 0x7F) + 0x80 : b1);
int i2 = ((b2 & 0x80) == 0x80 ? (b2 & 0x7F) + 0x80 : b2);
int i3 = ((b3 & 0x80) == 0x80 ? (b3 & 0x7F) + 0x80 : b3);
return ((long) i0 << 24) + ((long) i1 << 16) + ((long) i2 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif">
+ (long) i3;
}
/**
* Reads the NTP time stamp at the given offset in the buffer and returns it
* as a system time (milliseconds since January 1, 1970).
*/
private long readTimeStamp(byte[] buffer, int offset) {
long seconds = read32(buffer, offset);
long fraction = read32(buffer, offset + 4);
return ((seconds - OFFSET_1900_TO_1970) * 1000)
+ ((fraction * 1000L) / 0x100000000L);
}
/**
* Writes system time (milliseconds since January 1, 1970) as an NTP time
* stamp at the given offset in the buffer.
*/
private void writeTimeStamp(byte[] buffer, int offset, long time) {
long seconds = time / 1000L;
long milliseconds = time - seconds * 1000L;
seconds += OFFSET_1900_TO_1970;
// write seconds in big endian format
buffer[offset++] = (byte) (seconds >> 24);
buffer[offset++] = (byte) (seconds >> 16);
buffer[offset++] = (byte) (seconds >> 8);
buffer[offset++] = (byte) (seconds >> 0);
long fraction = milliseconds * 0x100000000L / 1000L;
// write fraction in big endian format
buffer[offset++] = (byte) (fraction >> 24);
buffer[offset++] = (byte) (fraction >> 16);
buffer[offset++] = (byte) (fraction >> 8);
// low order bits should be random data
buffer[offset++] = (byte) (Math.random() * 255.0);
}
}
运行这个Java应用,可以得到当前GMT时间,如:Sat Jun 16 10:52:19 BST 2012
分享到:
相关推荐
自动从NTP服务器获取时间并同步本地时间,新测可用,C#,WinForm
通过获取NTP服务器时间,更改本地时间,简单、方便、适用
NTP服务器配置自动脚本(ntp)
完成NTP服务器的配置以及客户端自动同步
威伦通NTP服务器搭建流程(计算机时间与触摸屏时间同步).pdf
通过windows系统为局域网搭建NTP服务器,为局域网内网络设备提供时间服务,经过测试,使用于windows xp、windows 2003、windows 7。 1、启用 NTPServer。为此,请按照下列步骤操作: a. 单击"开始",单击"运行",...
利用ntp服务器格式获取网络时间 下面是ntp数据包的格式 NTP packet = NTP header + Four TimeStamps = 48byte NTP header : 16byte +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- |LI | VN |...
Android 获取时间demo 包含apk 和里面的代码 本文 不是系统ntp获取时间代码,不过是有借鉴系统ntp service代码开发的demo
设置NTP时间服务器的方法,无需任何代码开启Windows Time服务
解决安卓设备内网状态无法与互联网时间同步问题,通过架设NTP服务器端口,安卓端连接内网WIFI后自动与NTP服务器时间同步。
从NTP服务器获取准确的时间,常常被用于校准本地时间。
WinForm实现从NTP服务器同步本地时间,并且可以自动检测当前时区;通过程序中的相关设置可以实现双击运行程序一键式自动同步时间。
多台ntp服务器时间校验...1、多台ntp服务器时间获取 2、可设置ntp同步间隔时间 3、获取的多个ntp时间进行判断有效性 4、与本机的时间误差大于设置值进行同步 5、可设置将本机作为ntp授时服务器 6、可设置程序开机自启动
获取NTP服务器授时中心时间源代码。 可以通过阿里云NTP服务器来获得时间,比较稳定。 ntp1.aliyun.com ntp2.aliyun.com ntp3.aliyun.com ntp4.aliyun.com ntp5.aliyun.com ntp6.aliyun.com ntp7.aliyun.com
S7-1500控制器与在线NTP服务器的时间同步
做了3个小程序,一键搭建NTP服务器小软件
自己在网上找的几个demo,自己整理,拼凑终于弄出来了,能满足基本的需求,多的功能也没有。欢迎一起留言讨论,欢迎私信
随着全球信息化和通络通讯技术的迅速发展,人们对时间的精准性要求越来越高。本文通过对NTP 网络时间同步协议和原理的研究,分析了时间同步的架构与流程,提出了NTP 网络时间同步在Android 平台的实现方案。
自行搭建NTP服务器,Windows xp NTP服务器的配置(2003配置方式一样) 1) 首先需要关闭作为NTP服务器的windows系统自带的防火墙,否则将同步不成功。 2) 单击“开始”,单击“运行”,键入 regedit,然后单击...
windows server 配置 NTP 时间同步服务器.txt