是否有Google API可以从时区获取当前时间?如果有,我该如何使用它? 我希望从特定时区获得当前在线时间,例如获取“当前时间:03/08/2015 11:27:54”或类似内容。我想从谷歌那里得到它,因为它充满信心。
谢谢!
答案 0 :(得分:3)
这足以得到我想要的东西:
使用HttpGet,Client和Response,我设法从响应日期标题中获取服务器的当前时间。我可以随时打电话给我,并会得到自信的回复(谷歌几乎100%可用,我可以信赖得到正确的日期和时间)
try{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet("https://google.com/"));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
String dateStr = response.getFirstHeader("Date").getValue();
//Here I do something with the Date String
System.out.println(dateStr);
} 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());
}
答案 1 :(得分:2)
这个答案很好地给出了一个如何做到这一点的方法。 https://stackoverflow.com/a/13066298/2864560
雅虎在获取时间数据这样的小事情方面与谷歌一样可靠,所以这不应该是一件麻烦事。您可以使用以下链接从Android(有点像Google)获取时间:https://stackoverflow.com/a/8214204/2864560。
另一个对每个用户来说可靠的是这段代码
LocationManager locMan = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE);
long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();
这实际上为您提供了第二次的时间,因为它使用的数据可能比谷歌更准确