Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Time output not updating

 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranch,
Kindly reply to my last post
Thanks
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This:

is just like doing this:


Would you expect x to change on each iteration of the loop?
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic