-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncTime.java
More file actions
153 lines (140 loc) · 4.99 KB
/
SyncTime.java
File metadata and controls
153 lines (140 loc) · 4.99 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* 同步时间
*/
public class SyncTime {
private static int sleepMinutes = 0;
private static final long EPOCH_OFFSET_MILLIS;
private static final String[] hostName = {"time-a.nist.gov", "time-nw.nist.gov", "time.nist.gov"};
static {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
// Java使用的参照标准是1970年,而时间服务器返回的秒是相当1900年的,算一下偏移
calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
EPOCH_OFFSET_MILLIS = Math.abs(calendar.getTime().getTime());
}
public static void main(String[] args) {
GetWebTime();
}
private static Date getNetDate(String hostName) {
try {
Socket socket = new Socket(hostName, 37);
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream(),
socket.getReceiveBufferSize());
int b1 = bis.read();
int b2 = bis.read();
int b3 = bis.read();
int b4 = bis.read();
if ((b1 | b2 | b3 | b4) < 0) {
return null;
}
long result = (((long) b1) << 24) + (b2 << 16) + (b3 << 8) + b4;
Date date = new Date(result * 1000 - EPOCH_OFFSET_MILLIS);
socket.close();
print("原子钟时间 " + date + ", hostName " + hostName);
return date;
} catch (IOException ex) {
print(ex.toString());
return null;
}
}
/**
* 通过ping命令判断是否离线
*
* @return
*/
public static boolean isOffline() {
Runtime run = Runtime.getRuntime();
try {
Process process = run.exec("ping www.hao123.com");
InputStream s = process.getInputStream();
BufferedReader bis = new BufferedReader(new InputStreamReader(s));
String str = bis.readLine();
if (str != null) {
print("网站返回的数据 " + str);
// if (str.startsWith("Reply from")) {
return false;
// }
}
process.waitFor();
} catch (IOException | InterruptedException ex) {
print(ex.toString());
}
return true;
}
/**
* 通过调用本地命令date和time修改计算机时间
*
* @param date
*/
private static void setComputeDate(Date date) {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
c.setTime(date);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
c.setTime(new Date());
int year_c = c.get(Calendar.YEAR);
int month_c = c.get(Calendar.MONTH) + 1;
int day_c = c.get(Calendar.DAY_OF_MONTH);
int hour_c = c.get(Calendar.HOUR_OF_DAY);
int minute_c = c.get(Calendar.MINUTE);
String ymd = year + "-" + month + "-" + day;
String time = hour + ":" + minute + ":" + second;
try {
// 日期不一致就修改一下日期
if (year != year_c || month != month_c || day != day_c) {
String cmd = "cmd /c date " + ymd;
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
}
// 时间不一致就修改一下时间
if (hour != hour_c || minute != minute_c) {
String cmd = "cmd /c time " + time;
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
}
} catch (IOException | InterruptedException ex) {
print("修改失败 " + ex.toString());
}
}
public static void GetWebTime() {
// 检测电脑是否在线
if (isOffline()) {
// 重试
while (sleepMinutes < 30) {
try {
Thread.sleep(1000 * 5 * 60);
sleepMinutes += 2;
GetWebTime();
} catch (InterruptedException ex) {
print(ex.toString());
}
}
// 30分钟还没有联线,表示就不上网了,退出吧
System.exit(0);
} else {
// 从网络上获取时间
for (String name : hostName) {
Date date = getNetDate(name);
if (date != null) {
setComputeDate(date);
break;
}
}
}
}
static void print(String msg) {
System.out.println("------ " + msg);
}
}