• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do GregorianCalendars work?  RSS feed

 
verduka fox
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble with GregorianCalendars. I set their value and add them to a Vector. As I add them to the Vector, I print out their value and see that they are the value that I want. When I get finished adding them to the Vector, I then iterate through the Vector and print the GregorianCalendars. At this point, all of the GregorianCalendars have the same value: the value of the last GregorianCalendar that was added to the Vector. I am using add() instead of set() because the API says:
unlike set(), add() forces an immediate recomputation of the calendar's milliseconds and all fields.
Your assistance is greatly appreciated!!

Running the code above displays:
01/02/2003
01/03/2003
01/04/2003
01/05/2003
01/06/2003
************************

0 :: 01/06/2003
1 :: 01/06/2003
2 :: 01/06/2003
3 :: 01/06/2003
4 :: 01/06/2003
 
Irene Loos
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Every time you did this
gc.add(Calendar.DATE, 1);
you modified the object "gc" to hold different value. So by the time you did last modification, all the entries in the vector had the same value in their object portion.
You can create new object every time for each entry in the Vector, then you should not have this problem.
Hope this will help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!