wood burning stoves 2.0*
The moose likes Java in General and the fly likes Time output not updating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Time output not updating" Watch "Time output not updating" New topic
Author

Time output not updating

upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

Hi,
I was just writing a program in which i want the output as acutal time like this
Sun Jun 16 15:23:18 IST 2013
Sun Jun 16 15:23:19 IST 2013
Sun Jun 16 15:23:20 IST 2013 ...
but i am getting output as
Sun Jun 16 15:23:18 IST 2013
Sun Jun 16 15:23:18 IST 2013
Sun Jun 16 15:23:18 IST 2013
Sun Jun 16 15:23:18 IST 2013 ...
Any help would be great

[Added code tags - see UseCodeTags for details]

Thanks
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

You're waiting a second between each output, but you keep outputting the same time. Date is an immutable object, so d is going to contain that time regardless of how long you wait.

The only point in that code where you actually update a date value is when you create a new Calendar. Try just creating a new Date object each time you go round the loop.

(I split this into a new topic, because it's a new question)
upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

Thanks for your reply Mathew,
I put the new Calendar Instance in the for loop
import java.util.*;
class Main_T implements Runnable
{
public void run(){
go();
}
public void go()
{
for(int i=1;i<=10;i++)
{
Calendar c= Calendar.getInstance();
Date d=c.getTime();
System.out.println(d);
try{
Thread.sleep(1000);
}catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
}
}
class Time
{
public static void main (String ar[])
{
Runnable one=new Main_T();
Thread t=new Thread(one);
t.start();
}
}
as getInstance is a static method i have couple of thingd tha
1) all the static stuff in class is loaded before the execution of main method so how in the above code the static stuff is called before the go method.
2) static instance is one per class so how new instance is creating every time getInstance is called.
Thanks

upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

Hi Ranch,
Kindly reply to my last post
Thanks
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

upanshu vaid wrote:
2) static instance is one per class so how new instance is creating every time getInstance is called.

Where did you find that Calendar.getInstance returns the same instance on every call?
Also you better use ScheduledExecutorService to call Runnable with fixed rate.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Matthew Brown wrote:Date is an immutable object

No it isn't. You can modify a Date using setTime or any of its other setters. Still, you have to update it (or its source) to get a different value.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11441
    
  16


This:

is just like doing this:


Would you expect x to change on each iteration of the loop?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

fred rosenberger wrote:
This:

Would you expect x to change on each iteration of the loop?

Yeah, if "c" was modified from another thread while iterating from 1 to 6 .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Time output not updating