/*** * Convert NTP timestamp to Java standard time. * * @return NTP Timestamp in Java time */ public long getTime() { return getTime(ntpTime); }
/*** * Convert NTP timestamp to Java Date object. * * @return NTP Timestamp in Java Date */ public Date getDate() { long time = getTime(ntpTime); return new Date(time); }
/*** * Convert NTP timestamp to Java standard time. * * @return NTP Timestamp in Java time */ public long getTime() { return getTime(ntpTime); }
/*** * Convert NTP timestamp to Java standard time. * * @return NTP Timestamp in Java time */ public long getTime() { return getTime(ntpTime); }
TimeStamp due_date = ...; long dueDateMillis = due_date.getTime(); long t = System.currTimeMillis(); long threshold = 24L * 60L * 60L * 1000L; // One day in milliseconds if (dueDateMillis - t <= threshold) { // Renew book }
/*** * Convert NTP timestamp to Java Date object. * * @return NTP Timestamp in Java Date */ public Date getDate() { long time = getTime(ntpTime); return new Date(time); }
/*** * Convert NTP timestamp to Java Date object. * * @return NTP Timestamp in Java Date */ public Date getDate() { long time = getTime(ntpTime); return new Date(time); }
@Test(successPercentage = 99, dataProvider = "delays") public void secondsPassed(int delay) throws UnknownHostException, InterruptedException, TimeoutException { TimeStamp ts1 = provider.getTimestamp(); Thread.sleep(delay); TimeStamp ts2 = provider.getTimestamp(); long seconds = delay / 1000; // Verify that seconds passed were calculated properly // since the last time NTP timestamp was fetched. Measuring fractions // is pointless as there's a gap between sleeping and requesting the timestamp. assertEquals("Delay=" + delay + " time_diff=" + (ts2.getTime() - ts1.getTime()), seconds, (ts2.getTime() - ts1.getTime()) / 1000); }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 获取网络时间 * todo 可优化为哪个地址延迟小使用哪个 * * @return long */ private long getWebTime(String address) { try { NTPUDPClient client = new NTPUDPClient(); client.open(); client.setDefaultTimeout(1000); client.setSoTimeout(1000); InetAddress inetAddress = InetAddress.getByName(address); // Log.debug("start ask time...."); TimeInfo timeInfo = client.getTime(inetAddress); // Log.debug("done!"); return timeInfo.getMessage().getTransmitTimeStamp().getTime(); } catch (Exception e) { return 0L; } }