-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)); } 
3
  • What dates are you putting in? What number did you get? What number did you expect? Commented May 24, 2018 at 14:36
  • Why aren't you just use a ScheduledExecutor for sending so you don't have to calculate a difference at all? Commented May 24, 2018 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? Commented May 24, 2018 at 14:54

1 Answer 1

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); 
Sign up to request clarification or add additional context in comments.

2 Comments

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 I did mean for you to adjust the submitted task, you can easily add the check you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.