aspose file tools
The moose likes Java in General and the fly likes Simple question about reloading Date Variable.. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Simple question about reloading Date Variable.." Watch "Simple question about reloading Date Variable.." New topic
Author

Simple question about reloading Date Variable..

Sam Smoot
Ranch Hand

Joined: Apr 18, 2002
Posts: 238
This is probably not advanced enough for this forum, but..

I'm trying to get a "clock" to work out in java (actually as a JSP, but for now, in Java). When I execute the following code, I get the same time over and over, but I (thought) am reloading the time value at every iteration of the loop...




What am I missing?

Thanks in advance.


CNSS/NSA Infosec Professional,<br />Software Engineer
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
You need to put inside your while loop.

And if you want to put a pause in your code, use Thread.sleep().
[ August 18, 2006: Message edited by: Joanne Neal ]

Joanne
Brian Mozhdehi
Ranch Hand

Joined: Aug 17, 2006
Posts: 81
The getTime() method returns the time as represented when the date object was created, not the current time. It never updates unless you update its attributes in your code.

You can think of this as an object representing a time, not an API object to access the time if that makes sense.

Try this:

private void clock()
{
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");

while(true)
{
Date d = new Date();
System.out.print(df.format(d));

for(int i=0;i<100;i++)
{
for(int j=0;j<1000;j++)
{
for(int k=0;k<10000;k++)
{
}
}
}
}
}

you may wish to replace the while(true) stuff with Thread.sleep(x) or some other sleeping mechanism where x is the number of milliseconds you wish to sleep. Your code will be processor intensive (just looping like that)
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Take a look at Timer. Use that to fire an update once every second. In the interest of accuracy I would update by getting the actual time, not by increasing some variable by one to indicate on second. Reason being you're less likely to have small errors in time cumulate.
Sam Smoot
Ranch Hand

Joined: Apr 18, 2002
Posts: 238
Thanks for the responses.. I haven't really had to code in a while, so I'm trying to catch back up. I'll let you know what I wind up with.

As a general review question: Won't the "new Date()" call cause a bunch of Date objects in memory? If so, when can they be expired to be collected by the garbage collector or would that really matter?

Thanks again.
Sam
[ August 21, 2006: Message edited by: Sam Smoot ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Simple question about reloading Date Variable..
 
Similar Threads
Popup menu not responding with mouse event
ArrayIndex in Arrays
default JFileChooser to details view Mode
Printing in Array in Ascending order
Part of program not printing output