This question already has an answer here:
这个问题在这里已有答案:
- In Java, how do I get the difference in seconds between 2 dates? 11 answers
在Java中,如何获得2个日期之间的秒数差异? 11个答案
I'm trting to send data every 10 seconds , so I'm trying tp use this code I wrote but I think I made a mistake in the converting to seconds?
我每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));
}
1 个解决方案
#1
3
If you're "trying to send data every 10 seconds", you can just do
如果您“尝试每10秒发送一次数据”,您就可以这样做
ScheduledExecutorService service = Executors.newScheduledThreadPool();
service.scheduleAtFixedRate(() -> sendUdp("OK"), 10, 10, TimeUnit.SECONDS);
#1
3
If you're "trying to send data every 10 seconds", you can just do
如果您“尝试每10秒发送一次数据”,您就可以这样做
ScheduledExecutorService service = Executors.newScheduledThreadPool();
service.scheduleAtFixedRate(() -> sendUdp("OK"), 10, 10, TimeUnit.SECONDS);