如何获取当前时间从谷歌为Android?时间、Android

2023-09-07 12:03:49 作者:余生勿扰

有一个谷歌API来获取当前的时间从一个时区?如果有,如何使用它? 我想目前的上网时间从一个特定的时区,想获得当前时间:2015年3月8日11点27分54秒或类似的东西。我想要得到的,因为它从谷歌的信心。

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.

谢谢!

推荐答案

这是足以让我想要的东西:

This is enough to get what I wanted:

使用HTTPGET,客户端和响应,我设法得到的答复日期标题服务器的当前时间。我可以调用所有我想要的时间,将获得自信的反应(谷歌提供几乎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());
    }