-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTime.java
More file actions
62 lines (50 loc) · 1.87 KB
/
Time.java
File metadata and controls
62 lines (50 loc) · 1.87 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
import java.net.*;
import java.text.*;
import java.util.Date;
import java.io.*;
public class Time {
private static final String HOSTNAME = "time.nist.gov";
public static void main(String[] args) throws IOException, ParseException {
Date d = Time.getDateFromNetwork();
System.out.println("It is " + d);
}
public static Date getDateFromNetwork() throws IOException, ParseException {
// The time protocol sets the epoch at 1900,
// the Java Date class at 1970. This number
// converts between them.
long differenceBetweenEpochs = 2208988800L;
// If you'd rather not use the magic number, uncomment
// the following section which calculates it directly.
/*
TimeZone gmt = TimeZone.getTimeZone("GMT");
Calendar epoch1900 = Calendar.getInstance(gmt);
epoch1900.set(1900, 01, 01, 00, 00, 00);
long epoch1900ms = epoch1900.getTime().getTime();
Calendar epoch1970 = Calendar.getInstance(gmt);
epoch1970.set(1970, 01, 01, 00, 00, 00);
long epoch1970ms = epoch1970.getTime().getTime();
long differenceInMS = epoch1970ms - epoch1900ms;
long differenceBetweenEpochs = differenceInMS/1000;
*/
Socket socket = null;
try {
socket = new Socket(HOSTNAME, 37);
socket.setSoTimeout(15000);
InputStream raw = socket.getInputStream();
long secondsSince1900 = 0;
for (int i = 0; i < 4; i++) {
secondsSince1900 = (secondsSince1900 << 8) | raw.read();
}
long secondsSince1970
= secondsSince1900 - differenceBetweenEpochs;
long msSince1970 = secondsSince1970 * 1000;
Date time = new Date(msSince1970);
return time;
} finally {
try {
if (socket != null) socket.close();
}
catch (IOException ex) {}
}
}
}