2015-05-19 17 views
0

我想知道是否有任何API /类可以给我们原始日期,因为我不想从android设备获取日期/时间。获取当前在android中的互联网日期

+0

没有,有没有像在Android系统。 –

+0

这可能有助于https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/net/SntpClient.java –

+0

您可以使用任何第三方服务,或者您可以编写web服务得到帽子 – Sree

回答

0

使用此

public String getTime() { 
try{ 
    //Make the Http connection so we can retrieve the time 
    HttpClient httpclient = new DefaultHttpClient(); 
    // I am using yahoos api to get the time 
    HttpResponse response = httpclient.execute(new 
    HttpGet("http://developer.yahooapis.com/TimeService/V1/getTime?appid=YahooDemo")); 
    StatusLine statusLine = response.getStatusLine(); 
    if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     response.getEntity().writeTo(out); 
     out.close(); 
     // The response is an xml file and i have stored it in a string 
     String responseString = out.toString(); 
     Log.d("Response", responseString); 
     //We have to parse the xml file using any parser, but since i have to 
     //take just one value i have deviced a shortcut to retrieve it 
     int x = responseString.indexOf("<Timestamp>"); 
     int y = responseString.indexOf("</Timestamp>"); 
     //I am using the x + "<Timestamp>" because x alone gives only the start value 
     Log.d("Response", responseString.substring(x + "<Timestamp>".length(),y)); 
     String timestamp = responseString.substring(x + "<Timestamp>".length(),y); 
     // The time returned is in UNIX format so i need to multiply it by 1000 to use it 
     Date d = new Date(Long.parseLong(timestamp) * 1000); 
     Log.d("Response", d.toString()); 
     return d.toString() ; 
    } else{ 
     //Closes the connection. 
     response.getEntity().getContent().close(); 
     throw new IOException(statusLine.getReasonPhrase()); 
    } 
}catch (ClientProtocolException e) { 
Log.d("Response", e.getMessage()); 
}catch (IOException e) { 
Log.d("Response", e.getMessage()); 
} 
return null; 
} 
+0

和你可以看看这个http://stackoverflow.com/questions/9466342/get-data-from-the-internet-android –

2

您可以从http://www.timeapi.org/utc/now获得UTC时间并将其转换为本地时间格式。

+0

因为我是新来的android ..你能提供一个例子使用您的方法获取当前日期?你能帮助我吗?... – Simon

0

您可以使用下面的程序

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.You将需要Apache共享网库这个工作得到网络时间服务器的时间。下载库并添加到您的项目构建路径。

(或者你也可以在这里使用的修剪Apache的百科全书网络库:https://www.dropbox.com/s/bjxjv7phkb8xfhh/commons-net-3.1.jar这足以从互联网上获取时间)

0

可以使用android.net.sntp.SntpClient类。

SntpClient client = new SntpClient(); 
int timeout = 50000; 
if (client.requestTime("time-a.nist.gov", timeout)) { 
    long time = client.getNtpTime(); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(time); 
    calendar.getTime(); // this should be your date 
} 
1

更新!

旧的答案不起作用,因为timeapi.org已关闭。

这是我的新getTime方法。它使用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' 
+0

它现在不工作 –

+0

克里希纳米娜方法是工作,但网站已关闭。我更新我的答案。 – Mete

相关问题