File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Looping accoridng to time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Looping accoridng to time" Watch "Looping accoridng to time" New topic

Looping accoridng to time

Steve De Costa
Ranch Hand

Joined: Jun 04, 2007
Posts: 33
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

Joined: Aug 05, 2005
Posts: 3742
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

Joined: Jun 04, 2007
Posts: 33
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

Joined: Aug 16, 2005
Posts: 15101

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Looping accoridng to time
jQuery in Action, 3rd edition