Java设置windows系统之间时间同步


前言

项目开发过程中,业务要求,一台电脑,要根据另一台电脑实时同步时间


运行环境

window10 64位系统
jdk 1.8

一、服务端

服务端启动,通过修改系统注册表,启动NTP服务,作为时间同步服务器。

代码如下(示例):

import java.io.IOException;

class Scratch {
    public static void main(String[] args) {
        String startNtpServer = "REG ADD HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\W32Time\\TimeProviders\\NtpServer /v Enabled /t REG_DWORD /d 1 /f";
        //TODO 设置为“5”,表示强制主机将它自身宣布为可靠的时间源
        String execute2 = "REG ADD HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\W32Time\\Config /v AnnounceFlags /t REG_DWORD /d 5 /f";

        String stopW32Time = "net stop w32Time";
        String startW32Time = "net start w32Time";
        try {
            Runtime.getRuntime().exec(startNtpServer);
            Thread.sleep(100);
            Runtime.getRuntime().exec(execute2);
            Thread.sleep(100);
            Runtime.getRuntime().exec(stopW32Time);
            Thread.sleep(100);
            Process exec = Runtime.getRuntime().exec(startW32Time);
            int res = exec.waitFor();
            if (res != 0) {
                System.out.println("fail");
            } else {
                System.out.println("success");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

二、客户端

客户端启动,通过修改系统注册表,设置时间同步服务器地址,时间同步间隔。

代码如下(示例):

import java.io.IOException;

class Scratch {
    public static void main(String[] args) {
        String host = "192.168.1.100";
        //设置时间同步间隔
        String setInterval = "REG ADD HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\W32Time\\TimeProviders\\NtpClient /v SpecialPollInterval /t REG_DWORD /d 10 /f";
        //添加时间同步服务器列表
        String addTimeHost = "REG ADD HKEY_LOCAL_MACHINE\\SOFTWARE\\MICROSOFT\\WINDOWS\\CURRENTVERSION\\DATETIME\\SERVERS /v 3 /t REG_SZ /d \"" + host + "\" /f";
        String setTimeHost = "REG ADD HKEY_LOCAL_MACHINE\\SOFTWARE\\MICROSOFT\\WINDOWS\\CURRENTVERSION\\DATETIME\\SERVERS /ve /t REG_SZ /d 3 /f";
        //设置时间同步服务器地址
        String setParameter = "REG ADD HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\W32Time\\Parameters /v NtpServer /t REG_SZ /d \"" + host + ",0x9\" /f";

        String stopW32Time = "net stop w32Time";
        String startW32Time = "net start w32Time";
        try {
            Runtime.getRuntime().exec(addTimeHost);
            Thread.sleep(100);
            Runtime.getRuntime().exec(setTimeHost);
            Thread.sleep(100);
            Runtime.getRuntime().exec(setInterval);
            Thread.sleep(100);
            Runtime.getRuntime().exec(setParameter);
            Thread.sleep(100);
            Runtime.getRuntime().exec(stopW32Time);
            Thread.sleep(100);
            Process exec = Runtime.getRuntime().exec(startW32Time);
            int res = exec.waitFor();
            if (res != 0) {
                System.out.println("fail");
            } else {
                System.out.println("success");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

总结

以上就是Windows下,通过注册表,指定时间同步服务器。 参考链接: [link](https://blog.csdn.net/RBPicsdn/article/details/80805926/). [link](https://www.cnblogs.com/liangqihui/p/7230881.html)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows系统时间同步可以通过设置NTP时间服务器来实现。对于能够连接互联网的系统,可以设置Windows系统自动进行NTP时间同步,连接互联网上的时间服务器即可。对于局域网环境,需要在该网络系统设置时间服务器,客户端访问时间服务器进行NTP时间同步。可以使用SymmTime软件来配置时间服务器和进行实际同步设置。在SymmTime软件中,可以设置同步的时机、修正门限和异常处理等参数。同时,需要设置开机自动启动SymmTime软件,并以管理员身份运行,以实现对系统时间的自动更改。可以使用Windows系统的任务计划程序功能来实现开机自动启动,并注意设置管理员身份运行的注意事项。此外,还需要启动Windows Time服务并设置开机启动,以及重启w32time服务来同步时间设置。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [windows系统开机自动进行NTP时间同步系统时间修正](https://blog.csdn.net/wangjikun/article/details/124998826)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [windows时间同步](https://blog.csdn.net/ghost_emperor/article/details/130519356)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值