Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7902983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:38:36+00:00 2026-06-03T09:38:36+00:00

I’m developing this time container which basically contains instances of a class called week,

  • 0

I’m developing this time container which basically contains instances of a class called week, which is made of 2 Calendar instances (start and end) and a lot of other Administrative information’s (for now not relevant)

The problem is that i create these Week instances in a while loop, and i see that during the creation of the week instances (printing the calendars milliseconds in the while loop), the millis are fine, but if i check the content of the list of weeks, they are not! all the instances of Calendar (start and end) contain the same values for all weeks.

I have wrote a bit of code to let you guys understand the problem clearly, there are the classes (Container and BiggerContainer):

public class Container
{
  private static final long serialVersionUID = 1L;

  private Calendar          start;

  private Calendar          end;

  public Container(Calendar start, Calendar end)
  {

    this.start = start;
    this.end = end;

  }

  public Calendar getStart()
  {
    return start;
  }

  public Calendar getEnd()
  {
    return end;
  }
}

Bigger container:

    public class BiggerContainer
{
  private static final long    serialVersionUID = 1L;

  private ArrayList<Container> containerList    = new ArrayList<Container>();

  public void init()
  {

    int i = 0;

    long max = 604800000;

    long nu = Calendar.getInstance().getTimeInMillis();

    Calendar startC = Calendar.getInstance();

    Calendar endC = Calendar.getInstance();

    while (i <= 5)
    {

      startC.setTimeInMillis(nu);
      endC.setTimeInMillis(nu + max);
      System.out.println("At creation time: " + startC.getTimeInMillis() + " / " + endC.getTimeInMillis());
      containerList.add(new Container(startC, endC));

      nu += max;
      i++;
    }
  }

  public void show()
  {

    for (Container c : containerList)
    {

      System.out.println("Start millis: " + c.getStart().getTimeInMillis() + " end millis "
          + c.getEnd().getTimeInMillis());
    }
  }

  /**
   * TEST
   * 
   * @param args
   */
  public static void main(String[] args)
  {

    BiggerContainer bc = new BiggerContainer();

    bc.init();

    bc.show();
  }

}

Output:

At creation time: 1336480993036 / 1337085793036
At creation time: 1337085793036 / 1337690593036
At creation time: 1337690593036 / 1338295393036
At creation time: 1338295393036 / 1338900193036
At creation time: 1338900193036 / 1339504993036
At creation time: 1339504993036 / 1340109793036


Start millis: 1339504993036 end millis 1340109793036
Start millis: 1339504993036 end millis 1340109793036
Start millis: 1339504993036 end millis 1340109793036
Start millis: 1339504993036 end millis 1340109793036
Start millis: 1339504993036 end millis 1340109793036
Start millis: 1339504993036 end millis 1340109793036

As you can see during the creation of the Container instances in the init() method the Calendar instances provide the correct amount of millis, while in the show method, they print all the same millis…

Do you have any idea?
I have the impression that its a very newby mistake, but i cant figure out where i’m doing wrong

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:38:37+00:00Added an answer on June 3, 2026 at 9:38 am

    You are reusing the same instance of Calendar (startC and endC) for all your container’s. Either make a “clone”/”copy” of the calendar in your constructor or create the calendar instances in your loop.

      public Container(Calendar start, Calendar end)
      {
    
        this.start = (Calendar) start.clone();
        this.end = (Calendar) end.clone();
      }
    

    or

    while (i <= 5)
    {
      Calendar startC = Calendar.getInstance();
      Calendar endC = Calendar.getInstance();
      startC.setTimeInMillis(nu);
      endC.setTimeInMillis(nu + max);
      System.out.println("At creation time: " + startC.getTimeInMillis() 
              + " / " + endC.getTimeInMillis());
      containerList.add(new Container(startC, endC));
    
      nu += max;
      i++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.