用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(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 , short wMilliseconds) {
SYSTEMTIME st = new SYSTEMTIME();
st.wYear = wYear;
st.wMonth = wMonth;
st.wDay = wDay;
st.wHour = wHour;
st.wMinute = wMinute;
st.wSecond = wSecond;
st.wMilliseconds=wMilliseconds;
return SetLocalTime(st);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
setWindowsTIme.java
调用刚才的方法,修改windows系统时间
import java.text.SimpleDateFormat;
public class setWindowsTIme{
public static void main(String[] args){
String dateStr1=“2018-08-30 19:00:00.000”
WindowsSetSystemTime w=new WindowsSetSystemTime();
w.SetLocalTime(
Short.parseShort(dateStr1.substring(0, 4).trim()),
Short.parseShort(dateStr1.substring(5, 7).trim()),
Short.parseShort(dateStr1.substring(8, 10).trim()),
Short.parseShort(dateStr1.substring(11, 13).trim()),
Short.parseShort(dateStr1.substring(14, 16).trim()),
Short.parseShort(dateStr1.substring(17, 19).trim()),
Short.parseShort(dateStr1.substring(20, 23).trim())
);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
setLInuxTIme.java
修改linux系统时间
import java.text.SimpleDateFormat;
public class setTime {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String dateStr = dateformat.format(System.currentTimeMillis());
System.out.println(dateStr);
System.out.println(System.currentTimeMillis());
String[] command ={"date","-s","2017-05-13 21:59:10.123"};
// String command ="date"+" -s \""+"2017-05-13 21:59:10.123\"";
// String command ="cd" +"\\n";
System.out.println(command);
Process pr=Runtime.getRuntime().exec(command);
pr.waitFor();
// System.out.println("pr.waitFor();"+);
String dateStr1 = dateformat.format(System.currentTimeMillis());
System.out.println(dateStr1);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
String[] command ={“date”,”-s”,”2017-05-13 21:59:10.123”};
// String command =”date”+” -s \”“+”2017-05-13 21:59:10.123\”“;
这两种命令字符创,上一种能够修改系统时间,下面的不能修改系统时间。并不清楚什么原因。在CSDN上看到的基本都是第一种写法。
最好加上waitfor()方法,不然又肯修改不成功。
————————————————
版权声明:本文为CSDN博主「qq_16751339」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_16751339/article/details/82227102