• Post Reply Bookmark Topic Watch Topic
  • New Topic

GregorianCalendar -- Calendar.SECOND  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here all three Calendar.SECOND values are same. how? i am creating three different objects for each.

class sec {

public sec() {
GregorianCalendar cal2 = new GregorianCalendar();
int currentIndex2 = cal2.get(Calendar.SECOND);
System.out.println("constrctor..second2.."+currentIndex2);
}

public static void main(String args[]) {
sec se = new sec();

GregorianCalendar cal = new GregorianCalendar();
int currentIndex = cal.get(Calendar.SECOND);
System.out.println("second.."+currentIndex);

GregorianCalendar cal1 = new GregorianCalendar();
int currentIndex1 = cal1.get(Calendar.SECOND);
System.out.println("second1.."+currentIndex1);
}
}
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's really just turning out that way because your statements are being executed so quickly. Because your command take a matter of milliseconds to complete, it's very likely that all 3 Calendar objects will be created at the same second.

I added this line after the "new sec();" line:



It's just a simple way to ensure that the first Calendar object is created well before the others because the application gets stuck in this loop for quite a while.

When I did that, I found that the first object was created at "34" while the last two were created at "52". (Obviously, your results may vary.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!