我每10秒钟发送一次数据, 所以我尝试使用我编写的代码 但我认为我在转换到秒时犯了一个错误?
Calendar Time= Calendar.getInstance();
Calendar SendDate= Calendar.getInstance();
long upload = TimeBetweenDates(SendTime,Time);
if (upload > 10000) {
String udp = "OK";
SendUDP(udp);
SendTime = Calendar.getInstance();
}
public static long TimeBetweenDates (Calendar Start , Calendar End) {
long end = End.getTimeInMillis();
long start = Start.getTimeInMillis();
return TimeUnit.MILLISECONDS.toSeconds(Math.abs(end - start));
}
答案 0 :(得分:3)
如果你"尝试每隔10秒发送一次数据",你可以这样做
ScheduledExecutorService service = Executors.newScheduledThreadPool();
service.scheduleAtFixedRate(() -> sendUdp("OK"), 10, 10, TimeUnit.SECONDS);