「java获取互联网时间」java获取互联网时间的方法

博主:adminadmin 2022-12-05 02:27:07 67

本篇文章给大家谈谈java获取互联网时间,以及java获取互联网时间的方法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用Java怎样获得互联网时间?

连接到时间服务器的 13 号端口,这是标准服务。只要这个服务器声称提供时间服务就是这个端口,右击windows 任务栏时钟,在第三个标签页“时间同步”中找服务器地址列表。

C:\ telnet time.nist.gov 13 

那么你想在 Java 中做到这点,只需要写一个简单的 TCP Client 连接到这个服务器的 13 号端口,然后 socket.getInputStream 读取内容,解析其中的时间。

java获取网络当前时间 时间不对

CST是美国时间啊大哥,就是说该连接提供的方法拿的是美国时间

java获取网络当前时间

如果你要获取的是Internet时间,可以使用NTP服务。

NTP概念简介 

Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。

java实现:

import java.io.InputStream;

import java.net.Socket;

public class TimeUtil {

    public static final int DEFAULT_PORT = 37;//NTP服务器端口

    public static final String DEFAULT_HOST = "time-nw.nist.gov";//NTP服务器地址

    private TimeUtil() {

    };

    public static long currentTimeMillis(Boolean sync) {

        if (sync != null  sync.booleanValue() != true)

            return System.currentTimeMillis();

        try {

            return syncCurrentTime();

        } catch (Exception e) {

            return System.currentTimeMillis();

        }

    }

    public static long syncCurrentTime()  throws Exception {

        // The time protocol sets the epoch at 1900,

        // the java Date class at 1970. This number

        // converts between them.

        long differenceBetweenEpochs = 2208988800L;

        // If you'd rather not use the magic number uncomment

        // the following section which calculates it directly.

        /*

         * TimeZone gmt = TimeZone.getTimeZone("GMT"); Calendar epoch1900 =

         * Calendar.getInstance(gmt); epoch1900.set(1900, 01, 01, 00, 00, 00);

         * long epoch1900ms = epoch1900.getTime().getTime(); Calendar epoch1970

         * = Calendar.getInstance(gmt); epoch1970.set(1970, 01, 01, 00, 00, 00);

         * long epoch1970ms = epoch1970.getTime().getTime();

         * 

         * long differenceInMS = epoch1970ms - epoch1900ms; long

         * differenceBetweenEpochs = differenceInMS/1000;

         */

        InputStream raw = null;

        try {

            Socket theSocket = new Socket(DEFAULT_HOST, DEFAULT_PORT);

            raw = theSocket.getInputStream();

            long secondsSince1900 = 0;

            for (int i = 0; i  4; i++) {

                secondsSince1900 = (secondsSince1900  8) | raw.read();

            }

            if (raw != null)

                raw.close();

            long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;

            long msSince1970 = secondsSince1970 * 1000;

            return msSince1970;

        } catch (Exception e) {

            throw new Exception(e);

        }

    }

}

中国大概能用的NTP时间服务器 

     server 133.100.11.8 prefer 

     server 210.72.145.44 

     server 203.117.180.36 //程序中所用的 

     server 131.107.1.10 

     server time.asia.apple.com 

     server 64.236.96.53 

     server 130.149.17.21 

     server 66.92.68.246 

     server  

     server 18.145.0.30 

     server clock.via.net 

     server 137.92.140.80 

     server 133.100.9.2 

     server 128.118.46.3 

     server ntp.nasa.gov 

     server 129.7.1.66 

     server ntp-sop.inria.frserver 210.72.145.44(国家授时中心服务器IP地址) 

     ntpdate 131.107.1.10 

     ntpdate -s time.asia.apple.com

java时间问题 项目写了个程序用到了系统当前时间 但是这个时间容易被篡改 想获取internet上的时间

我觉得这个问题你不用担心 一般情况下你获取本机的时间 如果项目部署到服务器那么 服务器时间是正规的不会改 那么你获取服务器时间就不会出现错误 服务器时间一般都是固定的北京时间

java获取互联网时间的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java获取互联网时间的方法、java获取互联网时间的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-05,除非注明,否则均为首码项目网原创文章,转载请注明出处。