Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Looping accoridng to time

 
Steve De Costa
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ho do I run a loop according to time. For instance if I want the loop to display time after every 30 seconds for 3 minutes, how would I do that?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could put a Thread.sleep call in the loop that sleeps for 30 seconds, but the better way to do it is using a TimerTask object.
 
Steve De Costa
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help.

Though there is once concern. I know I will be suing a DATE object from Java.util class.

ONce initialzied, the date and time doesn't get updated itself. How would I update that.

How would I code it such that

Date currTime = new Date()

where variable currTime always contains the most up to the second time. Is there a way to do that ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just create a new Date object every time you need the current date and time. When you create a new Date object, it will be initialized with the current system time.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you thinking something like this?

See if you can do that in almost exactly that many lines of Java. Note that it might not use the same format or datatype for "current time" all three times. Your toolkit includes new Date() and System.currentTimeMillis()

This waits 30 seconds after the last display before exiting the loop. Can you fix that?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic