ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2021-11-03 10:05:52  阅读:262  来源: 互联网

标签:同步 Java String exec windows getRuntime sleep Runtime REG


文章目录


前言

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


运行环境

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)

标签:同步,Java,String,exec,windows,getRuntime,sleep,Runtime,REG
来源: https://blog.csdn.net/huihuangaaa/article/details/121090279

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有