如何获取当前时间从互联网的机器人 [英] How to get current time from internet in android

查看:212
本文介绍了如何获取当前时间从互联网的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序中,我想获得当前时间从internet.I知道如何从设备甚至在搜索了大量的时间,我没有得到任何有关如何从互联网得到它线索。这是利用从设备获取时间codeI上午:

I am making an app in which i want to get the current time from internet.I know how to get the time from the device and even after searching a lot,i did not get any clue about how to get it from internet.This is the code i am using for getting the time from the device:

public static long TimeChecker()
    {
        long millis = System.currentTimeMillis();
        return millis;
    }

请帮忙me.Thanks很多提前。

Please help me.Thanks a lot in advance.

推荐答案

您可以使用下面的程序获得网络时间服务器时间

You can get time from internet time servers using the below program

import java.io.IOException;

import org.apache.commons.net.time.TimeTCPClient;

public final class GetTime {

    public static final void main(String[] args) {
        try {
            TimeTCPClient client = new TimeTCPClient();
            try {
                // Set timeout of 60 seconds
                client.setDefaultTimeout(60000);
                // Connecting to time server
                // Other time servers can be found at : http://tf.nist.gov/tf-cgi/servers.cgi#
                // Make sure that your program NEVER queries a server more frequently than once every 4 seconds
                client.connect("nist.time.nosc.us");
                System.out.println(client.getDate());
            } finally {
                client.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1。你需要阿帕奇共享网库这个工作。 <一href="http://apache.techartifact.com/mirror//commons/net/binaries/commons-net-3.1-bin.tar.gz">Download该库并添加到项目的构建路径。

1.You would need Apache Commons Net library for this to work. Download the library and add to your project build path.

(或者你也可以用修剪阿帕奇共享网络库的位置:<一href="https://www.dropbox.com/s/bjxjv7phkb8xfhh/commons-net-3.1.jar">https://www.dropbox.com/s/bjxjv7phkb8xfhh/commons-net-3.1.jar.这是足以让从互联网上的时间)

(Or you can also use the trimmed Apache Commons Net Library here : https://www.dropbox.com/s/bjxjv7phkb8xfhh/commons-net-3.1.jar. This is enough to get time from internet )

2.运行该程序。您将获得印在控制台上的时间。

2.Run the program. You will get the time printed on your console.

这篇关于如何获取当前时间从互联网的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆