• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple question about reloading Date Variable..

 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic