-1

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?

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)); 
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
Korenron
  • 101
  • 2
  • 10
  • What dates are you putting in? What number did you get? What number did you expect? – khelwood May 24 '18 at 14:36
  • Why aren't you just use a `ScheduledExecutor` for sending so you don't have to calculate a difference at all? – daniu May 24 '18 at 14:37
  • Your `TimeBetweenDates` method seems like it should be correct except that in Java method and variable names begin with a lowercase letter. I find it confusing that you have parameters `Start` and `End` and variables `start` and `end`. In what way does your observed behaviour differ from the desired? – Ole V.V. May 24 '18 at 14:54

1 Answers1

4

If you're "trying to send data every 10 seconds", you can just do

ScheduledExecutorService service = Executors.newScheduledThreadPool();

service.scheduleAtFixedRate(() -> sendUdp("OK"), 10, 10, TimeUnit.SECONDS);
daniu
  • 14,137
  • 4
  • 32
  • 53
  • this is not good for my beacue I need to make an IF with the send . if time is more then 10 or the data is not the same - the send. – Korenron May 25 '18 at 11:13
  • @Korenron I did mean for you to adjust the submitted task, you can easily add the check you want. – daniu May 26 '18 at 06:29