5

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.

Thanks!

IIRed-DeathII
  • 1,117
  • 2
  • 15
  • 34

2 Answers2

2

This answer does well to give a method on how to do this. https://stackoverflow.com/a/13066298/2864560

Yahoo is just as reliable as Google in terms of small things like getting Time data, so this should not be so much of a hassle. You could get the time from Android (kinda like Google) by using this link: https://stackoverflow.com/a/8214204/2864560.

Another that would be reliable per user is this little piece of code

LocationManager locMan = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE);
long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();

That essentially gives you the time down to the second since it is using data that could be even more accurate than Google's

Community
  • 1
  • 1
Tobi Akerele
  • 962
  • 10
  • 20
  • With the last example I may get not updated time. Low security – IIRed-DeathII Aug 03 '15 at 14:51
  • And just tried the Yahoo example and get refuse connection. – IIRed-DeathII Aug 03 '15 at 14:52
  • I just ran the Yahoo one and I got the time in PDT (Pacific DayLight Time), are you sure you added the required permissions like Wifi State, Internet, e.t.c? – Tobi Akerele Aug 03 '15 at 15:01
  • I have just tried the Yahoo code again (without making any changes) and got the answer. So this is telling me that I am not able to get the time all the time (has periods where I get refuse response), failing on one of my requisites: Confidence. Thanks any way for the help :) – IIRed-DeathII Aug 04 '15 at 11:23
2

This is enough to get what I wanted:

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());
    }
IIRed-DeathII
  • 1,117
  • 2
  • 15
  • 34