如何从 Google for Android 获取当前时间? [英] How to get current time from Google for Android?
问题描述
是否有用于从时区获取当前时间的 Google API?如果有,我该如何使用它?我想从特定时区获取当前在线时间,例如获取当前时间:03/08/2015 11:27:54"或类似的内容.我想从 Google 那里得到它,因为它很有信心.
Is there a Google API to get current time from a timezone? If there is, how do I use it? I want to get current online time from a specific timezone, like getting "current time: 03/08/2015 11:27:54" or something like that. I want to get it from Google because of it confidence.
谢谢!
推荐答案
这足以得到我想要的:
使用 HttpGet、客户端和响应,我设法从响应日期标头中获取服务器的当前时间.我可以随时调用它,并且会得到自信的回复(Google 几乎 100% 可用,我可以相信获得正确的日期和时间)
Using the HttpGet, Client and Response, I manage to get a server's current time from the response Date Header. I can call this all the times I want and will get confident responses (Google is almost 100% available and I can trust on getting correct Date and Time)
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());
}
这篇关于如何从 Google for Android 获取当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!