Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get actual time from internet ?

How to get 'current(actual) time' or 'network operator's time' programmatically if device time is changed ?

I'm trying to get current time through 'getLastKnownLocation' method of 'LocationManager' class. But it gives last location time, but I need current time.

Can anyone tell me a clue about the correct way to get actual time from internet ?

  • If possible without using any external library.

Thanks in advance.

like image 420
Tejas Patel Avatar asked Mar 13 '23 10:03

Tejas Patel


2 Answers

According to this answer you can get the current time from an NTP server.

support.ntp.org library

Add to your dependency

String timeServer = "server 0.pool.ntp.org";
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(timeServer);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getReturnTime();
System.out.println(returnTime)
like image 94
Sathish Kumar Avatar answered Mar 31 '23 17:03

Sathish Kumar


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

like image 28
Adeel Turk Avatar answered Mar 31 '23 16:03

Adeel Turk