当前位置: 首页>>代码示例>>Java>>正文


Java NTPUDPClient.getTime方法代码示例

本文整理汇总了Java中org.apache.commons.net.ntp.NTPUDPClient.getTime方法的典型用法代码示例。如果您正苦于以下问题:Java NTPUDPClient.getTime方法的具体用法?Java NTPUDPClient.getTime怎么用?Java NTPUDPClient.getTime使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.net.ntp.NTPUDPClient的用法示例。


在下文中一共展示了NTPUDPClient.getTime方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getCurrentTime

import org.apache.commons.net.ntp.NTPUDPClient; //导入方法依赖的package包/类
private long getCurrentTime() {
    long currentTime = 0;
    NTPUDPClient client = new NTPUDPClient();
    client.setDefaultTimeout(WAIT_FOR_SERVER_RESPONSE);
    try {
        client.open();
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MMM dd yyyy HH:mm:ss.SSS zzz");
        for (String server : NTP_SERVERS) {
            try {
                InetAddress ioe = InetAddress.getByName(server);
                TimeInfo info = client.getTime(ioe);
                TimeStamp ntpTime = TimeStamp.getNtpTime(info.getReturnTime());
                return ntpTime.getTime();
            } catch (Exception e2) {
                System.out.println("Can't get response from server: " + server + ".");
            }
        }
    } catch (SocketException se) {
        System.out.println("Can't open client session");
    } finally {
        client.close();
    }
    return currentTime;
}
 
开发者ID:axibase,项目名称:atsd-web-test,代码行数:25,代码来源:AdminServiceTest.java

示例2: getNTPDate

import org.apache.commons.net.ntp.NTPUDPClient; //导入方法依赖的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;
	}
 
开发者ID:AmulaySoftGroup,项目名称:TaBeTa,代码行数:25,代码来源:GameActivity.java

示例3: getNtpServerDate

import org.apache.commons.net.ntp.NTPUDPClient; //导入方法依赖的package包/类
/**
 * Queries the specified NTP Server for the time.
 * Do not query time server more than once every 4 seconds.
 * @param timeServerHost the time server host name.
 * @return the NTP server {@link Date} or {@code null}.
 * @throws IOException
 */
public static Date getNtpServerDate(final String timeServerHost) throws IOException {
    try {
        TimeInfo timeInfo = null;
        final NTPUDPClient timeClient = new NTPUDPClient();
        timeClient.setDefaultTimeout(NTP_SERVER_TIMEOUT_MS);
        final InetAddress inetAddress = InetAddress.getByName(timeServerHost);
        if (inetAddress != null) {
            timeInfo = timeClient.getTime(inetAddress);
            if (timeInfo != null) {
                // TODO: which time to use?
                final long serverTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
                //long serverTime = timeInfo.getReturnTime();
                final Date ntpDate = new Date(serverTime);
                return ntpDate;
            }
        }
    } catch (final IOException e) {
        throw new IOException("Unable to get NTP server time.", e);
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-rya,代码行数:29,代码来源:TimeUtils.java

示例4: getTime

import org.apache.commons.net.ntp.NTPUDPClient; //导入方法依赖的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();
}
 
开发者ID:andrey-desman,项目名称:openhab-hdl,代码行数:28,代码来源:NtpBinding.java

示例5: getCurrentNetworkTime

import org.apache.commons.net.ntp.NTPUDPClient; //导入方法依赖的package包/类
public long getCurrentNetworkTime() throws IOException{
    NTPUDPClient timeClient = new NTPUDPClient();
    
    TimeInfo timeInfo = null;
    
   	InetAddress inetAddress = InetAddress.getByName(Constants.TIME_SERVER);
   	timeInfo = timeClient.getTime(inetAddress);
    
    //long returnTime = timeInfo.getReturnTime();   //local device time
    long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();   //server time
    timeInfo.getMessage().getTransmitTimeStamp().getTime();
    Date time = new Date(returnTime);
    
    //TODO make this into a debug mode/verbose?
    if(true){
   		CharSequence text = "Time from " + Constants.TIME_SERVER + ": " + time + 
   				timeInfo.getMessage().getTransmitTimeStamp().toDateString();
   		Log.d("ntp", text.toString());
   	}
    
    return returnTime;
}
 
开发者ID:stevenqzhang,项目名称:synchronome,代码行数:23,代码来源:Metronome.java

示例6: get

import org.apache.commons.net.ntp.NTPUDPClient; //导入方法依赖的package包/类
public static TimeInfo get() throws IOException {
	final NTPUDPClient client = new NTPUDPClient();
	client.setDefaultTimeout(10000);
	client.open();
	final InetAddress hostAddr = InetAddress.getByName(EEWBot.instance.getConfig().getNptServer());
	return client.getTime(hostAddr);
}
 
开发者ID:Team-Fruit,项目名称:EEWBot,代码行数:8,代码来源:NTPDispatcher.java

示例7: updateOffSet

import org.apache.commons.net.ntp.NTPUDPClient; //导入方法依赖的package包/类
private static void updateOffSet()
{
	//CREATE CLIENT
	NTPUDPClient client = new NTPUDPClient();
   
	//SET TIMEOUT
	client.setDefaultTimeout(10000);
	try 
	{
		//OPEN CLIENT
		client.open();
         
		//GET INFO FROM NTP SERVER
		InetAddress hostAddr = InetAddress.getByName(NTP_SERVER);
		TimeInfo info = client.getTime(hostAddr);
		info.computeDetails();
          
		//UPDATE OFFSET
		if(info.getOffset() != null)
		{
			offset = info.getOffset();
		} 
	} 
	catch (Exception e) 
	{
   	   	//ERROR GETTING OFFSET
	}

	client.close(); 
  }
 
开发者ID:razakal,项目名称:Qora,代码行数:31,代码来源:NTP.java

示例8: main

import org.apache.commons.net.ntp.NTPUDPClient; //导入方法依赖的package包/类
public static void main(String[] args) {
	if (args.length == 0) {
		System.err.println("Usage: NTPClient <hostname-or-address-list>");
		System.exit(1);
	}

	NTPUDPClient client = new NTPUDPClient();
	// We want to timeout if a response takes longer than 10 seconds
	client.setDefaultTimeout(10000);
	try {
		client.open();
		for (String arg : args) {
			System.out.println();
			try {
				InetAddress hostAddr = InetAddress.getByName(arg);
				System.out.println("> " + hostAddr.getHostName() + "/"
						+ hostAddr.getHostAddress());
				TimeInfo info = client.getTime(hostAddr);
				processResponse(info);
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
	} catch (SocketException e) {
		e.printStackTrace();
	}

	client.close();
}
 
开发者ID:rafalmag,项目名称:EV3-projects,代码行数:30,代码来源:NTPClient.java

示例9: queryServerNow

import org.apache.commons.net.ntp.NTPUDPClient; //导入方法依赖的package包/类
private void queryServerNow() {
    Long offsetResult = null;
    for (int i = 0; i < MAX_QUERY_RETRIES; i++) {
        try {
            NTPUDPClient client = new NTPUDPClient();
            client.setDefaultTimeout(DEFAULT_NTP_TIMEOUT_MS);// Timeout if a response takes longer than 10 seconds

            client.open();
            InetAddress address = InetAddress.getByName(ntpServer);
            TimeInfo info = client.getTime(address);
            info.computeDetails();
            Long offset = info.getOffset();
            if (offset == null) {
                throw new Exception("Could not calculate time offset (offset is null)");
            } else {
                offsetResult = offset;
                break;
            }
        } catch (Exception e) {
            log.error("Error querying NTP server, attempt {} of {}", (i + 1), MAX_QUERY_RETRIES, e);
        }
    }

    if (offsetResult == null) {
        log.error("Could not successfully query NTP server after " + MAX_QUERY_RETRIES + " tries");
        throw new RuntimeException("Could not successfully query NTP server after " + MAX_QUERY_RETRIES + " tries");
    }

    lastOffsetGetTimeSystemMS = System.currentTimeMillis();
    lastOffsetMilliseconds = offsetResult;
    log.debug("Updated local time offset based on NTP server result. Offset = {}", lastOffsetMilliseconds);
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:33,代码来源:NTPTimeSource.java

示例10: getTime

import org.apache.commons.net.ntp.NTPUDPClient; //导入方法依赖的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();
}
 
开发者ID:openhab,项目名称:openhab1-addons,代码行数:28,代码来源:NtpBinding.java

示例11: getNTPDate

import org.apache.commons.net.ntp.NTPUDPClient; //导入方法依赖的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());
}
 
开发者ID:damianofalcioni,项目名称:Websocket-Smart-Card-Signer,代码行数:9,代码来源:SignUtils.java


注:本文中的org.apache.commons.net.ntp.NTPUDPClient.getTime方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。