Is there a way to get the date & time from the network provider programmatically? Possibly in the same manner in which we get the location from the network provider.
2 Answers
You may need to use a custom NITZ or NTP Library
So just a heads up that Android uses NITZ events provided by a carrier to properly set the system date and time. Android also falls-back to network NTP automatically when no cellular network is available.
http://en.wikipedia.org/wiki/NITZ
The time provided by currentTimeMillis() will typically be the best available time, and it's what all of the services on the device use, like Calendar and Alarm Clock.
However
However the NTP API isn't somewhere that Java code can access, which means we're back to using an existing Java NTP/SNTP client library if we want an accurate time regardless of whether we are on a network that is NITZ capable.
Java NTP library
You can find a naive implementation of a Java NTP Library from

- 2,390
- 16
- 12
-
1why it's not provided by android itself,as it's the basic need of developer it their application,anyone can make the Application fool by changing the time if we use Calendar – Rishabh Agrawal Oct 25 '15 at 19:20
The Answer is YES I tried the following code to get the time from network provider use loc.getTime() for that
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
latitude.setText(""+loc.getLatitude());
longitude.setText(""+loc.getLongitude());
time.setText(""+loc.getTime());
}
-
Isn't this the exact time of the last know location? And it's not from the carrier, but from the GPS (although the latter is more accurate) – rds Dec 01 '11 at 17:07
-
It could be from either, there is a LocationManager.GPS_PROVIDER and a LocationManager.NETWORK_PROVIDER – matt5784 Jun 19 '13 at 18:23
-
this does not get the time, it displays the time of the moment when the last location was fetched. what if you are immobile and not moving? your location will not change and thus you will not get the time updated? – Raphael C Oct 16 '19 at 11:12