vb 获取网络时间

vb 获取网络时间,第1张

下列代码不用任何控件就能从国家授时中心网页获取时间获得网络时间。

Function NetTime(Optional url As String) As String '返回包括时间和日期的字符串

Dim obj, OBJStatus, Retrieval

Dim GetText As String

Dim i As Long

Dim myDate As Date

Set Retrieval = CreateObject("MicrosoftXML>

需要一个客户组件 客户1连接(“time-atimefreqbldrdocgov”,13)'他的返回时间是英国格林威治时间 反馈代码如下 版本2 子程序_客户1_数据到达局部变量取回数据,文本型局部变量时间,文本型,,"0"局部变量日期,文本型,,"3"局部变量sj1,日期时间型 取回数据=取字节集数据(客户1取回数据(),#文本型,)时间=分割文本(取回数据,“”,)日期=分割文本(时间[2],“-”,)日期[1]=“20”+日期[1]sj1=增减时间(到时间(日期[1]+“/”+日期[2]+“/”+日期[3]+“/”+时间[3]),#小时,8)信息框(sj1,0,)客户1断开连接()

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

NTP概念简介 

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

java实现:

import javaioInputStream;

import javanetSocket;

public class TimeUtil {

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

    public static final String DEFAULT_HOST = "time-nwnistgov";//NTP服务器地址

    private TimeUtil() {

    };

    public static long currentTimeMillis(Boolean sync) {

        if (sync != null && syncbooleanValue() != true)

            return SystemcurrentTimeMillis();

        try {

            return syncCurrentTime();

        } catch (Exception e) {

            return SystemcurrentTimeMillis();

        }

    }

    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 = TimeZonegetTimeZone("GMT"); Calendar epoch1900 =

          CalendargetInstance(gmt); epoch1900set(1900, 01, 01, 00, 00, 00);

          long epoch1900ms = epoch1900getTime()getTime(); Calendar epoch1970

          = CalendargetInstance(gmt); epoch1970set(1970, 01, 01, 00, 00, 00);

          long epoch1970ms = epoch1970getTime()getTime();

          

          long differenceInMS = epoch1970ms - epoch1900ms; long

          differenceBetweenEpochs = differenceInMS/1000;

         /

        InputStream raw = null;

        try {

            Socket theSocket = new Socket(DEFAULT_HOST, DEFAULT_PORT);

            raw = theSocketgetInputStream();

            long secondsSince1900 = 0;

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

                secondsSince1900 = (secondsSince1900 << 8) | rawread();

            }

            if (raw != null)

                rawclose();

            long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;

            long msSince1970 = secondsSince1970  1000;

            return msSince1970;

        } catch (Exception e) {

            throw new Exception(e);

        }

    }

}

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

     server 133100118 prefer 

     server 2107214544 

     server 20311718036 //程序中所用的 

     server 131107110 

     server timeasiaapplecom 

     server 642369653 

     server 1301491721 

     server 669268246 

     server >

以上就是关于vb 获取网络时间全部的内容,包括:vb 获取网络时间、如何将GMT转换为本地时间、易语言获取网络时间等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9574419.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存