public static void getTimeFromGoogleServerNoAsync(final Context context) { 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("Google Time "+dateStr); PreferencesHelper preferencesHelper = new PreferencesHelper(context); preferencesHelper.setGoogleTime(dateStr); SimpleDateFormat sdf3 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH); Date d1 = null; try { d1 = sdf3.parse(dateStr); } catch (Exception e) { e.printStackTrace(); } System.out.println("OTP Google Time" + d1); Calendar calendar = Calendar.getInstance(); System.out.println("OTP Device Time" + calendar.getTime()); } 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()); } }
No comments:
Post a Comment