Get Actual Time From Internet ?
Solution 1:
According to this answer you can get the current time from an NTP server.
support.ntp.org library
Add to your dependency
StringtimeServer="server 0.pool.ntp.org";
NTPUDPClienttimeClient=newNTPUDPClient();
InetAddressinetAddress= InetAddress.getByName(timeServer);
TimeInfotimeInfo= timeClient.getTime(inetAddress);
longreturnTime= timeInfo.getReturnTime();
System.out.println(returnTime)
Solution 2:
You can use a rest full api provided by geo names http://www.geonames.org/login it will require lat and long for this purpose for example
http://api.geonames.org/timezoneJSON?lat=51.5034070&lng=-0.1275920&username=your_user_name
Solution 3:
Getting the time from the third-party servers is not reliable most of the times and some of them are paid services.
If you want to get the exact time and check with the phone whether it is correct or not, irrespective of the proper way, you can use the following simple trick to get the actual time.
privateclassGetActualTimeextendsAsyncTask<String, Void, String> {
@Overrideprotected String doInBackground(String... urls) {
try {
HttpURLConnectionurlConnection=null;
StringBuilderresult=newStringBuilder();
try {
URLurl=newURL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
intcode= urlConnection.getResponseCode();
if (code == 200) {
InputStreamin=newBufferedInputStream(urlConnection.getInputStream());
BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(in));
Stringline="";
while ((line = bufferedReader.readLine()) != null)
result.append(line);
in.close();
}
else {
return"error on fetching";
}
return result.toString();
} catch (MalformedURLException e) {
return"malformed URL";
} catch (IOException e) {
return"io exception";
} finally {
if (urlConnection != null) {urlConnection.disconnect();
}
}
} catch (Exception e) { return"null"; }
}
@OverrideprotectedvoidonPostExecute(String time) {
Calendarcalendar= Calendar.getInstance();
SimpleDateFormatmdformat=newSimpleDateFormat("h:mm");
Stringtimes= mdformat.format(calendar.getTime());
try {
Stringareatime= time.substring(time.indexOf(String.valueOf(times)), time.indexOf(String.valueOf(times)) + 5).trim();
Toast.makeText(this, "The actual time is " + areatime, Toast.LENGTH_SHORT).show();
}
catch(IndexOutOfBoundsException e){
Toast.makeText(this, "Mobile time is not same as Internet time", Toast.LENGTH_SHORT).show();
}
}
}
}
Call the class in the onCreate();
new GetActualTime().execute("https://www.google.com/search?q=time");
So this is actually getting the time from Google. This works pretty awesomely in my projects. In order to check whether the system time is wrong, you can use this trick. Instead of depending on the time servers, you can trust Google.
As it is more sensitive in checking, even a minute ahead or lag will catch the exception. You can customise the code if you want to handle that.
Solution 4:
For Android:
Add to gradle app module:
compile'commons-net:commons-net:3.3'
Add to your code:
...
StringTAG="YOUR_APP_TAG";
StringTIME_SERVER="0.europe.pool.ntp.org";
...
publicvoidcheckTimeServer() {
try {
NTPUDPClienttimeClient=newNTPUDPClient();
InetAddressinetAddress= InetAddress.getByName(TIME_SERVER);
TimeInfotimeInfo= timeClient.getTime(inetAddress);
longsetverTime= timeInfo.getMessage().getTransmitTimeStamp().getTime();
// store this somewhere and use to correct system timelongtimeCorrection= System.currentTimeMillis()-setverTime;
} catch (Exception e) {
Log.v(TAG,"Time server error - "+e.getLocalizedMessage());
}
}
NOTE: timeInfo.getReturnTime() as mentioned in an earlier answer will get you local system time, if you need server time you must use timeInfo.getMessage().getTransmitTimeStamp().getTime().
Solution 5:
Try this :
System.currentTimeMillis();
Post a Comment for "Get Actual Time From Internet ?"