我想知道是否有任何API /类可以给我们原始日期,因为我不想从android设备获取日期/时间。获取当前在android中的互联网日期
回答
使用此
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;
}
和你可以看看这个http://stackoverflow.com/questions/9466342/get-data-from-the-internet-android –
您可以从http://www.timeapi.org/utc/now获得UTC时间并将其转换为本地时间格式。
因为我是新来的android ..你能提供一个例子使用您的方法获取当前日期?你能帮助我吗?... – Simon
您可以使用下面的程序
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这足以从互联网上获取时间)
可以使用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
}
更新!
旧的答案不起作用,因为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'
它现在不工作 –
克里希纳米娜方法是工作,但网站已关闭。我更新我的答案。 – Mete
- 1. 如何在Android上从互联网获取当前时间
- 2. 如何在xcode中使用互联网获取当前位置?
- 3. 在android中获取当前联系人
- 4. 如何从asp.net(不是系统日期)直接从互联网获取当前日期?
- 5. 在SQL Server中获取当前日期?
- 6. 在java中获取当前日期
- 7. 在Swift 3中获取当前日期?
- 8. 获取互联网
- 9. Android的日期选择器(获取当前日期)
- 10. 获取当前的QNetworkInterface激活并连接到互联网
- 11. 获取当前类型的互联网连接
- 12. Android - 获取当前日期(今天)日期
- 13. 获取当前游戏状态并通过互联网发送
- 14. T-SQL:从日期表中获取当前日期+前几年
- 15. 如何在Android中的sqlite中获取当前日期和上一年当前日期之间的值
- 16. 在Android中从互联网读取XML
- 17. 如何在字符串中获取当前系统日期 - ANDROID
- 18. 如何在Android中获取当前日期(最小API 10)
- 19. 如何在android中获取当前日期?
- 20. 我如何在Android中获取当前系统日期?
- 21. 获取当前日期和前一天
- 22. 在变量上获取当前日期
- 23. 直到连接到互联网才能获取当前位置 - Android GPS
- 24. 如何在android中使用特定日期获取当前日期名称?
- 25. 从python当前日期获取7天前的日期
- 26. 获取没有互联网
- 27. 参考当前日期获取一周中的日期数组
- 28. PHP获取日期的当前月份
- 29. 获取当前日期的年份
- 30. 获取当前日期的Rails型号
没有,有没有像在Android系统。 –
这可能有助于https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/net/SntpClient.java –
您可以使用任何第三方服务,或者您可以编写web服务得到帽子 – Sree