从Android中的互联网获取当前日期 [英] get current Date from internet in android

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

问题描述

我想知道android中是否有可以给我们原始日期的API/类,因为我不想从android设备获取日期/时间.

I want to know if there is any API/class in android which can give us the original date as I do not want to get the date/time from android device.

推荐答案

已更新!

由于timeapi.org关闭,旧答案无效.

The old answer does not work because the timeapi.org is down.

这是我新的getTime方法.它使用的是 JSOUP .

Here is the my new getTime method. It's using JSOUP.

private long getTime() throws Exception {
    String url = "https://time.is/Unix_time_now";
    Document doc = Jsoup.parse(new URL(url).openStream(), "UTF-8", url);
    String[] tags = new String[] {
            "div[id=time_section]",
            "div[id=clock0_bg]"
    };
    Elements elements= doc.select(tags[0]);
    for (int i = 0; i <tags.length; i++) {
        elements = elements.select(tags[i]);
    }
    return Long.parseLong(elements.text() + "000");
}

等级:

compile 'org.jsoup:jsoup:1.10.2'

这篇关于从Android中的互联网获取当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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