用Java修改windows和linux系统时间(精确到毫秒)
项目中需要同步客户端和服务器时间。要求精确到毫秒。Java目前没有直接修改系统时间的方法。这里用到了jna.
JNA在线帮助文档:http://twall.github.io/jna/4.0/javadoc/
JNA入门示例:https://github.com/twall/jna/blob/master/www/GettingStarted.md
快捷键
具体代码如下
WindowsSetSystemTime.java
生成可调用C语言的Java方法
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinBase.SYSTEMTIME;
import com.sun.jna.win32.StdCallLibrary;
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(SYST