如何从 Google 获取 Android 的当前时间?
How to get current time from Google for Android?
是否有 Google API 从时区获取当前时间?如果有,我该如何使用它?
我想从特定时区获取当前在线时间,比如获取 "current time: 03/08/2015 11:27:54" 或类似的东西。我想从Google那里得到它,因为它有信心。
谢谢!
这个答案很好地给出了如何做到这一点的方法。
Yahoo 在获取时间数据等小事上与 Google 一样可靠,因此这应该不会太麻烦。您可以通过使用此 link 从 Android(有点像 Google)获取时间:.
另一个对每个用户来说都是可靠的是这段代码
LocationManager locMan = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE);
long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();
这基本上可以让您将时间精确到秒,因为它使用的数据可能比 Google 的
更准确
这足以得到我想要的:
使用 HttpGet、客户端和响应,我设法从响应日期 Header 获取服务器的当前时间。我可以随时调用它,并且会得到自信的响应(Google 几乎 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());
}
是否有 Google API 从时区获取当前时间?如果有,我该如何使用它? 我想从特定时区获取当前在线时间,比如获取 "current time: 03/08/2015 11:27:54" 或类似的东西。我想从Google那里得到它,因为它有信心。
谢谢!
这个答案很好地给出了如何做到这一点的方法。
Yahoo 在获取时间数据等小事上与 Google 一样可靠,因此这应该不会太麻烦。您可以通过使用此 link 从 Android(有点像 Google)获取时间:.
另一个对每个用户来说都是可靠的是这段代码
LocationManager locMan = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE);
long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();
这基本上可以让您将时间精确到秒,因为它使用的数据可能比 Google 的
更准确这足以得到我想要的:
使用 HttpGet、客户端和响应,我设法从响应日期 Header 获取服务器的当前时间。我可以随时调用它,并且会得到自信的响应(Google 几乎 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());
}