如何从网络提供商获得日期和时间?

时间:2022-04-04 15:54:02

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 个解决方案

#1


4  

You may need to use a custom NITZ or NTP Library

您可能需要使用自定义的NITZ或NTP库

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.

所以要注意,Android使用了运营商提供的NITZ事件来正确设置系统日期和时间。Android还可以在没有蜂窝网络可用时自动返回到网络NTP。

http://en.wikipedia.org/wiki/NITZ

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.

currentTimeMillis()提供的时间通常是最好的可用时间,设备上的所有服务都使用这个时间,比如日历和闹钟。

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.

然而,NTP API并不是Java代码可以访问的地方,这意味着如果我们想要一个准确的时间,不管我们是否在一个可以使用NITZ的网络上,我们都要回到使用现有的Java NTP/SNTP客户端库。

Java NTP library

Java国家结核控制规划库

You can find a naive implementation of a Java NTP Library from

您可以从Java NTP库中找到一个简单的实现

#2


0  

The Answer is YES I tried the following code to get the time from network provider use loc.getTime() for that

答案是肯定的,我尝试了下面的代码来获取网络提供者使用location . gettime()的时间

    @Override
    public void onLocationChanged(Location loc)
    {
        loc.getLatitude();
        loc.getLongitude();

        latitude.setText(""+loc.getLatitude());
        longitude.setText(""+loc.getLongitude());
        time.setText(""+loc.getTime());
    }

#1


4  

You may need to use a custom NITZ or NTP Library

您可能需要使用自定义的NITZ或NTP库

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.

所以要注意,Android使用了运营商提供的NITZ事件来正确设置系统日期和时间。Android还可以在没有蜂窝网络可用时自动返回到网络NTP。

http://en.wikipedia.org/wiki/NITZ

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.

currentTimeMillis()提供的时间通常是最好的可用时间,设备上的所有服务都使用这个时间,比如日历和闹钟。

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.

然而,NTP API并不是Java代码可以访问的地方,这意味着如果我们想要一个准确的时间,不管我们是否在一个可以使用NITZ的网络上,我们都要回到使用现有的Java NTP/SNTP客户端库。

Java NTP library

Java国家结核控制规划库

You can find a naive implementation of a Java NTP Library from

您可以从Java NTP库中找到一个简单的实现

#2


0  

The Answer is YES I tried the following code to get the time from network provider use loc.getTime() for that

答案是肯定的,我尝试了下面的代码来获取网络提供者使用location . gettime()的时间

    @Override
    public void onLocationChanged(Location loc)
    {
        loc.getLatitude();
        loc.getLongitude();

        latitude.setText(""+loc.getLatitude());
        longitude.setText(""+loc.getLongitude());
        time.setText(""+loc.getTime());
    }