Example usage for org.apache.commons.net.time TimeTCPClient getDate

List of usage examples for org.apache.commons.net.time TimeTCPClient getDate

Introduction

In this page you can find the example usage for org.apache.commons.net.time TimeTCPClient getDate.

Prototype

public Date getDate() throws IOException 

Source Link

Document

Retrieves the time from the server and returns a Java Date containing the time converted to the local timezone.

Usage

From source file:examples.unix.rdate.java

public static final void timeTCP(String host) throws IOException {
    TimeTCPClient client = new TimeTCPClient();

    // We want to timeout if a response takes longer than 60 seconds
    client.setDefaultTimeout(60000);//from w  w w  .  j  a  v a 2 s .  com
    client.connect(host);
    System.out.println(client.getDate().toString());
    client.disconnect();
}

From source file:beans.TimeClient.java

public static final void timeTCP(String host) throws IOException {
    TimeTCPClient client = new TimeTCPClient();
    try {//from w w  w . ja va  2s.  co m
        // We want to timeout if a response takes longer than 60 seconds
        client.setDefaultTimeout(60000);
        client.connect(host);
        System.out.println(client.getDate());
    } finally {
        client.disconnect();
    }
}

From source file:it.jnrpe.plugin.CheckTime.java

private Date getTimeTCP(String host, int timeout, int port) throws IOException {
    TimeTCPClient client = new TimeTCPClient();
    client.setDefaultPort(port);//  w ww  . jav a 2s.  com
    client.setDefaultTimeout((int) TimeUnit.SECOND.convert(timeout));
    client.connect(host);
    Date date = client.getDate();
    client.disconnect();
    return date;

}

From source file:org.techytax.util.DateHelper.java

public static Date getNTPDate() {
    List<String> hosts = Collections.singletonList("nl.pool.ntp.org");

    for (String host : hosts) {
        TimeTCPClient client = new TimeTCPClient();
        client.setDefaultTimeout(5000);//from ww  w  .  j  a v a  2s.  com
        try {
            client.connect(host);
            Date ntpDate = client.getDate();
            client.disconnect();
            return ntpDate;
        } catch (Exception exp) {
            System.out.println("NTP connection error");
        }
    }
    return null;
}