本文整理汇总了Java中org.apache.commons.net.ntp.TimeInfo.getReturnTime方法的典型用法代码示例。如果您正苦于以下问题:Java TimeInfo.getReturnTime方法的具体用法?Java TimeInfo.getReturnTime怎么用?Java TimeInfo.getReturnTime使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.net.ntp.TimeInfo
的用法示例。
在下文中一共展示了TimeInfo.getReturnTime方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNTPDate
import org.apache.commons.net.ntp.TimeInfo; //导入方法依赖的package包/类
public static String getNTPDate() {
String[] hosts = new String[] { "ntp02.oal.ul.pt", "ntp04.oal.ul.pt",
"ntp.xs4all.nl", "time.foo.com", "time.nist.gov" };
NTPUDPClient client = new NTPUDPClient();
// We want to timeout if a response takes longer than 5 seconds
client.setDefaultTimeout(2000);
SimpleDateFormat OutPutFormat = new SimpleDateFormat(
"dd/M/yyyy HH:mm:ss", java.util.Locale.getDefault());
for (String host : hosts) {
try {
InetAddress hostAddr = InetAddress.getByName(host);
TimeInfo info = client.getTime(hostAddr);
Date date = new Date(info.getReturnTime());
String out = OutPutFormat.format(date);
return out;
} catch (IOException e) {
e.printStackTrace();
}
}
client.close();
return null;
}
示例2: getTime
import org.apache.commons.net.ntp.TimeInfo; //导入方法依赖的package包/类
/**
* Queries the given timeserver <code>hostname</code> and returns the time
* in milliseconds.
*
* @param hostname the timeserver to query
* @return the time in milliseconds or the current time of the system if an
* error occurs.
*/
protected static long getTime(String hostname) {
try {
NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_TIMEOUT);
InetAddress inetAddress = InetAddress.getByName(hostname);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
return timeInfo.getReturnTime();
}
catch (UnknownHostException uhe) {
logger.warn("the given hostname '{}' of the timeserver is unknown -> returning current sytem time instead", hostname);
}
catch (IOException ioe) {
logger.warn("couldn't establish network connection [host '{}'] -> returning current sytem time instead", hostname);
}
return System.currentTimeMillis();
}
示例3: getTime
import org.apache.commons.net.ntp.TimeInfo; //导入方法依赖的package包/类
/**
* Queries the given timeserver <code>hostname</code> and returns the time
* in milliseconds.
*
* @param hostname the timeserver to query
* @return the time in milliseconds or the current time of the system if an
* error occurs.
*/
protected static long getTime(String hostname) {
try {
NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_TIMEOUT);
InetAddress inetAddress = InetAddress.getByName(hostname);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
return timeInfo.getReturnTime();
} catch (UnknownHostException uhe) {
logger.warn("the given hostname '{}' of the timeserver is unknown -> returning current sytem time instead",
hostname);
} catch (IOException ioe) {
logger.warn("couldn't establish network connection [host '{}'] -> returning current sytem time instead",
hostname);
}
return System.currentTimeMillis();
}
示例4: getNTPDate
import org.apache.commons.net.ntp.TimeInfo; //导入方法依赖的package包/类
public static Date getNTPDate() throws Exception{
//FIXME: how to use system defined proxy here ?
System.setProperty("java.net.useSystemProxies", "true");
NTPUDPClient client = new NTPUDPClient();
client.setDefaultTimeout(5000);
TimeInfo response = client.getTime(InetAddress.getByName("pool.ntp.org"));
return new Date(response.getReturnTime());
}