通过java应用程序在Windows机器上设置日期和时间的最佳方法 [英] Best way to set date and time on windows machine by java application

查看:717
本文介绍了通过java应用程序在Windows机器上设置日期和时间的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用代理java应用程序,它安装在世界不同地方的几台Windows机器上。我想定期同步Windows时钟(日期和时间)。我已经找到了用java代码在windows中设置时间的本机命令:

I'm working with an agent java application and it is installed on several Windows machines in different places of the world. I would like periodically synchronize windows clock (Date and Time). I have already found the native command to set time in windows by java code:

Runtime.getRuntime().exec("cmd /C date " + strDateToSet); // dd-MM-yy
Runtime.getRuntime().exec("cmd /C time " + strTimeToSet); // hh:mm:ss

或执行

Runtime.getRuntime().exec("cmd /C date " + strDateToSet + "& time " + strTimeToSet);

但主要问题是在设定日期聚焦,因为Windows机器上的日期格式可能是所有机器都不一样。例如,我可以为意大利机器提供 dd-MM-yy ,为美国机器提供 yy-MM-dd 。因此,如果我的应用程序设置日期格式为dd-MM-yy wuold对于美国机器是错误的。

But the main problem is focalizied on set date, because is possible that the date format on windows machines is not the same for all machines. For example I could have dd-MM-yy for Italian machine and yy-MM-dd for US machine. So if my application set the date with format dd-MM-yy wuold be wrong for US machine.

知道我不能使用NTP(机器进入局域网与防火墙仅限规则协议HTTPS端口443)
如何通过Java应用程序为所有Windows机器正确设置日期?
这是最简单和可维护性的最佳解决方案?

Knowing that I cant't use NTP (Machines into LAN with Firewall with out rules only protocol HTTPS port 443) how can I set date correctly by java application for all windows machines ? Which is the best solution both semplicity and maintainability ?

注意代理java应用程序已经在Web服务响应传递的Windows机器上设置了时间戳因此,只需要在Windows上设置格式为日期yyyy-MM-dd的setDateAndTime

TEST exec date命令(设置错误的日期):

TEST exec date command with format date yyyy-MM-dd on Windows (set wrong date):

推荐答案

我尝试使用JNA导入kernel32来实现解决方案。 DLL在时区UTC + 1(意大利国家)的Windows 7计算机上执行测试。

I tried to implement the solution with JNA importing kernel32.dll performed the test on Windows 7 machine with timezone UTC+1 (Italy country).

我描述了以下步骤:

1)我导入了我的maven项目后面的依赖项:

1) I imported my maven project the followed dependencies:

<dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna-platform</artifactId>
        <version>4.4.0</version>
</dependency>
<dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <version>4.3.0</version>
</dependency>

2)我实施了以下课程:

2) I implemented the followed class:

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinBase.SYSTEMTIME;
import com.sun.jna.win32.StdCallLibrary;


@Component
@Qualifier("windowsSetSystemTime")
public class WindowsSetSystemTime {

    /**
     * Kernel32 DLL Interface. kernel32.dll uses the __stdcall calling
     * convention (check the function declaration for "WINAPI" or "PASCAL"), so
     * extend StdCallLibrary Most C libraries will just extend
     * com.sun.jna.Library,
     */
    public interface Kernel32 extends StdCallLibrary {

        boolean SetLocalTime(SYSTEMTIME st);

        Kernel32 instance = (Kernel32) Native.loadLibrary("kernel32.dll", Kernel32.class);

    }

    public boolean SetLocalTime(SYSTEMTIME st) {
        return Kernel32.instance.SetLocalTime(st);
    }

    public boolean SetLocalTime(short wYear, short wMonth, short wDay, short wHour, short wMinute, short wSecond) {
        SYSTEMTIME st = new SYSTEMTIME();
        st.wYear = wYear;
        st.wMonth = wMonth;
        st.wDay = wDay;
        st.wHour = wHour;
        st.wMinute = wMinute;
        st.wSecond = wSecond;
        return SetLocalTime(st);
    }
}

3)通过测试类我尝试设置跟随日期和时间

3) By the test class I tried to set the followed date and time

public void setTime(){
        System.out.println("START SYNC " + windowsSetSystemTime);

        windowsSetSystemTime.SetLocalTime((short)2017, (short)10,(short) 29,(short) 11,(short) 35,(short) 0);
    }

测试结果:
结果在这种情况下我得到了正确的日期和时间,因为该功能被认为是在2017年10月29日3:00进入的白天冬季节省时间。

TEST RESULT: As result in this case I obtained the correct date and time because the function considered daylight winter time saving that enter at 29 October 2017 3:00.

在测试之前,时钟已经设定:

Before test, clock was set:

设置测试时钟后:

我在链接的Windows开发人员文档中找到了逻辑SetLocalTime方法到Kernel32.dll中:
SetLocalTime文档

I found out the logic SetLocalTime method into Kernel32.dll by Windows dev center documentation at link: SetLocalTime documentation

Windows开发人员中心备注设置LocalTime:

Windows Dev center REMARKS SetLocalTime:


系统内部使用UTC。因此,当您调用SetLocalTime时,系统将使用当前时区信息执行转换,包括夏令时设置。请注意,系统使用当前时间的夏令时设置,而不是您设置的新时间。因此,为了确保正确的结果,请再次调用SetLocalTime,现在第一次调用已更新夏令时设置。

The system uses UTC internally. Therefore, when you call SetLocalTime, the system uses the current time zone information to perform the conversion, including the daylight saving time setting. Note that the system uses the daylight saving time setting of the current time, not the new time you are setting. Therefore, to ensure the correct result, call SetLocalTime a second time, now that the first call has updated the daylight saving time setting.

这篇关于通过java应用程序在Windows机器上设置日期和时间的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆