我正在制作一个分布式的Java应用程序,我需要应用程序的两个部分在一个标准时间运行。由于系统时间可能不同,我在想是否java API包含一些从标准在线源下载时间的类?
答案 0 :(得分:6)
您需要使用NTP(网络时间协议):
http://en.wikipedia.org/wiki/Network_Time_Protocol
以下链接包含一些用于与NTP服务器交互的参考Java NTP客户端代码:
答案 1 :(得分:3)
这是我发现的其他代码..我正在使用它。 这使用了apache commons库。
// List of time servers: http://tf.nist.gov/service/time-servers.html
import java.net.InetAddress;
import java.util.Date;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
public class TimeLookup {
public static final String TIME_SERVER = "time-a.nist.gov";
public static void main(String[] args) throws Exception {
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getReturnTime();
Date time = new Date(returnTime);
System.out.println("Time from " + TIME_SERVER + ": " + time);
}
}
答案 2 :(得分:1)
注意.... timeInfo.getReturnTime()
不会从timeserver
返回当前时间。它返回了对server
的请求的本地时间。
在调用timeInfo.computeDetails()
之后,可以通过timeInfo.getOffset().
获得偏移量,这将返回本地时间的偏移量(毫秒)。
计算您可以执行以下操作的当前时间:
...
long systemtime = System.currentTimeMillis();
Date realdate = new Date(systemtime + timeInfo.getOffset());
...
答案 3 :(得分:1)
感谢Rajendra_Prasad,这是真的
public static void main(String[] args) throws Exception {
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getReturnTime();
Date time = new Date(returnTime);
long systemtime = System.currentTimeMillis();
timeInfo.computeDetails();
Date realdate = new Date(systemtime + timeInfo.getOffset());
System.out.println("Time from " + TIME_SERVER + ": " + time);
System.out.println("Time from " + TIME_SERVER + ": " + realdate);
System.out.println(""+time.getTime());
}
答案 4 :(得分:0)
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class TimeValidationHook {
private static String getServerHttpDate(String serverUrl) throws IOException {
URL url = new URL(serverUrl);
URLConnection connection = url.openConnection();
Map<String, List<String>> httpHeaders = connection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : httpHeaders.entrySet()) {
String headerName = entry.getKey();
if (headerName != null && headerName.equalsIgnoreCase("date")) {
return entry.getValue().get(0);
}
}
return null;
}
public static void main(String[] args) throws IOException {
String serverUrl = args.length > 0 ? args[0] : "https://www.google.co.in";
System.out.println(getServerHttpDate(serverUrl));
Timestamp ts = new Timestamp(System.currentTimeMillis());
System.out.println(ts);
//formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US) Tue, 24 Jul 2018 13:25:37 GMT
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = null;
Date date1 = null;
try
{
date1. = sdf.parse(getServerHttpDate(serverUrl));
date2 = sdf2.parse(ts.toString());
}
catch (ParseException e)
{
e.printStackTrace();
}
System.out.println("date1 : " + sdf2.format(date1));
System.out.println("date2 : " + sdf2.format(date2));
if (date1.compareTo(date2) > 0) {
System.out.println("Date1 is after Date2");
List<String> cmd = new ArrayList<String>();
cmd.add("hg rollback");
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.directory(new File("E:\\repos\\Trunk"));
Process p = pb.start();
} else if (date1.compareTo(date2) < 0) {
System.out.println("Date1 is before Date2");
} else if (date1.compareTo(date2) == 0) {
System.out.println("Date1 is equal to Date2");
} else {
System.out.println("How to get here?");
}
}
}